<template>
  <div>
    <basic-container v-show="show" class="page-crad">
      <avue-crud
        ref="crud"
        :option="option"
        :data="dataList"
        :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"
        :cell-style="cellStyle"
        @expand-change="expandChange"
        @selection-change="selectionChange"
        @search-criteria-switch="searchCriteriaSwitch"
      >
        <template slot-scope="{ row }" slot="expand">
          <avue-crud
            :data="row.itemData"
            :option="itemOption"
            :table-loading="row.itemLoading"
            :cell-style="cellStyle"
            class="itemTable"
          ></avue-crud>
        </template>
        <template slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="small"
            @click.stop="newAdd()"
            >新增
          </el-button>
          <el-button
            type="success"
            size="small"
            @click.stop="copyDoc()"
            :disabled="selectionList.length != 1"
            >复制新单</el-button
          >
        </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, 2)">
            编辑
          </el-button>
          <el-button type="text" size="small" @click.stop="rowDel(row, index)">
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <details-page
      v-if="!show"
      @goBack="backToList"
      @copyOrder="copyOrder"
      :detailData="detailData"
    />
  </div>
</template>

<script>
import detailsPage from "./detailsPage";
import {
  getList,
  remove,
  detailItem
} from "@/api/basicData/salaryConfiguration";

export default {
  name: "index",
  data() {
    return {
      show: true,
      loading: false,
      form: {},
      search: {},
      detailData: {},
      dataList: [],
      selectionList: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        pageSizes: [10, 50, 100, 200, 300, 400, 500]
      },
      option: {
        searchShow: true,
        searchMenuSpan: 24,
        align: "center",
        searchSpan: 8,
        tip: false,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        menuWidth: 140,
        dialogClickModal: false,
        searchLabelWidth: 100,
        searchIcon: true,
        searchIndex: 2,
        selection: true,
        stripe: true,
        expand: true,
        expandWidth: 38,
        column: [
          {
            label: "部门",
            prop: "inSection",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=in_section",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            search: true,
            overHidden: true,
            span: 8
          },
          {
            label: "教师类别",
            prop: "salaryWithdrawalStandardName",
            type: "select",
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=Salary_allocation_standard",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            search: true,
            overHidden: true,
            span: 8
          },
          {
            label: "标准类别",
            prop: "normType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=norm_type",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            search: true,
            overHidden: true,
            span: 8
          },
          {
            label: "分类",
            prop: "resultType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=result_type",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            overHidden: true,
            search: true
          },
          {
            label: "职位",
            prop: "compileCategory",
            type: "select",
            dicUrl:
              "/api/blade-system/dict-biz/dictionary?code=compile_category",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            search: true,
            span: 8
          },
          {
            label: "授课类别",
            prop: "classType",
            type: "select",
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=class_type",
            props: {
              label: "dictValue",
              value: "dictValue"
            },
            filterable: true,
            width: "100",
            search: true,
            span: 8
          },
          {
            label: "创建人",
            prop: "createUserName",
            width: "100",
            overHidden: true
          },
          {
            label: "创建时间",
            prop: "createTime",
            width: "150",
            overHidden: true
          },
          {
            label: "备注",
            prop: "remarks",
            overHidden: true
          }
        ]
      },
      itemOption: {
        align: "center",
        header: false,
        menu: false,
        column: [
          {
            label: "分类明细",
            prop: "parameter",
            overHidden: true
          },
          {
            label: "工资标准",
            prop: "salary",
            overHidden: true
          },
          {
            label: "备注",
            prop: "remarks",
            overHidden: true
          }
        ]
      }
    };
  },
  components: {
    detailsPage
  },
  created() {
    this.option.height = window.innerHeight - 210;
  },
  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;";
    },
    expandChange(row) {
      if (!row.itemData) {
        detailItem(row.id)
          .then(res => {
            this.dataList[row.$index].itemData = res.data.data.records;
          })
          .finally(() => {
            this.dataList[row.$index].itemLoading = false;
          });
      }
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    newAdd() {
      this.show = false;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      this.dataList.forEach(item => {
        this.$refs.crud.toggleRowExpansion(item, false);
      });
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.search)
      )
        .then(res => {
          if (res.data.data.records) {
            res.data.data.records.forEach(e => {
              e.itemLoading = true;
            });
          }
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    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;
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    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);
          }
        });
      });
    },
    //返回列表
    backToList() {
      this.detailData = this.$options.data().detailData;
      this.show = true;
      this.onLoad(this.page, this.search);
    }
  }
};
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}
::v-deep .el-table__expanded-cell[class*="cell"] {
  padding: 0px;
}
.itemTable ::v-deep .el-table {
  width: 738px;
}
</style>
