<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/collectLedger";
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: 'totalMoney',
            type: 'sum'
          },
          {
            name: 'true',
            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: "totalMoney",
            overHidden: true,
          },
          {
            label: "实收金额",
            prop: "paymentAmountTl",
            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, 'd', 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() {
      console.log(this.tradeType)
      this.$confirm('是否导出应收总账详情信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        window.open(
          `/api/blade-sales-part/order/statistics-exportDetails?${
            this.website.tokenHeader
          }=${getToken()}&exportType=2&customerId=${this.customerId}&bsType=CG&businesDateList[0]=${this.startDate}&businesDateList[1]=${this.endDate}`
        );
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消' //
        });
      })
    },
    // 跳转页面
    jumpPage(row, index) {
      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>
