<template>
  <div class="app-container">
    <!-- 搜索插件 -->
    <SearchForm v-show="showSearch" ref="SearchForm" @search="_getTableList" />

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddAndUpdate(undefined)" v-hasPermi="['sch:rules:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="_getTableList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableList">
      <el-table-column type="index" width="55" align="center" />
      <el-table-column label="制度名" align="center" prop="ruleName" />
      <el-table-column label="实施时间" align="center" prop="implementTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.implementTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到期时间" align="center" prop="dueTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dueTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制度内容" align="center" prop="ruleContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleAddAndUpdate(scope.row)" v-hasPermi="['sch:rules:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['sch:rules:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="searchForm.pageNum" :limit.sync="searchForm.pageSize" @pagination="_getTableList" />

    <!-- 添加或修改规章制度对话框 -->
    <RuleDialogForm v-if="dialogVisible" ref="ruleDialogForm" :dialog-visible="dialogVisible" @refreshDataList="_getTableList" />

  </div>
</template>

<script>
import { listRules, getRules, delRules } from "@/api/sch/rules";
import SearchForm from "./components/SearchForm.vue";
import RuleDialogForm from "./components/RuleDialogForm.vue";

export default {
  name: "Rules",
  components: {
    SearchForm, RuleDialogForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 规章制度表格数据
      tableList: [],
      // 查询参数
      searchForm: {
        pageNum: 1,
        pageSize: 10
      },
      dialogVisible: false
    };
  },
  created() {
    this._getTableList();
  },
  methods: {
    /** 查询规章制度列表 */
    _getTableList() {
      this.loading = true;
      const tempForm = this.$refs.SearchForm?.searchForm || {};
      const params = { ...this.searchForm, ...tempForm };
      listRules(params).then(response => {
        this.tableList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 新增按钮操作 */
    handleAddAndUpdate(info) {
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.ruleDialogForm.init(info)
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ruleIds = row.ruleId || this.ids;
      this.$modal.confirm('是否确认删除规章制度名为"' + row.ruleName + '"的数据项？').then(function () {
        return delRules(ruleIds);
      }).then(() => {
        this._getTableList();
        this.$message.success('删除成功');
      }).catch(() => { });
    },
  }
};
</script>
