<template>
  <div>
    <basic-container class="page-crad">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        :before-open="beforeOpen"
        :page.sync="page"
        :search.sync="search"
        :cell-style="cellStyle"
        @search-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @search-reset="searchReset"
        @on-load="onLoad"
        :table-loading="loading"
        :summary-method="summaryMethod"
        @saveColumn="saveColumn"
        @resetColumn="resetColumn"
        @search-criteria-switch="searchCriteriaSwitch"
      >
        <template slot="menuLeft">
          <el-button
            type="info"
            size="small"
            icon="el-icon-download"
            @click="outExport"
            :disabled="dataList.length == 0"
            >导出</el-button
          >
        </template>
        <template slot="corpIdSearch">
          <crop-select v-model="search.corpId" corpType="KH"></crop-select>
        </template>
        <!--        <template slot="dcSearch">-->
        <!--          <el-select-->
        <!--            v-model="search.dc"-->
        <!--            filterable-->
        <!--          >-->
        <!--            <el-option-->
        <!--              label="收"-->
        <!--              value="d"-->
        <!--            />-->
        <!--            <el-option-->
        <!--              label="付"-->
        <!--              value="c"-->
        <!--            />-->
        <!--          </el-select>-->
        <!--        </template>-->
        <template slot="corpId" slot-scope="{ row }">
          <span>{{ row.corpName }}</span>
        </template>
        <template slot="dc" slot-scope="{ row }">
          <span>{{ row.dc == "d" ? "收" : "付" }}</span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getToken } from "@/util/auth";
