<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"
        :table-loading="loading"
        :cell-style="cellStyle"
        @selection-change="selectionChange"
        @search-change="searchChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
        @search-criteria-switch="searchCriteriaSwitch"
        @saveColumn="saveColumn"
        @resetColumn="resetColumn"
      >
        <template slot="cnameSearch">
          <el-select
            v-model="search.goodsId"
            filterable
            clearable
            remote
            :remote-method="cnameMethod"
            @clear="cnameClear"
          >
            <el-option
              v-for="(item, index) in goodsOption"
              :label="item.cname"
              :value="item.id"
              :key="index"
            ></el-option>
          </el-select>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import option from "./config/mainList.json";
import {getList as goodsSelect} from "@/api/basicData/commodityType";
import {getStockList} from "@/api/officeSupplies/stock";

export default {
  name: "index",
  data() {
    return {
      option: {},
      dataList: [],
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 50, 100, 200, 300, 400, 500]
      },
      search: {},
      show: true,
      loading: false,
      selection: [],
      goodsOption: [],
    }
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(105), option);
    goodsSelect(1, 10).then(res => {
      this.goodsOption = res.data.data.records;
    })
    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: {
    searchCriteriaSwitch(type) {
      if (type){
        this.option.height =  this.option.height - 90
      }else {
        this.option.height =  this.option.height + 90
      }
      this.$refs.crud.getTableHeight()
    },
    newAdd() {
      this.show = false;
    },
    onLoad(page, params) {
      // 重置掉展开
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false)
      })
      let queryParams = Object.assign({}, params, {
        size: page.pageSize,
        current: page.currentPage,
        tradeType: 'BGYP',
      })
      if (queryParams.inDate && queryParams.inDate.length > 0) {
        queryParams = {
          ...queryParams,
          beginInDate: queryParams.inDate[0],
          endInDate: queryParams.inDate[1],
        }
      }
      delete queryParams.inDate;
      this.loading = true;
      getStockList(queryParams).then(res => {
        this.dataList = res.data.data.records;
        this.page.total = res.data.data.total;
        this.option.height = window.innerHeight - 240;
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
      }).finally(() => {
        this.loading = false;
      })
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(105),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
      }
    },
    async resetColumn() {
      this.option = option;
      const inSave = await this.delColumnData(this.getColumnName(105), option);
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout()
        })
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.onLoad(this.page, params);
      done();
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    copyDoc() {},
    selectionChange(list) {
      this.selection = list;
    },
    cnameMethod(name) {
      if (name) {
        goodsSelect(1, 20, {cname: name}).then(res => {
          this.goodsOption = res.data.data.records;
        })
      } else {
        goodsSelect(1, 10).then(res => {
          this.goodsOption = res.data.data.records;
        })
      }
    },
    cnameClear() {
      goodsSelect(1, 10).then(res => {
        this.goodsOption = res.data.data.records;
      })
    },
  },
}
</script>

<style scoped>

</style>
