<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="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="newAdd()"
          >创建单据</el-button>
          <el-button
            type="success"
            size="small"
            icon="el-icon-plus"
            @click.stop="copyDoc()"
            :disabled="selection.length != 1"
            v-if="false"
          >复制单据</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)"
            :disabled="scope.row.status == 3"
          >删除
          </el-button>
        </template>
        <template slot="applyUserNameSearch">
          <el-select
            v-model="search.applyUser"
            filterable
            clearable
            size="small"
          >
            <el-option
              v-for="(item,index) in userList"
              :key="index"
              :label="item.realName"
              :value="item.id"
            ></el-option>
          </el-select>
        </template>
        <template slot="applyUserName" slot-scope="scope">
          <span style="color: #409EFF;cursor: pointer" @click.stop="beforeOpenPage(scope.row,scope.index)">{{ scope.row.applyUserName }}</span>
        </template>
      </avue-crud>
    </basic-container>
    <detail-page
      @goBack="goBack"
      @copyOrder="copyOrder"
      :detailData="detailData"
      v-if="!show"
    />
  </div>
</template>

<script>
import option from "./config/mainList.json";
import detailPage from "./detail";
import { gainUser } from "@/api/basicData/customerInquiry";
import {getList, deleteList} from "@/api/officeSupplies/requisition";

export default {
  name: "index",
  components: {
    detailPage,
  },
  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: [],
      detailData: {},
      userList: [],
    }
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(94), option);
    let i = 0;
    this.option.column.forEach(item => {
      if (item.search) i++
    })
    gainUser().then(res => {
      this.userList = res.data.data;
    });
    if (i % 3 !== 0){
      const num = 3 - Number(i % 3)
      this.option.searchMenuSpan = num * 8;
      this.option.searchMenuPosition = "right";
    }
  },
  activated() {
    if (this.$route.query.check) {
      this.show = true
      this.detailData = {
        id: this.$route.query.check.billId,
        check: this.$route.query.check,
        auditId: this.$route.query.check.id,
      };
      this.show = false;
      this.$store.commit("OFFICELY_IN_DETAIL");
    }
    if (this.$route.query.params) {
      this.show = true;
      this.detailData = {
        id: this.$route.query.params,
      };
      this.show = false;
      this.$store.commit("OFFICELY_IN_DETAIL");
    }
  },
  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;
      this.$store.commit("OFFICELY_IN_DETAIL");
    },
    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: 'OLY',
      })
      if (queryParams.applyTime && queryParams.applyTime.length > 0) {
        queryParams = {
          ...queryParams,
          beginApplyTime: queryParams.applyTime[0] + ' 00:00:00',
          endApplyTime: queryParams.applyTime[1] + ' 23:59:59',
        }
      }
      delete queryParams.applyTime;
      this.loading = true;
      getList(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(94),
        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(94), 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;
    },
    goBack() {
      if (this.$route.query.check || this.$route.query.params) {
        this.$router.$avueRouter.closeTag(this.$route.fullPath);
        this.$router.push({
          path: "/workManagement/requisition/index"
        });
      }
      this.detailData = this.$options.data().detailData;
      this.show = true;
      this.onLoad(this.page, this.search);
    },
    copyOrder(id) {
      this.show = true;
      this.detailData = {
        id: id,
        status: "copy"
      };
      this.$nextTick(() => {
        this.show = false;
      });
      this.$store.commit("OFFICELY_IN_DETAIL");
    },
    //删除列表后面的删除按钮触发触发(row, index, done)
    rowDel(row, index, done) {
      this.$confirm("确定删除数据?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(res => {
        return deleteList(row.id)
      }).then(() => {
        this.dataList.splice(row.$index, 1)
        this.$message({
          type: "success",
          message: "删除成功!"
        });
        this.page.currentPage = 1;
        this.onLoad(this.page)
      })
    },
    // 详情打开
    beforeOpenPage(row, index) {
      this.show = false;
      this.detailData = {
        id: row.id,
        query: true, // 表示只是查询
      };
      this.$store.commit("OFFICELY_IN_DETAIL");
    },
  },
}
</script>

<style scoped>

</style>
