<template>
  <basic-container>
    <avue-crud :option="option"
               :data="dataList"
               ref="crud"
               v-model="form"
               :page.sync="page"
               :search.sync="search"
               :table-loading="loading"
               :span-method="spanMethod"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @saveColumn="saveColumn"
               :cell-style="cellStyle"
               @resetColumn="resetColumn"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button size="small"
                   type="info"
                   icon="el-icon-download"
                   :loading="exportLoading"
                   @click.stop="downFile"
        >导出
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import option from "./config/mainList.json";
  import { getProfitList } from "@/api/statisticAnalysis/profit";
  import { contrastList } from "@/util/contrastData";

  export default {
    data() {
      return {
        loading : false,
        exportLoading:false,
        form: {},
        search:{},
        show:true,
        detailData:{},
        option: {},
        spanArr:[],
        position:0,
        parentId:0,
        dataList: [],
        page: {
          pageSize: 10,
          pagerCount: 5,
          total: 0,
        },
        query:{},
        configuration:{
          multipleChoices:false,
          multiple:false,
          disabled:false,
          searchShow:true,
          collapseTags:false,
          clearable:true,
          placeholder:'请点击右边按钮选择',
          dicData:[]
        },
      }
    },
    async created() {
      this.option = await this.getColumnData(this.getColumnName(66), option);
    },
    mounted() {

    },
    methods: {
      //导出
      downFile(){
        this.exportLoading = true
        let  searchParams = Object.assign({},this.search);
        let param = this.paramsAdjustment(searchParams)
        getProfitList(1, -1,param).then(res =>{
          const fileData = this.deepClone(res.data.data.records)

          option.column.shift();
          option.column.shift();

          this.$Export.excel({
            title: "利润分析",
            columns: option.column,
            data: fileData,
          });
        }).finally(()=>{
          this.exportLoading = false
        })
      },
      addReceipt(){
        console.log('1')
      },
      editOpen(){
        console.log('1')
      },
      rowspan() {
        //记录原始
        let oldList = [this.dataList[0].corpName,this.dataList[0].orderNo,this.dataList[0].invoiceWeight,this.dataList[0].customDutyMoney,
          this.dataList[0].gstMoney,this.dataList[0].quotaMoney,this.dataList[0].itemMoney,this.dataList[0].unitPrice]
        this.dataList.forEach((item,index)=>{
          let newList = [item.corpName,item.orderNo,item.invoiceWeight,item.customDutyMoney,item.gstMoney,item.quotaMoney,item.itemMoney,item.unitPrice]
          if(index===0){
            this.spanArr.push(1)
            this.position=0;
          }else{
            //如果循环数据与原始数据相等  那么合并
            if(!contrastList(oldList,newList)){
              this.spanArr[this.position] +=1;
              this.spanArr.push(0)
            }else{
              //如果不等 更新原始数据  循环在比较
              oldList = newList
              this.spanArr.push(1)
              this.position = index
            }
          }
        })
      },
      spanMethod({ row, column, rowIndex, columnIndex }) {
        if (column.property === "corpName" || column.property === "orderNo" || column.property === "invoiceWeight" || column.property === "customDutyMoney" ||
        column.property === "gstMoney" || column.property === "quotaMoney" || column.property === "itemMoney" || column.property === "unitPrice") {
          const _row=this.spanArr[rowIndex];
          const _col=_row>0?1:0;
          return {
            rowspan:_row,
            colspan:_col
          }
        }
      },
      //点击搜索按钮触发
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done()
      },
      searchReset() {
        console.log('1')
      },
      selectionChange() {
        console.log('1')
      },
      currentChange(val) {
        this.page.currentPage = val
      },
      sizeChange() {
        console.log('1')
      },
      refreshChange() {
        this.onLoad(this.page);
      },
      paramsAdjustment(params) {
        params = Object.assign({}, this.search);
        if (params.createTime &&  params.createTime.length !==0 ) {  //发货
          params.startTime = params.createTime[0]+ " " + "00:00:00";
          params.endTime = params.createTime[1] + " " + "23:59:59";
          this.$delete(params,'createTime')
        }
        return params
      },
      onLoad(page, params) {
        this.loading = true;
        params = this.paramsAdjustment(params)
        getProfitList(page.currentPage, page.pageSize,params).then(res=>{
          this.dataList = res.data.data.records
          this.page.total = res.data.data.total
          if (this.page.total || this.page.total === 0) {
            this.option.height = window.innerHeight - 230;
          }
          this.rowspan()
        }).finally(()=>{
          this.loading = false;
        })
      },
      cellStyle() {
        return "padding:0;height:40px;";
      },
      //列保存触发
      async saveColumn() {
        const inSave = await this.saveColumnData(
          this.getColumnName(66),
          this.option
        );
        if (inSave) {
          this.$message.success("保存成功");
          //关闭窗口
          this.$refs.crud.$refs.dialogColumn.columnBox = false;
        }
      },
      async resetColumn() {
        const inSave = await this.delColumnData(
          this.getColumnName(66),
          option
        );
        if (inSave) {
          this.$message.success("重置成功");
          this.option = option;
          //关闭窗口
          this.$refs.crud.$refs.dialogColumn.columnBox = false;
        }
      },
    }
  }
</script>

<style scoped>

</style>
