<template>
  <div class="card-rule">
    <div class="part-box part-box-filter part-box-flex">
      <el-form ref="FilterForm" label-position="left" label-width="85px" inline>
        <el-form-item label="规则名称：">
          <el-input
            style="width: 200px;"
            v-model.trim="filter.name"
            placeholder="请输入内容"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="创建时间：">
          <el-date-picker
            v-model="filter.createTime"
            type="date"
            value-format="timestamp"
            placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="启用/禁用：" label-width="90px">
          <el-select
            v-model="filter.enable"
            style="width: 100px;"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="(val, key) in ABLE_TYPE"
              :key="key"
              :value="key"
              :label="val"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="icon icon-search" @click="toPage(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
      <div class="part-box-action">
        <el-button type="warning" icon="icon icon-plus" @click="toAdd"
          >添加</el-button
        >
      </div>
    </div>
    <div class="part-box">
      <el-table ref="TableList" :data="rules" border stripe>
        <el-table-column
          type="index"
          label="序号"
          width="70"
          align="center"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column prop="name" label="题卡规则名称"></el-table-column>
        <el-table-column prop="orgs" label="适用学院">
          <template slot-scope="scope">
            {{ scope.row.orgs | orgsFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column prop="enable" label="启用/禁用">
          <template slot-scope="scope">
            {{ scope.row.enable | enableFilter }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <span slot-scope="scope">{{
            scope.row.createTime | timestampFilter
          }}</span>
        </el-table-column>
        <el-table-column
          class-name="action-column"
          label="操作"
          align="center"
          width="120px"
        >
          <template slot-scope="scope">
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-edit"
              @click="toEdit(scope.row)"
              title="编辑"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              icon="icon icon-circle-right"
              @click="toDetail(scope.row)"
              title="查看"
            ></el-button>
            <el-button
              class="btn-table-icon"
              type="text"
              :icon="
                scope.row.enable
                  ? 'icon icon-circle-stop'
                  : 'icon icon-circle-caret-right'
              "
              @click="toEnable(scope.row)"
              :title="scope.row.enable ? '禁用' : '启用'"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="part-page">
        <el-pagination
          background
          layout="total,prev, pager, next"
          :current-page="current"
          :total="total"
          :page-size="size"
          @current-change="toPage"
        >
        </el-pagination>
      </div>
    </div>
    <!-- ModifyCardRule -->
    <modify-card-rule
      ref="ModifyCardRule"
      :instance="curRule"
      :editable="ruleCanEdit"
      @modified="getList"
    ></modify-card-rule>
  </div>
</template>

<script>
import { ABLE_TYPE } from "@/constants/enumerate";
import { commonRuleDetail, cardRuleListPage, ableCardRule } from "../api";
import ModifyCardRule from "../components/ModifyCardRule";

export default {
  name: "card-rule",
  components: {
    ModifyCardRule
  },
  data() {
    return {
      filter: {
        enable: null,
        name: "",
        createTime: null
      },
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      rules: [],
      curRule: {},
      ruleCanEdit: true,
      commonRule: {},
      ABLE_TYPE
    };
  },
  created() {
    this.getList();
    this.getCommonRuleDetail();
  },
  methods: {
    async getCommonRuleDetail() {
      this.commonRule = await commonRuleDetail();
    },
    async getList() {
      const datas = {
        ...this.filter,
        pageNumber: this.current,
        pageSize: this.size
      };
      datas.enable = !!datas.enable;
      const data = await cardRuleListPage(datas);
      this.rules = data.records;
      this.total = data.total;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toAdd() {
      this.curRule = {
        requiredFields: this.commonRule.requiredFields.filter(
          item => item.enable
        ),
        extendFields: this.commonRule.extendFields
          .filter(item => item.enable)
          .map(item => {
            item.enable = false;
            return item;
          })
      };
      this.ruleCanEdit = true;
      this.$refs.ModifyCardRule.open();
    },
    toEdit(row) {
      this.curRule = row;
      this.ruleCanEdit = true;
      this.$refs.ModifyCardRule.open();
    },
    toDetail(row) {
      this.curRule = row;
      this.ruleCanEdit = false;
      this.$refs.ModifyCardRule.open();
    },
    async toEnable(row) {
      const enable = !row.enable;
      await ableCardRule({
        id: row.id,
        enable
      });
      row.enable = enable;
    }
  }
};
</script>
