<template>
  <basic-container>
    <avue-crud :option="option" :data="dataList" ref="crud" v-model="form" :page.sync="page" :search.sync="search"
      :table-loading="loading" :cell-style="cellStyle" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @saveColumn="saveColumn" @resetColumn="resetColumn"
      @search-criteria-switch="searchCriteriaSwitch" @on-load="onLoad">
      <template slot="corpIdCSearch">
        <crop-select v-model="search.corpIdC" corpType="GYS"></crop-select>
      </template>
      <template slot-scope="{ row }"  slot="corpIdC">
        <span>
          {{row.corpNameC}}
        </span>
      </template>
      <template slot="corpIdXSearch">
        <crop-select v-model="search.corpIdX" corpType="KH"></crop-select>
      </template>
      <template slot-scope="{ row }"  slot="corpIdX">
        <span>
          {{row.corpNameX}}
        </span>
      </template>
      <template slot="itemIdCSearch">
        <el-select v-model="search.itemIdC" placeholder="请选择" size="small">
          <el-option v-for="item in goodsoptions" :key="item.id" :label="item.cname" :value="item.id">
          </el-option>
        </el-select>
      </template>
      <template slot-scope="{ row }"  slot="itemIdC">
        <span>
          {{row.itemNameC}}
        </span>
      </template>
      <template slot-scope="{ row }"  slot="belongToCorpIdC">
        <span>
          {{row.belongToCorpNameC}}
        </span>
      </template>
      <template slot="belongToCorpIdCSearch">
        <crop-select v-model="search.belongToCorpIdC" corpType="GS"></crop-select>
      </template>
      <template slot="requiredArrivalDateCSearch">
        <el-date-picker v-model="search.requiredArrivalDateC" 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="belongToCorpIdXSearch">
        <crop-select v-model="search.belongToCorpIdX" corpType="GS"></crop-select>
      </template>
      <template slot="plannedDeliveryDateXSearch">
        <el-date-picker v-model="search.plannedDeliveryDateX" 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="salesNameSearch">
        <user-com v-model="search.salesName"></user-com>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import option from "./config/mainList.json";
import { getList, getGoods } from "@/api/basicData/rubberStock"
import { roundNumbers } from "@/util/validate";

export default {
  data() {
    return {
      loading: false,
      form: {},
      search: {},
      show: true,
      detailData: {},
      option: {},
      parentId: 0,
      dataList: [],
      page: {
        currentPage: 1,
        total: 0,
        pageSize: 10,
        pageSizes: [10, 50, 100, 200, 300, 400, 500, 1000]
      },
      query: {},
      goodsoptions: [],
    }
  },
  filters: {
    roundNumbers(val) {
      return roundNumbers(val);
    }
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(187), option);
    getGoods(1, 10).then(res => {
      if (res.data.data.total > 0) {
        this.goodsoptions = res.data.data.records;
        if (Math.ceil(res.data.data.total / 10) > 1) {
          for (let i = 2; i <= Math.ceil(res.data.data.total / 10); i++) {
            getGoods(i, 10).then(e => {
              this.goodsoptions = this.goodsoptions.concat(e.data.data.records);
            });
          }
        }
      }
    });
  },
  mounted() {
  },
  methods: {
    //新单打开
    addReceipt(row) {

    },
    //编辑打开
    editOpen(row, status) {

    },
    rowDel(row, index, done) {
      if (row.id) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {

        });
      }
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done()
    },
    searchReset() {
    },
    selectionChange() {
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      if (this.search.requiredArrivalDateC && this.search.requiredArrivalDateC.length > 0) {
        params = {
          ...params,
          requiredArrivalDateStartC: this.search.requiredArrivalDateC[0],
          requiredArrivalDateEndC: this.search.requiredArrivalDateC[1]
        };
      }
      if (this.search.plannedDeliveryDateX && this.search.plannedDeliveryDateX.length > 0) {
        params = {
          ...params,
          plannedDeliveryDateStartX: this.search.plannedDeliveryDateX[0],
          plannedDeliveryDateEndX: this.search.plannedDeliveryDateX[1]
        };
      }
      let data = this.deepClone(Object.assign(params));
      delete data.requiredArrivalDateC;
      delete data.plannedDeliveryDateX;
      // delete data.createTime;
      this.loading = true;
      getList(page.currentPage, page.pageSize, data)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 138
      } else {
        this.option.height = this.option.height + 138
      }
      this.$refs.crud.getTableHeight()
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(187),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      const inSave = await this.delColumnData(
        this.getColumnName(187),
        option
      );
      if (inSave) {
        this.$message.success("重置成功");
        this.option = option;
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
  }
}
</script>

<style scoped>

</style>
