<!--数据字典-->
<template>
  <div class="app-container dict">
    <div class="page-container dictPage" v-if="pageType == 1">
      <div class="page-form-title-box">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" class="form-box">
          <el-form-item label="" prop="dictName" label-width="68px">
            <el-input
              v-model="queryParams.searchValue"
              placeholder="字典名称/字典类型"
              clearable
              size="small"
              style="width: 240px"
            />
          </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>
          <el-form-item label="">
            <el-date-picker
              v-model="dateRange"
              size="small"
              style="width: 240px"
              clearable
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="创建时间"
              end-placeholder="创建时间"
            ></el-date-picker>
          </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-submit-common-style-button"
             style="float: right;"
             @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="typeList" @selection-change="handleSelectionChange" >
          <el-table-column label="字典名称" align="left" prop="dictName"/>
          <el-table-column label="字典类型" align="left" prop="dictCode"/>
          <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>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="left" prop="remark"/>
          <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="240"
                           v-if="tableButtonType">
            <template slot-scope="scope">
              <div class="table-button-box">
                <p class="table-button-null"></p>
                <p class="table-button-p"
                   @click="clickPage(2,scope.row.dictId)"
                   v-hasPermiRouter="['system:dict:edit']"
                >配置字典</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-if="pageType == 1"
                    v-show="total>0"
                    :total="total"
                    layout="total, prev, pager, next, sizes, jumper"
                    :page.sync="queryParams.page"
                    :limit.sync="queryParams.pageSize"
                    @pagination="getList"
        />
      </div>
    </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="dictName">
          <el-input v-model="form.dictName" placeholder="请输入字典名称" />
        </el-form-item>
        <el-form-item label="字典类型" prop="dictCode">
          <el-input v-model="form.dictCode" placeholder="请输入字典类型" />
        </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>
    <!--字典配置-->
    <data-page v-if="pageType==2" :dictId="dictId"></data-page>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, refreshCache } from "@/api/systemManagement/index";
import dataPage from "./data.vue";

export default {
  components: {
    dataPage,
  },
  name: "Dict",
  data() {
    return {
      tableButtonType:this.hasPermiDom(['system:dict:list','system:dict:edit','system:dict:detail','system:dict:del']),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [
        { dictValue:true,dictLabel:"开" },
        { dictValue:false,dictLabel:"关" },
      ],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        page: 1,
        pageSize:20,
        searchValue: null,
        dictName: undefined,
        dictCode: undefined,
        state: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" },
          { required: true, message: "字典名称不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ],
        dictCode: [
          { required: true, message: "字典类型不能为空", trigger: "blur" },
          { required: true, message: "字典类型不能为空", validator: this.spaceJudgment, trigger: "blur" }
        ]
      },
      //页面状态
      pageType:1,
      // 选中id
      dictId:"",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //页面切换
    clickPage(type,id){
      if(this.pageType!=type){
        if(type==1){
          this.dictId="";
          this.pageType=1;
        }else if(type==2){
          this.dictId=id;
          this.pageType=2;
        }
      }
    },
    //switch开关切换
    switchChange(row){
      let obj = {
        dictId:row.dictId,
        dictName:row.dictName,
        dictCode:row.dictCode,
        state:row.state?row.state:false,
        remark:row.remark?row.remark:'',
      }
      updateType(obj).then(response => {
        this.msgSuccess("操作成功");
      });
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      let obj = JSON.parse(JSON.stringify(this.queryParams))
      if(this.dateRange[0]){
        obj.startTime = this.dateRange[0]+'T00:00:00'
        obj.endTime = this.dateRange[1]+'T23:59:59'
      }else{
        obj.startTime = "";
        obj.endTime = "";
      }
      listType(this.addDateRange(obj)).then(response => {
          this.typeList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictId: undefined,
        dictName: undefined,
        dictCode: undefined,
        state: true,
        remark: undefined
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this,'queryParams',{
        page: 1,
        pageSize:20,
        searchValue: null,
        dictName: undefined,
        dictCode: undefined,
        state: undefined
      })
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.dictId || this.ids
      getType({dictId:dictId}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改字典类型";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dictId != undefined) {
            let obj = {
              dictId:this.form.dictId,
              dictName:this.form.dictName,
              dictCode:this.form.dictCode,
              state:this.form.state,
              remark:this.form.remark?this.form.remark:'',
            }
            updateType(obj).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            let obj = {
              dictName:this.form.dictName,
              dictCode:this.form.dictCode,
              state:this.form.state,
              remark:this.form.remark?this.form.remark:'',
            }
            addType(obj).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.dictId || this.ids;
      this.$confirm('是否确认删除该字典?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delType({dictId:dictIds});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.msgSuccess("刷新成功");
      });
    }
  }
};
</script>

<style scoped lang="scss">
  .dict {
    .dictPage{

    }
  }
</style>
