<template>
  <div>
    <basic-container class="page-crad">
      <avue-crud ref="crud" :option="option" :data="dataList" :page.sync="page" :search.sync="search"
        :cell-style="cellStyle" @search-change="searchChange" @current-change="currentChange" @size-change="sizeChange"
        @refresh-change="refreshChange" @on-load="onLoad" :table-loading="loading" @saveColumn="saveColumn"
        @resetColumn="resetColumn" @search-criteria-switch="searchCriteriaSwitch">
        <template slot="menuLeft">
          <el-button type="info" size="small" @click="outExport">导出
          </el-button>
        </template>
        <template slot="corpIdSearch">
          <crop-select v-model="search.corpId" corpType="KH"></crop-select>
        </template>
        <template slot-scope="scope" slot="orderNo">
          <span style="color: #409EFF;cursor: pointer" @click.stop="beforeOpenPage(scope.row, scope.index)">{{scope.row.orderNo}}</span>
        </template>
        <template slot="businesDateSearch">
          <el-date-picker v-model="search.businesDate" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期"
            format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
        </template>
        <template slot="corpId" slot-scope="{ row, index }">
          <span>{{ row.corpName }}</span>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { getToken } from "@/util/auth";
import { getList } from "@/api/statisticAnalysis/salesDetails"

export default {
  name: "index",
  data() {
    return {
      form: {},
      search: {},
      dataList: [],
      loading: false,
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      option: {},
      optionBack: {
        searchShow: true,
        searchMenuPosition: "right",
        searchSpan: 8,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        searchIcon: true,
        menu: false,
        summaryText: "合计",
        showSummary: true,
        sumColumnList: [{
          name: "quantity",
          type: "sum",
          decimals: 0
        }, {
          name: "amount",
          type: "sum",
          decimals: 2
        }, {
          name: "settlmentAmount",
          type: "sum",
          decimals: 2
        }, {
          name: "feesAmount",
          type: "sum",
          decimals: 2
        }, {
          name: "specialPrice",
          type: "sum",
          decimals: 2
        }],
        column: [
          {
            label: "订单号",
            prop: "orderNo",
            overHidden: true
          },
          {
            label: "客户名称",
            prop: "corpId",
            overHidden: true,
            search: true,
          },
          // {
          //   label: "品牌",
          //   prop: "billType",
          //   type: "select",
          //   hide: true,
          //   showColumn: false,
          //   search: true
          // },
          {
            label: "日期",
            prop: "careteTime",
            type: "datetime",
            format: 'yyyy-MM-dd',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            searchRange: true,
            searchDefaultTime: ['00:00:00', '23:59:59'],
            overHidden: true,
            search: true,
          },
          {
            label: "业务员",
            prop: "salesName",
            overHidden: true,
            search: true,

          },
          {
            label: "产品编码",
            prop: "commodityCode",
            overHidden: true,
          },
          {
            label: "规格",
            prop: "typeno",
            overHidden: true
          },
          {
            label: "花纹",
            prop: "brandItem",
            overHidden: true
          },
          {
            label: "数量",
            prop: "quantity",
            overHidden: true
          },
          {
            label: "金额",
            prop: "amount",
            overHidden: true
          },
          {
            label: "单价",
            prop: "price",
            overHidden: true
          },
          {
            label: "回款额",
            prop: "settlmentAmount",
            overHidden: true
          },
          {
            label: "包装费",
            prop: "feesAmount",
            overHidden: true
          },
          {
            label: "特价金额",
            prop: "specialPrice",
            overHidden: true
          }
        ]
      }
    };
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(130.1), this.optionBack);
    // this.option = this.optionBack
    console.log(this.option);
    let i = 0;
    this.option.column.forEach(item => {
      if (item.search) i++
    })
    if (i % 3 !== 0) {
      const num = 3 - Number(i % 3)
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
  },
  methods: {
    //查看跳转页面
    beforeOpenPage(row, index) {
      this.$router.$avueRouter.closeTag("/businessManagement/salesOrder/index");
      this.$router.push({
        path: "/businessManagement/salesOrder/index",
        query: { id: row.orderId }
      });
    },
    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) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params) {
      this.loading = true;
      let data = this.deepClone(Object.assign({}, params, this.search));
      if (data.careteTime) {
        data.beginCreateTime = data.careteTime[0]
        data.endCreateTime = data.careteTime[1]
        delete data.careteTime
      }

      getList(
        page.currentPage,
        page.pageSize,
        data
      ).then(res => {
        if (res.data.data.records) {
          res.data.data.records.forEach(e => {
            e.itemLoading = true;
          });
        }
        this.dataList = res.data.data.records ? res.data.data.records : [];
        this.page.total = res.data.data.total;
        if (this.page.total) {
          this.option.height = window.innerHeight - 210;
        }
      }).finally(() => {
        this.loading = false;
      });
    },

    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(this.getColumnName(130.1), this.option);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = this.optionBack;
      const inSave = await this.delColumnData(this.getColumnName(130.1), this.optionBack);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    editOpen(row) {
    },
    outExport() {
      let params = { ...this.search }
      if (params.careteTime && params.careteTime.length > 0) {
        params = {
          ...params,
          beginCreateTime: params.careteTime[0] + ' 00:00:00',
          endCreateTime: params.careteTime[1] + ' 23:59:59',
        }
      } else {
        params = {
          ...params,
          beginCreateTime: '',
          endCreateTime: '',
        }
      }
      window.open(`/api/blade-purchase-sales/exportOrder/selBusinessExport?${this.website.tokenHeader}=${getToken()}&corpId=${params.corpId}&salesName=${params.salesName}&beginCreateTime=${params.beginCreateTime}&endCreateTime=${params.endCreateTime}`);
    }
  }
};
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}

::v-deep .el-table__expanded-cell[class*="cell"] {
  padding: 0px;
}

.itemTable ::v-deep .el-table {
  width: 100%;
}
</style>
