<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      ref="crud"
      v-model="form"
      :page="page"
      :permission="permissionList"
      @row-del="rowDel"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @selection-change="selectionChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <!--分类选择搜索栏-->
      <template slot="categoryOneSearch">
        <el-select
          v-model="query.categoryOne"
          placeholder="所属一级分类"
          @change="selectFirstTypeChange"
        >
          <el-option
            v-for="(item, index) in firstTyepSearchList"
            :key="index"
            :label="item.name"
            :value="item.id"
            >{{ item.name }}</el-option
          >
        </el-select>
      </template>
      <template slot="categoryTwoSearch">
        <el-select
          v-model="query.categoryTwo"
          placeholder="所属二级分类"
          @change="selectSecondTypeChange"
        >
          <el-option
            v-for="(item, index) in secondTyepSearchList"
            :key="index"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </template>
      <!--分类选择搜索栏-->

      <!--日期选择搜索栏-->
      <template slot="createTimeSearch">
        <el-date-picker
          v-model="query.createTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          @change="dateChange"
        >
        </el-date-picker>
      </template>

      <!--日期选择搜索栏-->

      <template slot="menuLeft">
        <el-button
          v-if="permission.manage_rules_rulesList_addRule"
          type="primary"
          @click="handleAddRule(null, 'add')"
          >新增制度</el-button
        >
        <el-button type="danger" @click="mutiDel">批量删除</el-button>
        <el-button type="success" @click="mutiShowOrHidRule('show')"
          >批量显示</el-button
        >
        <el-button type="info" @click="mutiShowOrHidRule('hide')"
          >批量隐藏</el-button
        >
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button
          v-if="row.status == 0 && permission.manage_rules_rulesList_hideRule"
          type="text"
          @click="hideOrShowRule(row.id, 'hide')"
          size="small"
          >隐藏</el-button
        >
        <el-button
          v-if="row.status==1 && permission.manage_rules_rulesList_showRule"
          type="text"
          @click="hideOrShowRule(row.id, 'show')"
          size="small"
          >显示</el-button
        >

        <el-button
          v-if="permission.manage_rules_rulesList_editRule"
          type="text"
          @click="handleAddRule(row, 'edit')"
          size="small"
          >查看</el-button
        >
      </template>
    </avue-crud>
    <add-edit-rule ref="addOrEditRule" @success="refreshChange"></add-edit-rule>
  </basic-container>
</template>

<script>
import {
  getRulesList,
  delRules,
  hideOrShowRules,
  ruleGetTypeList,
} from "@/api/manage/rules";
import addEditRule from "./Dialog/addEditRule";
import { mapGetters } from "vuex";

