<template>
  <div>
    <basic-container v-if="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"
        :cell-style="cellStyle"
      >
        <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, 2)">
            编辑
          </el-button>
          <el-button type="text" size="small" @click.stop="rowDel(row, index)">
            删除
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
    <detailPage
      v-else
      ref="detail"
      @goBack="goBack"
      :detailData="detailData"
    ></detailPage>
  </div>
</template>

<script>
import detailPage from "./detailsPage";
import { getList, remove } from "@/api/salaryManagement/primarySchool";
export default {
  name: "index",
  components: {
    detailPage
  },
  data() {
    return {
      form: {},
      dataList: [],
      loading: false,
      isShow: true,
      detailData: {},
      page: {
        pageSize: 10,
        currentPage: 1
      },
      option: {
        searchShow: true,
        searchMenuSpan: 16,
        align: "center",
        searchSpan: 8,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        menuWidth: 120,
        searchIcon: true,
        searchIndex: 2,
        addBtnText: "新单",
        column: [
          {
            label: "年",
            prop: "annual",
            type: "year",
            valueFormat: "yyyy",
            overHidden: true,
            width: 100,
            search: true
          },
          {
            label: "月",
            prop: "moon",
            type: "select",
            filterable: true,
            dicUrl: "/api/blade-system/dict-biz/dictionary?code=month",
            props: {
              label: "dictValue",
              value: "dictKey"
            },
            overHidden: true,
            width: 100,
            search: true
          },
          {
            label: "制单人",
            prop: "createUser",
            type: "select",
            filterable: true,
            dicUrl: "/api/blade-user/client/gainUser",
            props: {
              label: "name",
              value: "id"
            },
            overHidden: true,
            width: 100,
            search: true
          },
          {
            label: "制单日期",
            prop: "createTime",
            type: "date",
            overHidden: true,
            width: 200
          },
          {
            label: "更新人",
            prop: "updateUser",
            overHidden: true,
            width: 100
          },
          {
            label: "更新日期",
            prop: "updateTime",
            type: "date",
            overHidden: true,
            width: 200
          },
          {
            label: "备注",
            prop: "remarks",
            overHidden: true,
            search: true
          }
        ]
      }
    };
  },
  methods: {
    cellStyle() {
      return "padding:0;height:40px;";
    },
    //点击搜索按钮触发
    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;
      getList(page.currentPage, page.pageSize, params)
        .then(res => {
          this.dataList = res.data.data.records ? res.data.data.records : [];
          this.page.total = res.data.data.total;
          if (this.page.total) {
            this.option.height = window.innerHeight - 260;
          }
        })
        .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);
          }
        });
      });
    },
    goBack() {
      this.detailData = this.$options.data().detailData;
      this.isShow = true;
    }
  }
};
</script>

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