<template>
  <basic-container v-if="show">
    <avue-crud :option="option"
               :data="dataList"
               :table-loading="loading"
               ref="crud"
               v-model="form"
               :page.sync="page"
               :search.sync="search"
               @row-del="rowDel"
               :before-open="beforeOpen"
               :before-close="beforeClose"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @saveColumn="saveColumn"
               @resetColumn="resetColumn"
               @on-load="onLoad">
      <template slot="corpIdSearch">
        <select-component
          v-model="search.corpId"
          :configuration="configuration"
        ></select-component>
      </template>
      <template slot="storageIdSearch">
        <warehouse-select
          v-model="search.storageId"
          :configuration="sConfiguration"
        ></warehouse-select>
      </template>
      <template slot="menuLeft">
        <el-button size="small"
                   type="success"
                   :disabled="selectionList.length != 1"
                   @click.stop="copyBill"
        >复制单据
        </el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="text"
          icon="el-icon-delete"
          size="small"
          @click.stop="rowDel(scope.row,scope.index)"
        >删除
        </el-button>
      </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-scope="scope" slot="corpsName">
        <span style="color: #409EFF;cursor: pointer" @click.stop="beforeOpenPage(scope.row,scope.index)">{{ scope.row.corpsName }}</span>
      </template>
      <template slot-scope="scope" slot="totalQuantity">
        <span>{{ scope.row.totalQuantity | roundNumbers}}</span>
      </template>
    </avue-crud>
  </basic-container>
  <detail-page
    ref="detail"
    @goBack="goBack"
    :detailData="detailData"
    v-else
  ></detail-page>
</template>

<script>
import option from "./config/mainList.json";
import {selectReceiptList,
  removeReceiptList,} from "@/api/importTrade/receipt"
import detailPage from "./detailsPageEdit.vue";
import { roundNumbers } from "@/util/validate";

export default {
  name: "customerInformation",
  data() {
    return {
      form: {},
      show:true,
      loading:false,
      detailData:{},
      selectionList:[],
      option: {},
      search: {},
      configuration:{
        multipleChoices:false,
        multiple:false,
        disabled:false,
        searchShow:true,
        collapseTags:false,
        clearable:true,
        placeholder:'请点击右边按钮选择',
        dicData:[]
      },
      sConfiguration:{
        multipleChoices:false,
        multiple:false,
        disabled:false,
        searchShow:true,
        collapseTags:false,
        clearable:true,
        placeholder:'请点击右边按钮选择',
        dicData:[]
      },
      parentId: 0,
      dataList: [],
      page: {
        pageSize: 10,
        pagerCount: 5,
        total: 0,
      }
    }
  },
  filters: {
    roundNumbers(val){
      return roundNumbers(val);
    }
  },
  components:{
    detailPage
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(40), option);
  },
  activated() {
    if(!this.show && !this.$store.getters.takeStatus){
      this.show = true;
    }
    setTimeout(() => {
      if(this.$route.query.params && this.show){
        this.detailData={
          params:this.$route.query.params
        }
        this.show = false;
        this.$store.commit("TAKE_IN_DETAIL");
      }
    }, 100);
  },
  mounted() {
    // this.option.height = window.innerHeight - 200;
  },
  methods: {
    //删除列表后面的删除按钮触发触发(row, index, done)
    rowDel(row, index, done) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return removeReceiptList(row.id);
      }).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        this.page.currentPage = 1;
        this.onLoad(this.page);
      });
    },
    copyBill(){
      this.detailData = {
        id: this.selectionList[0].id,
        status: 'copy'
      };
      this.show = false;
      this.$store.commit("TAKE_IN_DETAIL");
    },
    //查看跳转页面
    beforeOpenPage(row, status) {
      let lockData = {
        moduleName: 'sh',
        tableName: 'business_delivery',
        billId: row.id,
        no: localStorage.getItem('browserID'),
        billNo:row.orderNo
      }

      this.detailData = {
        id: row.id,
        view:true,
        status: status,
        lockData:lockData
      };
      this.show = false;
      this.$store.commit("TAKE_IN_DETAIL");
    },
    //新增跳转页面
    beforeOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
      this.$store.commit("TAKE_IN_DETAIL");
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
      this.$store.commit("TAKE_IN_DETAIL");
    },
    //点击新增时触发
    beforeClose(done) {
      this.parentId = "";
      const column = this.findObject(this.option.column, "parentId");
      column.value = "";
      column.addDisabled = false;
      done();
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done()
    },
    searchReset() {
      console.log('1')
    },
    selectionChange(row) {
      this.selectionList = row
    },
    currentChange(val) {
      this.page.currentPage = val
    },
    sizeChange() {
      console.log('1')
    },
    refreshChange(params,done) {
      this.onLoad(this.page,params)
    },
    paramsAdjustment(params) {
      params = Object.assign({}, this.search);
      if (params.businessDate &&  params.businessDate.length !==0 ) {  //发货
        params.businessStartDate = params.businessDate[0]+ " " + "00:00:00";
        params.businessEndDate = params.businessDate[1] + " " + "23:59:59";
        this.$delete(params,'businessDate')
      }
      if (params.createTime &&  params.createTime.length !==0 ) {
        params.createStartTime = params.createTime[0]+ " " + "00:00:00";
        params.createEndTime = params.createTime[1] + " " + "23:59:59";
        this.$delete(params,'createTime')
      }
      return params
    },
    onLoad(page, params) {
      this.loading = true

      params = this.paramsAdjustment(params)
      params.size = page.pageSize
      params.current = page.currentPage

      selectReceiptList(params).then(res => {
        this.dataList = res.data.data.records
        this.page.total = res.data.data.total
      }).finally(()=>{
        this.loading = false
      })
    },
    goBack() {
      this.detailData=this.$options.data().detailData
      this.show = true;
      this.onLoad(this.page,this.search)
    },
    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(40),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      const inSave = await this.delColumnData(
        this.getColumnName(40),
        option
      );
      if (inSave) {
        this.$message.success("重置成功");
        this.option = option;
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
  }
}
</script>

<style scoped>

</style>