import { getList, getProfitItem } from "@/api/statisticAnalysis/salesProfit";
import { micrometerFormat } from "@/util/validate";
import _ from "lodash";
import { getAccList, exportAcctList } from "@/api/statisticAnalysis/payment";
export default {
  name: "index",
  data() {
    return {
      form: {},
      search: {
        dc: "d"
      },
      dataList: [],
      loading: false,
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      option: {
        searchShow: true,
        searchMenuSpan: 16,
        align: "center",
        searchSpan: 8,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        showSummary: true,
        searchIcon: true,
        searchIndex: 2,
        menu: false,
        column: [
          {
            label: "订单号",
            prop: "accSysNo",
            width: 100,
            search: true
          },
          {
            label: "订单日期",
            prop: "businesDate",
            overHidden: true,
            width: 100,
            hide: true,
            showColumn: false,
            search: true,
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            type: "date",
            unlinkPanels: true,
            searchRange: true
          },
          {
            label: "客户名称",
            prop: "corpId",
            overHidden: true,
            width: 100,
            search: true,
            formatter: row => {
              return row.corpsName;
            }
          },
          {
            label: "收付类别",
            prop: "dc",
            overHidden: true,
            hide: true,
            showColumn: false,
            search: true,
            width: 100,
            searchValue: "c",
            type: "select",
            dicData: [
              {
                label: "收",
                value: "d"
              },
              {
                label: "付",
                value: "c"
              }
            ]
          },
          {
            label: "商品名称",
            prop: "goodName",
            overHidden: true,
            width: 100
          },
          {
            label: "型号",
            prop: "goodType",
            overHidden: true,
            width: 100
          },
          {
            label: "数量",
            prop: "quantity",
            overHidden: true,
            width: 100
          },
          {
            label: "销售单价",
            prop: "price",
            overHidden: true,
            width: 100
          },
          {
            label: "金额",
            prop: "amount",
            overHidden: true,
            width: 100
          },
          {
            label: "币别",
            prop: "currency",
            overHidden: true,
            width: 100
          },
          {
            label: "汇率",
            prop: "exchangeRate",
            overHidden: true,
            width: 100
          },
          {
            label: "结算金额",
            prop: "settlementAmount",
            overHidden: true,
            width: 100
          },
          {
            label: "发票号",
            prop: "invoice",
            overHidden: true,
            width: 100
          },
          {
            label: "备注",
            prop: "remarks",
            overHidden: true,
            width: 100
          }
        ]
      },
      tradeType: ""
    };
  },
  filters: {
    decimalFormat(num) {
      return num ? Number(num).toFixed(2) : "0.00";
    }
  },
  created() {},
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      if (params.businesDate) {
        params.contractStartDate = params.businesDate[0];
        params.contractEndDate = params.businesDate[1];
      }
      delete params.businesDate;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    searchReset() {
      this.search = {
        dc: "c"
      };
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    getSysType() {
      const sysType = localStorage.getItem("sysitemType");
      if (sysType == 6) {
        this.tradeType = "JXS";
      } else if (sysType == 5) {
        this.tradeType = "SW";
      } else if (sysType == 4) {
        this.tradeType = "CK";
      } else if (sysType == 3) {
        this.tradeType = "JK";
      } else if (sysType == 2) {
        this.tradeType = "GN";
      } else if (sysType == 1) {
        this.tradeType = "XX";
      }
    },
    onLoad(page, params) {
      this.getSysType();
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false);
      });
      let queryParams = Object.assign(
        { tradeType: this.tradeType },
        params,
        this.search,
        {
          size: page.pageSize,
          current: page.currentPage
        }
      );
      if (this.search.businesDate && this.search.businesDate.length > 0) {
        queryParams = {
          ...queryParams,
          accDateStart: this.search.businesDate[0],
          accDateEnd: this.search.businesDate[1]
        };
      }
      delete queryParams.businesDate;
      this.loading = true;
      getAccList(queryParams)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
          this.option.height = window.innerHeight - 210;
          this.$nextTick(() => {
            this.$refs.crud.doLayout();
          });
        })
        .finally(() => {
          this.loading = false;
        });
    },
    editOpen(row) {
      if (row.billType == "BJ") {
        this.$router.push({
          path: "/exportTrade/customerInquiry/index",
          query: {
            id: row.id
          }
        });
      } else {
        this.$router.push({
          path: "/exportTrade/salesContract/index",
          query: {
            id: row.id
          }
        });
      }
    },
    outExport() {
      let accDateStart = "";
      let accDateEnd = "";
      if (this.search.businesDate && this.search.businesDate.length > 0) {
        accDateStart = this.search.businesDate[0];
        accDateEnd = this.search.businesDate[1];
      }
      let a = { ...this.search };
      // let itemList = JSON.stringify(a)
      // let result = itemList.replace(/""/g, "null");
      // a = JSON.parse(result)
      // if (!a.corpId) {
      //   a.corpId = ''
      // }
      window.open(
        `/api/trade-finance/acc/exportAccMessage?${
          this.website.tokenHeader
        }=${getToken()}&corpId=${
          a.corpId
        }&accDateStart=${accDateStart}&accDateEnd=${accDateEnd}&dc=${
          a.dc
        }&accSysNo=${a.accSysNo}&tradeType=${this.tradeType}`
      );
    },
    summaryMethod({ columns, data }) {
      const sums = [];
      if (columns.length > 0) {
        columns.forEach((item, index) => {
          sums[0] = "合计";
          if (item.property == "purchasePrice" || item.property == "amount") {
            let amountSum = 0;
            let purchaseAmountSum = 0;
            data.forEach(e => {
              amountSum = _.add(amountSum, Number(e.amount));
              purchaseAmountSum = _.add(
                purchaseAmountSum,
                Number(e.purchasePrice)
              );
            });
            //入库金额总计
            if (item.property == "purchasePrice") {
              sums[index] = micrometerFormat(purchaseAmountSum);
            }
            if (item.property == "amount") {
              sums[index] = micrometerFormat(amountSum);
            }
          }
        });
      }
      return sums;
    }
  }
};
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}
.itemTable ::v-deep .el-table {
  width: 100%;
}
</style>
