<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 字典类型 -->
      <el-col :span="4">
        <el-menu :default-active="navselected" @select="selectItems" class="el-menu-vertical-demo">
          <el-menu-item :index="index.toString()" v-for="(item, index) in typeList" :key="item.dictId">
            <span slot="title">{{ item.dictName }}</span>
          </el-menu-item>
        </el-menu>
      </el-col>
      <!-- 字典数据 -->
      <el-col :span="20">
        <el-row :gutter="10" class="mb8">
          <template>
            <el-col :span="8">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:dict:add']">新增</el-button>
            </el-col>
          </template>
        </el-row>

        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" size="mini">
          <el-table-column label="字典标签" align="center" prop="dictLabel" />
          <!-- <el-table-column label="字典编码" align="center" prop="dictValue" /> -->
          <el-table-column label="字典排序" align="center" prop="dictSort" />
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="ishidden&&!scope.row.default">
              <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dict:edit']">修改</el-button> -->
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dict:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="dialogWidth" append-to-body v-loading="loading" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数据标签" prop="dictLabel">
          <el-input v-model="form.dictLabel" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="显示排序" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio label="0">正常</el-radio>
            <el-radio label="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { listType, } from "@/api/system/dict/type";
import { listData, getData, delData, addData, updateData, changeDictDataStatus, } from "@/api/system/dict/data";

export default {
  name: "Dict",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      //字典模板表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据标签回显样式
      listClassOptions: [
        {
          value: "default",
          label: "默认"
        },
        {
          value: "primary",
          label: "主要"
        },
        {
          value: "success",
          label: "成功"
        },
        {
          value: "info",
          label: "信息"
        },
        {
          value: "warning",
          label: "警告"
        },
        {
          value: "danger",
          label: "危险"
        }
      ],
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      menuleftnav: undefined,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictLabel: [
          { required: true, message: "数据标签不能为空", trigger: "blur" }
        ],
        dictValue: [
          { required: true, message: "数据键值不能为空", trigger: "blur" }
        ],
        dictSort: [
          { required: true, message: "数据顺序不能为空", trigger: "blur" }
        ]
      },
      navselected: "0",
      ishidden: true,
      dialogWidth: '500px'
    };
  },

  created() {
    this.getTypeList();
    //this.getDataTemplateList();
  },
  methods: {
    /** 查询字典类型列表 */
    getTypeList() {
      this.loading = true;
      listType().then((response) => {
        this.typeList = response.rows;
        this.loading = false;
        //左边菜单加载完毕才加载右边的菜单
        this.queryParams.dictType = this.typeList[this.navselected].dictType;
        this.getList();
      });
    },
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      listData(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    selectItems(index) {
      this.menuleftnav = index;
      this.queryParams.dictType = this.typeList[index].dictType;
      this.getList();
    },

    // 表单重置
    reset() {
      this.form = {
        dictCode: undefined,
        dictLabel: undefined,
        dictValue: undefined,
        cssClass: undefined,
        listClass: 'default',
        dictSort: 0,
        status: '0',
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.dictType = this.typeList[this.menuleftnav].dictType;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典数据";
      this.$set(
        this.form,
        "dictType",
        this.typeList[this.menuleftnav].dictType
      );
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictCode = row.dictCode || this.ids
      getData(dictCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典数据";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.dictType = this.queryParams.dictType;
      this.form.dictValue = this.form.dictLabel
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dictCode != undefined) {
            updateData(this.form).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }

            });
          } else {
            addData(this.form).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }

            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids;
      this.$modal.confirm('是否确认删除字典编码为"' + row.dictValue + '"的数据项？').then(function () {
        return delData(dictCodes);
      }).then((resp) => {
        if (resp.code == 200) {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }

      }).catch(() => { });
    },
    // 字典模板状态修改
    handleStatusChange(row) {
      let text = row.status === true ? "启用" : "停用";
      this.$modal
        .confirm('确认要"' + text + '""' + row.dictLabel + '"标签吗？')
        .then(function () {
          return changeDictDataStatus(row.dictCode, row.status);
        })
        .then((resp) => {
          if (resp.code == 200) {
            this.$modal.msgSuccess(text + "成功");
          }
        })
        .catch(function () {
          row.status = row.status === false ? true : false;
        });
    },
  },
};
  </script>
  