<template>
  <div>
    <basic-container v-show="isShow" class="page-crad">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        :before-open="beforeOpen"
        :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"
        @selection-change="selectionChange"
        :cell-style="cellStyle"
        @search-criteria-switch="searchCriteriaSwitch"
      >
        <template slot="menuLeft">
          <el-button
            type="success"
            size="small"
            @click.stop="copyDoc()"
            :disabled="selectionList.length != 1"
            >复制新单</el-button
          >
        </template>
        <template slot-scope="{ row }" slot="updateUser">
          <span>{{ row.updateUserName }}</span>
        </template>
        <template slot-scope="{ row, index }" slot="menu">
          <el-button type="text" size="small" @click.stop="editOpen(row, 1)">
            查看
          </el-button>
          <!-- <el-button type="text" size="small" @click.stop="editOpen(row, 1)">
            编辑
          </el-button> -->
          <el-button
            type="text"
            size="small"
            @click.stop="rowDel(row, index)"
            :disabled="row.status > 0"
            v-if="permission.highSchool_edit"
          >
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <detailPage
      v-if="!isShow"
      ref="detail"
      @goBack="goBack"
      @copyOrder="copyOrder"
      :detailData="detailData"
    ></detailPage>
  </div>
</template>

<script>
import detailPage from "./detailsPage";
import { option } from "./js/optionList";
import {
  getList,
  remove,
  gainUser
} from "@/api/salaryManagement/primarySchool";
import { mapGetters } from "vuex";
export default {
  name: "index",
  components: {
    detailPage
  },
  data() {
    return {
      selectionList: [],
      form: {},
      dataList: [],
      loading: false,
      isShow: true,
      detailData: {},
      page: {
        pageSize: 10,
        currentPage: 1
      },
      option: {}
    };
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(112), option);
    this.getWorkDicts("month").then(res => {
      this.findObject(this.option.column, "moon").dicData = res.data.data;
      this.$refs.crud.init();
    });
    gainUser().then(res => {
      this.findObject(this.option.column, "createUser").dicData = res.data.data;
      this.$refs.crud.init();
    });
    this.option.height = window.innerHeight - 210;
  },
  activated() {
    if (this.$route.query.check) {
      this.isShow = true;
      setTimeout(() => {
        this.editOpen({ id: this.$route.query.check.billId }, 1);
      }, 100);
    }
  },
  methods: {
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    copyDoc() {
      this.selectionList.forEach(e => {
        this.detailData = {
          id: e.id,
          status: "copy"
        };
        this.isShow = false;
      });
    },
    copyOrder(id) {
      this.isShow = true;
      this.detailData = {
        id: id,
        status: "copy"
      };
      this.$nextTick(() => {
        this.isShow = false;
      });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.salaryType = "高中部";
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.search)
      )
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //新增跳转页面
    beforeOpen() {
      this.isShow = false;
    },
    editOpen(row, status) {
      this.detailData = {
        id: row.id,
        status: status
      };
      this.isShow = false;
    },
    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);
          }
        });
      });
    },
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(112),
        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(112), option);
      if (inSave) {
        this.$nextTick(() => {
          this.$refs.crud.doLayout();
        });
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    goBack() {
      if (this.$route.query.check) {
        this.$router.$avueRouter.closeTag(this.$route.fullPath);
        this.$router.push({
          path: "/salaryManagement/highSchool/index"
        });
      }
      this.detailData = this.$options.data().detailData;
      this.onLoad(this.page, this.search);
      this.isShow = true;
    }
  },
  computed: {
    ...mapGetters(["permission"])
  }
};
</script>

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