<!--配置字典-->
<template>
  <div class="page-container dictAddPage">
    <div class="page-form-title-box">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px" class="form-box">
        <el-form-item label="" prop="label">
          <el-input
            v-model="queryParams.label"
            placeholder="请输入字典标签"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="" prop="state">
          <el-select v-model="queryParams.state" placeholder="请选择状态" clearable size="small">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <p class="page-inquire-common-style-button" @click="handleQuery">查询</p>
        <p class="page-reset-common-style-button" @click="resetQuery">重置</p>
        <p class="page-out-common-style-button"
           style="float: right;"
           @click="backPage"
        >返回</p>
        <p class="page-submit-common-style-button"
           style="float: right;margin-right:20px;"
           @click="handleAdd"
           v-hasPermiRouter="['system:dict:add']"
        >新增</p>
      </el-form>
    </div>
    <div class="page-content-box">
      <el-table class="table-box" v-loading="loading" border :data="dataList" @selection-change="handleSelectionChange">
        <el-table-column label="字典名称" align="left" prop="label"/>
        <el-table-column label="字典数值" align="left" prop="value" />
        <el-table-column label="显示顺序" align="left" prop="sort" />
        <el-table-column label="状态" align="left" prop="state" v-hasPermiRouter="['system:dict:edit']">
          <template slot-scope="scope">
            <el-switch
              class="switch captcha-img"
              @change="switchChange(scope.row)"
              v-model="scope.row.state"
              :active-value="true"
              :inactive-value="false"
              active-text="开"
              inactive-text="关"
            >
            </el-switch>
            <!--<dict-tag :options="statusOptions" :value="scope.row.state"/>-->
          </template>
        </el-table-column>
        <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="left" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime,"{y}-{m}-{d} {h}:{i}:{s}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="left"
                         class-name="small-padding fixed-width" width="160"
                         v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="table-button-box">
              <p class="table-button-null"></p>
              <p class="table-button-p"
                 @click="handleUpdate(scope.row)"
                 v-hasPermiRouter="['system:dict:edit']"
              >编辑</p>
              <p class="table-button-p"
                 @click="handleDelete(scope.row)"
                 v-hasPermiRouter="['system:dict:del']"
              >删除</p>
              <p class="table-button-null"></p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
                  v-show="total>0"
                  :total="total"
                  layout="total, prev, pager, next, sizes, jumper"
                  :page.sync="queryParams.page"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getList"
      />
    </div>
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="数据标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入数据标签" />
        </el-form-item>
        <el-form-item label="数据键值" prop="value">
          <el-input v-model="form.value" placeholder="请输入数据键值" />
        </el-form-item>
        <el-form-item label="显示排序" prop="sort">
          <el-input-number v-model="form.sort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</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 dialog-footer-box">
        <p class="dialog-footer-button-null"></p>
        <p class="dialog-footer-button-info" @click="cancel">取消</p>
        <p class="dialog-footer-button-primary" @click="submitForm">提交</p>
        <p class="dialog-footer-button-null"></p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData, getData, delData, addData, updateData,listType, getType } from "@/api/systemManagement/index";

export default {
  props: {
    dictId: {},
  },
  name: "Data",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['system:dict:edit','system:dict:detail','system:dict:del']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      dataList: [],
      // 默认字典类型
      defaultDictType: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据标签回显样式
      listClassOptions: [
        {
          value: "default",
          label: "默认"
        },
        {
          value: "primary",
          label: "主要"
        },
        {
          value: "success",
          label: "成功"
        },
        {
          value: "info",
          label: "信息"
        },
        {
          value: "warning",
          label: "警告"
        },
        {
          value: "danger",
          label: "危险"
        }
      ],
      // 状态数据字典
      statusOptions: [
        { dictValue:true,dictLabel:"开" },
        { dictValue:false,dictLabel:"关" },
      ],
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize:20,
        dictName: undefined,
        dictId: undefined,
        state: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        label: [
          { required: true, message: "数据标签不能为空", trigger: "blur" },
          { required: true, message: "数据标签不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        value: [
          { required: true, message: "数据键值不能为空", trigger: "blur" },
          { required: true, message: "数据键值不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        sort: [
          { required: true, message: "数据顺序不能为空", trigger: "blur" },
          { required: true, message: "数据顺序不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // const dictId = this.$route.params && this.$route.params.dictId;
    const dictId = this.dictId;
    this.getType(dictId);
    // this.getTypeList();
  },
  methods: {
    //switch开关切换
    switchChange(row){
      let obj = JSON.parse(JSON.stringify(row))
      obj.dictId = this.dictId;
      obj.state = obj.state?obj.state:false
      updateData(obj).then(response => {
        this.msgSuccess("操作成功");
        this.getList();
      });
    },
    //返回按钮
    backPage(){
      this.$parent.clickPage(1);
    },
    /** 查询字典类型详细 */
    getType(dictId) {
      getType({dictId:dictId}).then(response => {
        this.defaultDictType = response.data.dictCode;
        this.getList();
      });
    },
    /** 查询字典类型列表 */
    getTypeList() {
      listType().then(response => {
        this.typeOptions = response.data.records;
      });
    },
    /** 查询字典数据列表 */
    getList() {
      this.loading = true;
      let obj = JSON.parse(JSON.stringify(this.queryParams))
      obj.dictId = this.dictId
      listData(obj).then(response => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        label: null,
        value: null,
        sort: 0,
        state: true,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this,'queryParams',{
        page: 1,
        pageSize:20,
        label: null,
        state: null
      })
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典数据";
      this.form.dictCode = this.queryParams.dictCode;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictCode)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$set(this,'form',{
        itemId:row.itemId,
        label:row.label,
        value:row.value,
        sort:row.sort,
        state:row.state,
        remark:row.remark,
      });
        this.open = true;
      // this.reset();
      // getData({itemId:row.dictId}).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改字典数据";
      // });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let obj = JSON.parse(JSON.stringify(this.form))
          obj.dictId = this.dictId;
          if (this.form.itemId) {
            updateData(obj).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addData(obj).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除该标签?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delData({dictId:row.dictId,itemId:row.itemId});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/data/export', {
        ...this.queryParams
      }, `data_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped lang="scss">
  .dictAddPage {

  }
</style>
