<template>
  <div class="page_info">
    <div class="search_pannel">
      <el-form
        ref="dataForm"
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item prop="dictName" label="字典名称">
          <el-input
            v-model="dataForm.dictName"
            maxlength="20"
            placeholder="请输入字典名称"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item prop="dictType" label="字典类型">
          <el-input
            v-model="dataForm.dictType"
            maxlength="20"
            placeholder="请输入字典类型"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn">
        <el-button type="primary" @click="getDataList">{{
          $t("query")
        }}</el-button>
        <el-button plain @click="resetForm('dataForm')">重置</el-button>
      </div>
    </div>
    <div class="card_info">
      <div class="function_btn">
        <el-button
          v-if="$hasPermission('sys:dict:save')"
          icon="el-icon-plus"
          type="primary"
          @click="addOrUpdateHandle()"
          >{{ $t("add") }}</el-button
        >
        <el-button
          v-if="$hasPermission('sys:dict:delete')"
          icon="el-icon-delete"
          plain
          class="btn_color"
          @click="deleteHandle()"
          >{{ $t("deleteBatch") }}</el-button
        >
      </div>
      <div class="info_list">
        <el-table
          v-loading="dataListLoading"
          :data="dataList"
          stripe
          :header-cell-style="{ background: '#e6e7ea', color: '#0C1935' }"
          @selection-change="dataListSelectionChangeHandle"
          @sort-change="dataListSortChangeHandle"
          style="width: 100%"
          :height="screenHeight"
        >
          <el-table-column
            type="selection"
            header-align="center"
            align="center"
            width="50"
          ></el-table-column>
          <el-table-column
            prop="dictName"
            label="字典名称"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="dictType"
            label="字典类型"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              <el-button type="text" @click="childHandle(scope.row)">{{
                scope.row.dictType
              }}</el-button>
            </template>
          </el-table-column>
          <el-table-column
            prop="sort"
            label="排序"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="remark"
            label="备注"
            header-align="center"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="createDate"
            label="创建时间"
            header-align="center"
            align="center"
            width="180"
          ></el-table-column>
          <el-table-column
            label="操作"
            :resizable="false"
            fixed="right"
            header-align="center"
            align="center"
          >
            <template slot-scope="scope">
              <el-link
                v-if="$hasPermission('sys:dict:update')"
                type="primary"
                :underline="false"
                @click="addOrUpdateHandle(scope.row.id)"
                >编辑</el-link
              >
              <el-link
                v-if="$hasPermission('sys:dict:delete')"
                type="primary"
                :underline="false"
                @click="deleteHandle(scope.row.id)"
                >删除</el-link
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <template v-if="total > 0">
        <el-pagination
          :current-page="page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="limit"
          :total="total"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="pageSizeChangeHandle"
          @current-change="pageCurrentChangeHandle"
        >
        </el-pagination>
      </template>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <el-drawer
      size="25%"
      :visible.sync="drawer"
      :with-header="false"
      :modal="false"
      @close="closedrawer"
    >
      <add-or-update
        v-if="addOrUpdateVisible"
        ref="addOrUpdate"
        @refreshDataList="getDataList"
      ></add-or-update>
    </el-drawer>
    <dict-data ref="dictData" v-if="dictDataVisible"></dict-data>
  </div>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./dict-type-add-or-update";
import DictData from "./dict-data";
export default {
  mixins: [mixinViewModule],
  data() {
    return {
      screenHeight: "512px",
      mixinViewModuleOptions: {
        getDataListURL: "/sys/dict/type/page",
        getDataListIsPage: true,
        deleteURL: "/sys/dict/type",
        deleteIsBatch: true,
      },
      dataForm: {
        id: "0",
        dictName: "",
        dictType: "",
      },
      dictDataVisible: false,
    };
  },
  components: {
    AddOrUpdate,
    DictData,
  },
  mounted() {
    this.setTableHeight();
    // 页面创建完成后添加监听
    window.addEventListener("resize", this.setTableHeight);
  },
  methods: {
    setTableHeight() {
      setTimeout(() => {
        this.screenHeight = getComputedStyle(
          document.querySelector(".info_list")
        ).height;
      }, 1000);
    },
    //表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 子级
    childHandle(row) {
      this.dictDataVisible = true;
      this.$nextTick(() => {
        this.$refs.dictData.visible = true;
        this.$refs.dictData.dataForm.dictTypeId = row.id;
        this.$refs.dictData.dataForm.dictLabel = "";
        this.$refs.dictData.dataForm.dictValue = "";
        this.$refs.dictData.getDataList();
      });
    },
  },
  destroyed() {
    window.removeEventListener("resize", this.setTableHeight);
  },
};
</script>
