<template>
  <div>
    <basic-container v-show="show" class="page-crad">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        v-model="form"
        :page.sync="page"
        :search.sync="search"
        @search-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
        :table-loading="loading"
        @saveColumn="saveColumn"
        @resetColumn="resetColumn"
        :cell-style="cellStyle"
        @selection-change="selectionChange"
        @search-criteria-switch="searchCriteriaSwitch"
      >
        <template slot="createTimeSearch">
          <el-date-picker
            v-model="search.createTime"
            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="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="newAdd('new')"
            >创建单据
          </el-button>
          <el-button
            type="success"
            size="small"
            @click.stop="copyDoc()"
            :disabled="selectionList.length != 1"
            >复制单据</el-button
          >
          <el-button type="info" size="small">报表</el-button>
        </template>
        <template slot="corpIdSearch">
          <crop-select v-model="search.corpId" corpType="KH"></crop-select>
        </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="plannedDeliveryDateSearch">
          <el-date-picker
            v-model="search.plannedDeliveryDate"
            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="requiredDeliveryDateSearch">
          <el-date-picker
            v-model="search.requiredDeliveryDate"
            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-scope="scope" slot="corpId">
          <span
            style="color: #409EFF;cursor: pointer"
            @click.stop="editOpen(scope.row, 1)"
            >{{ scope.row.corpName }}
          </span>
        </template>
        <template slot-scope="scope" slot="orderNo">
          <span
            style="color: #409EFF;cursor: pointer"
            @click.stop="editOpen(scope.row, 1)"
            >{{ scope.row.orderNo }}
          </span>
        </template>
        <template slot-scope="scope" slot="createUser">
          {{ scope.row.createUserName }}
        </template>
        <template slot-scope="{ row }" slot="orderQuantity">
          <span>{{ row.orderQuantity | IntegerFormat }}</span>
        </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>
      </avue-crud>
    </basic-container>
    <detail-page
      @goBack="goBack"
      @copyOrder="copyOrder"
      :detailData="detailData"
      v-if="!show"
    ></detail-page>
  </div>
</template>

<script>
import option from "./config/mainList.json";
import { getList, remove, gainUser } from "@/api/exportTrade/purchaseContract";
import detailPage from "./detailsPage.vue";
import { defaultDate } from "@/util/date";
import { IntegerFormat } from "@/util/validate";
export default {
  name: "customerInformation",
  data() {
    return {
      search: {
        businesDate: defaultDate()
      },
      form: {},
      option: {},
      parentId: 0,
      dataList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 50, 100, 200, 300, 400, 500]
      },
      show: true,
      detailData: {},
      loading: false,
      selectionList: []
    };
  },
  components: { detailPage },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(2), option);
    this.getWorkDicts("payment_term").then(res => {
      this.findObject(this.option.column, "paymentType").dicData =
        res.data.data;
    });
    gainUser().then(res => {
      this.findObject(this.option.column, "createUser").dicData = res.data.data;
    });
    this.option.height = window.innerHeight - 210;
  },
  filters: {
    IntegerFormat(num) {
      return IntegerFormat(num);
    }
  },
  methods: {
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 139;
      } else {
        this.option.height = this.option.height + 139;
      }
      this.$refs.crud.getTableHeight();
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    //删除列表后面的删除按钮触发触发(row, index, done)
    rowDel(row, index, done) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        remove(row.id).then(res => {
          if (res.data.code == 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.onLoad(this.page, this.search);
          }
        });
      });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    copyDoc() {
      this.selectionList.forEach(e => {
        this.detailData = {
          id: e.id,
          status: "copy"
        };
        this.show = false;
      });
    },
    copyOrder(id) {
      this.show = true;
      this.detailData = {
        id: id,
        status: "copy"
      };
      this.$nextTick(() => {
        this.show = false;
      });
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.show = false;
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      if (params.businesDate) {
        params.businesStartDate = params.businesDate[0];
        params.businesEndDate = params.businesDate[1];
      }
      if (params.requiredDeliveryDate) {
        params.requiredDeliveryStartDate = params.requiredDeliveryDate[0];
        params.requiredDeliveryEndDate = params.requiredDeliveryDate[1];
      }
      delete params.businesDate;
      delete params.requiredDeliveryDate;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params) {
      if (this.search.businesDate && this.search.businesDate.length > 0) {
        params = {
          ...params,
          orderStartDate: this.search.businesDate[0],
          orderEndDate: this.search.businesDate[1]
        };
        delete params.businesDate;
      }
      this.loading = true;
      getList(page.currentPage, page.pageSize, params)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    newAdd(type) {
      this.detailData = {
        pageType: type
      };
      this.show = 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(2),
        this.option
      );
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = option;
      const inSave = await this.delColumnData(this.getColumnName(2), option);
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    }
  },
  watch: {
    option: function() {
      this.search.businesDate = defaultDate();
    }
  }
};
</script>

<style scoped>
::v-deep .select-component {
  display: flex;
}
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}
</style>
