<template>
  <div>
    <div class="customer-head">
      <div class="customer-back">
        <el-button type="danger" style="border: none;background: none;color: red" icon="el-icon-arrow-left"
                   @click="backToList">返回列表
        </el-button>
      </div>
    </div>
    <basic-container class="page-crad" style="margin-top: 30px">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        :table-loading="loading"
        :cell-style="cellStyle"
        :key="crudIndex"
      >
        <template slot="menuLeft">
          <el-button type="info" size="small" @click="outExport" icon="el-icon-download">导出</el-button>
        </template>
        <template slot="accSysNo" slot-scope="scope">
          <span style="color: #409EFF;cursor: pointer" @click.stop="jumpPage(scope.row,scope.index)">{{ scope.row.accSysNo }}</span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {detail} from "@/api/statisticAnalysis/profitLedger";
import {getList} from "@/api/tirePartsMall/salesManagement/saleOrder";
import { getToken } from "@/util/auth";

export default {
  name: "detail",
  props: {
    detailData: {
      type: Object
    },
    tradeType: {
      type: [Number, String]
    }
  },
  data() {
    return {
      startDate: '',
      endDate: '',
      customerId: '',
      crudIndex: 0,
      dataList: [],
      loading: false,
      option: {
        searchShow: true,
        align: "center",
        searchSpan: 8,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        cellBtn: false,
        cancelBtn: false,
        refreshBtn: false,
        showSummary: true,
        summaryText: '合计',
        sumColumnList: [
          {
            name: 'goodsTotalNum',
            type: 'sum'
          },
          {
            name: 'numberRows',
            type: 'sum'
          },
          {
            name: 'totalMoney',
            type: 'sum'
          },
          {
            name: 'paymentAmountTl',
            type: 'sum'
          },
          {
            name: 'cost',
            type: 'sum'
          },
          {
            name: 'grossProfit',
            type: 'sum'
          },
        ],
        searchIcon: true,
        searchIndex: 2,
        menu: false,
        column: [
          {
            label: "客户",
            prop: "customerName",
            overHidden: true,
          },
          {
            label: "订单号",
            prop: "ordNo",
            overHidden: true,
          },
          {
            label: "业务日期",
            prop: "businesDate",
            overHidden: true,
          },
          {
            label: "数量",
            prop: "goodsTotalNum",
            overHidden: true,
          },
          {
            label: "行数",
            prop: "numberRows",
            overHidden: true,
          },
          {
            label: "收入",
            prop: "totalMoney",
            overHidden: true,
          },
          {
            label: "实收",
            prop: "paymentAmountTl",
            overHidden: true,
          },
          {
            label: "成本",
            prop: "cost",
            overHidden: true,
          },
          {
            label: "利润",
            prop: "grossProfit",
            overHidden: true,
          },
        ],
      },
    }
  },
  created() {
    if (this.detailData) {
      this.queryData(this.detailData);
    }
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    // 日期格式化
    dateFormatting(date) {
      const year = date.getFullYear().toString().padStart(4, '0');
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      console.log(`${year}-${month}-${day}`)
      return `${year}-${month}-${day}`
    },
    queryData(detailData) {
      this.loading = true;
      const date = new Date(detailData.businesDate);
      const y = date.getFullYear()
      const m = date.getMonth()
      const startDate = new Date(y, m, 1);
      const endDate = new Date(y, m + 1, 0);
      const requestData = {
        'customerId': detailData.customerId,
        'bsType': 'XS',
        'current': 1,
        'size': 999999999,
        'businesDateList[0]' : this.dateFormatting(startDate) + " 00:00:00",
        'businesDateList[1]' : this.dateFormatting(endDate) + " 23:59:59"
      };
      this.customerId = detailData.customerId
      this.startDate = this.dateFormatting(startDate) + " 00:00:00"
      this.endDate = this.dateFormatting(endDate) + " 23:59:59"
      getList(requestData).then(res => {
        this.dataList = res.data.data.records;
      }).finally(() => {
        this.loading = false;
        this.option.height = window.innerHeight - 180;
        this.crudIndex++;
      })

      // detail(id, this.tradeType).then(res => {
      //   this.dataList = res.data.data.records;
      // }).finally(() => {
      //   this.loading = false;
      //   this.option.height = window.innerHeight - 180;
      //   this.crudIndex++;
      // })
    },
    backToList() {
      this.$emit("goBack");
    },
    //导出
    outExport() {
      this.$confirm('是否导出应付总账详情信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        window.open(
          `/api/blade-sales-part/order/statistics-exportDetails?${
            this.website.tokenHeader
          }=${getToken()}&exportType=4&customerId=${this.customerId}&bsType=CG&businesDateList[0]=${this.startDate}&businesDateList[1]=${this.endDate}`
        );
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消' //
        });
      })
    },
    // 跳转页面
    jumpPage(row, index) {
      if (row.billType == '申请') {
        if (this.tradeType == '' || this.tradeType == 'JXS') {
          this.$router.$avueRouter.closeTag("/dealer/purchase/index");
          this.$router.push({
            path: "/dealer/purchase/index",
            query: {
              params: row.srcParentId
            },
          });
        }
      } else if (row.billType == '收费') {
        if (this.tradeType == '' || this.tradeType == 'JXS') {
          this.$router.$avueRouter.closeTag("/dealer/sales/index");
          this.$router.push({
            path: "/dealer/sales/index",
            query: {
              params: row.srcParentId
            },
          });
        }
      }
    },
  },
}
</script>

<style scoped>

</style>