export default {
  components: {
    addEditRule,
  },
  data() {
    return {
      edit: {
        id: "",
        visible: false,
      },
      selectionList: [],
      addRuleDialogVisible: false,
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      data: [],
      fileList: [],
      form: {
        firstId: "",
      },
      query: {},
      firstTyepSearchList: [],
      secondTyepSearchList: [],
      option: {
        height: "auto",
        align: "center",
        menuAlign: "center",
        tip: false,
        addBtn: false,
        addBtnText: "新增制度",
        delBtn: true,
        editBtn: false,
        viewBtn: false,
        excelBtn: false,
        columnBtn: false,
        searchBtn: true,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        indexLabel: "序号",
        selection: true,
        dialogClickModal: false,
        dialogType: "drawer",
        column: [
          {
            label: "制度名称",
            prop: "docName",
            searchSpan: 6,
            search: true,
            display: false,
            rules: [
              {
                required: true,
                whitespace: true,
                message: "制度名称必须填写",
              },
              {
                max: 30,
                message: "长度在30个字以内",
              },
            ],
            row: true,
          },
          {
            type: "select",
            label: "所属一级分类",
            prop: "categoryOne",
            display: false,
            searchSpan: 8,
            search: true,
            searchLabelWidth: 110,
            searchslot: true,
          },
          {
            type: "select",
            label: "所属二级分类",
            prop: "categoryTwo",
            display: false,
            searchSpan: 8,
            search: true,
            searchslot: true,
            searchLabelWidth: 110,
          },
          {
            label: "发布时间",
            prop: "createTime",
            type: "datatime",
            formate: "yyyy-mm-dd hh:mm",
            searchslot: true,

            searchSpan: 8,
            search: true,
            display: false,
          },
          {
            label: "格式",
            prop: "typeStr",
            display: false,
          },
          {
            label: "状态",
            prop: "status",
            type: "select",
            dicData: [
              {
                label: "显示",
                value: 0,
              },
              {
                label: "隐藏",
                value: 1,
              },
            ],
            display: false,
            searchSpan: 6,
            search: true,
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        delBtn: this.vaildData(this.permission.manage_rules_rulesList_delRule, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids;
    },
  },
  activated() {
    this.$refs.crud &&
      this.$refs.crud.$refs.table &&
      this.$refs.crud.$refs.table.doLayout();
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      if (this.query.createTime) {
        this.query.beginTime = this.query.createTime[0];
        this.query.endTime = this.query.createTime[1];
        delete this.query.createTime;
      }
      if (this.query.categoryOne) {
        this.query.firstId = this.query.categoryOne;
      }
      if (this.query.categoryTwo) {
        this.query.secondId = this.query.categoryTwo;
      }
      Object.assign(this.query, params);
      this.page.currentPage = 1;
      this.onLoad(this.page, this.query);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    /*刷新本页 带搜索参数*/
    refreshChange() {
      this.onLoad(this.page);
    },

    /**批量删除 */
    mutiDel() {
      if (this.selectionList.length == 0) {
        this.$message.closeAll();
        this.$message.error("至少选择一条数据");
      } else {
        this.$confirm("是否确定批量删除制度?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          delRules(this.ids, 1)
            .then(() => {
              this.$message.success("批量删除成功");
              this.onLoad(this.page);
            })
            .catch((err) => {
              this.$message.error(err);
            });
        });
      }
    },

    hideOrShowRule(id, type) {
      let ids = [];
      ids.push(id);
      let tips = type == "show" ? "显示" : "隐藏";
      type = type == "show" ? 0 : 1;
      this.$confirm(`是否确定${tips}该制度?`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        hideOrShowRules(ids, type)
          .then(() => {
            this.$message.success(`${tips}成功`);
            this.onLoad(this.page);
          })
          .catch((err) => {
            this.$message.error(err);
          });
      });
    },
    mutiShowOrHidRule(type) {
      let tips = type == "show" ? "显示" : "隐藏";
      type = type == "show" ? 0 : 1;

      if (this.selectionList.length == 0) {
        this.$message.closeAll();
        this.$message.error("至少选择一条数据");
      } else {
        this.$confirm(`是否确定批量${tips}制度?`, {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          hideOrShowRules(this.ids, type)
            .then(() => {
              this.$message.success("操作成功");
              this.onLoad(this.page);
            })
            .catch((err) => {
              this.$message.error(err);
            });
        });
      }
    },
    /**删除制度 */
    rowDel(row, done, loading) {
      let ids = [];
      ids.push(row.id);
      this.$confirm("是否确定删除该制度?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delRules(ids, 1)
          .then(() => {
            this.$message.success("删除成功");
            this.onLoad(this.page);
            done();
          })
          .catch((err) => {
            this.$message.error(err);
            loading();
          });
      });
    },

    handleAddRule(data, type) {
      this.$refs.addOrEditRule.open(data, type);
    },
    getRuleTypeSelectItems(val) {
      if (val == 0) {
        ruleGetTypeList(val).then((res) => {
          this.firstTyepSearchList = res.data.data;
        });
      } else {
        ruleGetTypeList(val).then((res) => {
          this.secondTyepSearchList = res.data.data;
        });
      }
    },
    selectFirstTypeChange(val) {
      this.query.categoryOne = val;
      this.getRuleTypeSelectItems(val);
    },
    selectSecondTypeChange(val) {
      this.query.categoryTwo = val;
    },
    onLoad(page, params = {}) {
      getRulesList(
        page.currentPage,
        page.pageSize,
        Object.assign(this.query, params)
      ).then((res) => {
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
        if (this.secondTyepSearchList.length == 0) {
          this.getRuleTypeSelectItems(0);
        }
      });
    },
  },
};
</script>
<style scoped>
.insure-select .el-tag.el-tag--info {
  background-color: #fdf6ec;
  border-color: #faecd8;
  color: #e6a23c;
}
.insure-select .el-tag.el-tag--info .el-tag__close {
  color: #e6a23c;
}
.insure-select .el-tag__close.el-icon-close {
  background-color: #fdf6ec;
}
.el-form-item {
  margin-bottom: 18px;
  padding: 0 10px;
}
.searchBtn {
  display: inline-block;
  margin-bottom: 18px;
}
.el-scrollbar__view .el-select-dropdown__list {
  text-align: center !important;
}
</style>
