<template>
  <basic-container>
    <avue-crud
      :option="option"
      :data="dataList"
      ref="crud"
      v-model="form"
      :page.sync="page"
      :search.sync="search"
      :table-loading="loading"
      :before-open="beforeOpen"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @saveColumn="saveColumn"
      @resetColumn="resetColumn"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="moduleName">
        <span>{{ scope.row.moduleName | moduleNameFormat(moduleOption) }}</span>
      </template>
      <template slot-scope="scope" slot="isEnable">
        <span>{{ scope.row.isEnable == "1" ? "是" : "否" }}</span>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
import option from "./config/mainList.json";
import {
  lockConfigList,
  lockConfigSave,
  lockConfigUpdate,
  lockConfigDetail
} from "@/api/lock/lock";
import { customerList } from "@/api/basicData/inventoryAccount";

export default {
  data() {
    return {
      loading: false,
      form: {},
      search: {},
      show: true,
      detailData: {},
      option: option,
      parentId: 0,
      dataList: [],
      page: {
        currentPage: 1,
        total: 0,
        pageSize: 10,
        pageSizes: [10, 50, 100, 200, 300, 400, 500, 1000]
      },
      query: {},
      moduleOption: [
        {
          label: "销售",
          value: "xs"
        },
        {
          label: "采购",
          value: "cg"
        },
        {
          label: "发货",
          value: "fh"
        },
        {
          label: "收货",
          value: "sh"
        },
        {
          label: "收费",
          value: "sf"
        },
        {
          label: "付费",
          value: "ff"
        },
        {
          label: "进项",
          value: "jx"
        },
        {
          label: "销项",
          value: "xx"
        },
        {
          label: "小学部",
          value: "xxb"
        },
        {
          label: "初中部",
          value: "czb"
        },
        {
          label: "高中部",
          value: "gzb"
        },
        {
          label: "后勤部",
          value: "hqb"
        },
        {
          label: "幼儿园",
          value: "yry"
        },
        {
          label: "幼儿园后勤",
          value: "yryhq"
        }
      ]
    };
  },
  async created() {
    // this.option = await this.getColumnData(this.getColumnName(65), option);
  },
  filters: {
    moduleNameFormat(row, moduleOption) {
      let name;
      moduleOption.map(e => {
        if (row == e.value) {
          name = e.label;
        }
      });
      return name;
    }
  },
  mounted() {},
  methods: {
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        lockConfigDetail(this.form.id).then(res => {
          res.data.data.isEnable = res.data.data.isEnable.toString();
          this.form = res.data.data;
        });
      }
      done();
    },
    rowSave(row, done, loading) {
      lockConfigSave(row).then(() => {
        this.onLoad(this.page, this.search);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      });
    },
    rowUpdate(row, index, done, loading) {
      lockConfigUpdate(row).then(() => {
        this.onLoad(this.page, this.search);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      });
    },
    rowDel(row, index, done) {
      if (row.id) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {});
      }
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    searchReset() {
      console.log("1");
    },
    selectionChange() {
      console.log("1");
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    currentChange(val) {
      this.page.currentPage = val;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page);
    },
    paramsAdjustment(params) {
      params = Object.assign({}, this.search);
      if (!params.tenantId) {
        params.tenantId = "000000";
      }
      return params;
    },

    onLoad(page, params) {
      this.loading = true;
      params = this.paramsAdjustment(params);

      lockConfigList(page.currentPage, page.pageSize, params).then(res => {
        this.dataList = res.data.data.records;
        this.page.total = res.data.data.total;
        this.loading = false;
      });
    },
    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(
        this.getColumnName(65),
        this.option
      );
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      const inSave = await this.delColumnData(this.getColumnName(65), option);
      if (inSave) {
        this.$message.success("重置成功");
        this.option = option;
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    }
  }
};
</script>

<style scoped></style>
