<template>
  <div class="app-container">
    <card-table :modalData="dataList" labelWidth="120px" :searchParams="queryParams" @pagination="getList" @selection-change="handleSelectionChange" :loading="loading"
                :tableSearch="true" @columnSearch="handleQuery"  @resetList="resetQuery">
      <template #operations="{ selections }">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:dict:add']"
            >{{$t('common.add')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:dict:edit']"
            >{{$t('common.edit')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:dict:remove']"
            >{{$t('common.delete')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:dict:export']"
            >{{$t('common.export')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              plain
              icon="el-icon-close"
              size="mini"
              @click="handleClose"
            >{{$t('common.close')}}</el-button>
          </el-col>
        </el-row>
      </template>

      <template #default="{ list }">
        <el-table-column type="selection" width="55" align="center" />
        <Column :label="$t('dicData.dictCode')" align="center" prop="dictCode" :min-width="120"/>
        <Column :label="$t('dicData.dictLabel')" align="center" prop="dictLabel" :min-width="150"
                :searchParams="queryParams" :showSearch="true" searchType="input">
          <template #default="{ row }">
            <span v-if="(row.listClass == '' || row.listClass == 'default') && (row.cssClass == '' || row.cssClass == null)">{{ row.dictLabel }}</span>
            <el-tag v-else :type="row.listClass == 'primary' ? '' : row.listClass" :class="row.cssClass">{{ row.dictLabel }}</el-tag>
          </template>
        </Column>
        <Column :label="$t('dicData.dictLabelUS')" align="center" prop="dictLabelUS" :min-width="150"
                :searchParams="queryParams" :showSearch="true" searchType="input">
          <template #default="{ row }">
            <span v-if="(row.listClass == '' || row.listClass == 'default') && (row.cssClass == '' || row.cssClass == null)">{{ row.dictLabelUS }}</span>
            <el-tag v-else :type="row.listClass == 'primary' ? '' : row.listClass" :class="row.cssClass">{{ row.dictLabelUS }}</el-tag>
          </template>
        </Column>
        <Column :label="$t('dicData.dictLabelRU')" align="center" prop="dictLabelRU" :min-width="150"
                :searchParams="queryParams" :showSearch="true" searchType="input">
          <template #default="{ row }">
            <span v-if="(row.listClass == '' || row.listClass == 'default') && (row.cssClass == '' || row.cssClass == null)">{{ row.dictLabelRU }}</span>
            <el-tag v-else :type="row.listClass == 'primary' ? '' : row.listClass" :class="row.cssClass">{{ row.dictLabelRU }}</el-tag>
          </template>
        </Column>
        <Column :label="$t('dicData.dictLabelES')" align="center" prop="dictLabelES" :min-width="150"
                :searchParams="queryParams" :showSearch="true" searchType="input">
          <template #default="{ row }">
            <span v-if="(row.listClass == '' || row.listClass == 'default') && (row.cssClass == '' || row.cssClass == null)">{{ row.dictLabelES }}</span>
            <el-tag v-else :type="row.listClass == 'primary' ? '' : row.listClass" :class="row.cssClass">{{ row.dictLabelES }}</el-tag>
          </template>
        </Column>
        <Column :label="$t('dicData.dictValue')" align="center" prop="dictValue" :min-width="120"/>
        <Column :label="$t('dicData.dictSort')" align="center" prop="dictSort" :min-width="120"/>
        <Column :label="$t('dicData.status')" align="center" prop="status" min-width="150"
                         :searchDict="dict.type.sys_normal_disable" :searchParams="queryParams" :showSearch="true" searchType="select">
          <template #default="{ row }">
            <dict-tag :options="dict.type.sys_normal_disable" :value="row.status"/>
          </template>
        </Column>
        <Column :label="$t('common.remark')" align="center" prop="remark" :show-overflow-tooltip="true" :minwidth="200"/>
        <Column :label="$t('common.createTime')" align="center" prop="createTime" :width="250"
                :searchParams="queryParams" :showSearch="true" searchType="dateRange">
          <template #default="{ row }">
            <span>{{ parseTime(row.createTime) }}</span>
          </template>
        </Column>
        <Column :label="$t('common.operate')" align="center"  min-width="200" fixed="right"
                :showSearch="true" searchType="operate">
          <template #default="{ row }">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(row)"
              v-hasPermi="['system:dict:edit']"
            >{{$t('common.edit')}}</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(row)"
              v-hasPermi="['system:dict:remove']"
            >{{$t('common.delete')}}</el-button>
          </template>
        </Column>
      </template>
    </card-table>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="30%">
        <el-form-item :label="$t('dicType.dictType')">
          <el-input v-model="form.dictType" :disabled="true" />
        </el-form-item>
        <el-form-item :label="$t('dicData.dictLabel')" prop="dictLabel">
          <el-input v-model.trim="form.dictLabel" :placeholder="$t('dicData.placeholder_dataLabel')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.dictLabelUS')" prop="dictLabelUS">
          <el-input v-model.trim="form.dictLabelUS" :placeholder="$t('dicData.placeholder_dataLabelUS')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.dictLabelRU')" prop="dictLabelRU">
          <el-input v-model.trim="form.dictLabelRU" :placeholder="$t('dicData.placeholder_dataLabelRU')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.dictLabelES')" prop="dictLabelES">
          <el-input v-model.trim="form.dictLabelES" :placeholder="$t('dicData.placeholder_dataLabelES')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.dictValue')" prop="dictValue">
          <el-input v-model.trim="form.dictValue" :placeholder="$t('dicData.placeholder_dataValue')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.listClass')" prop="cssClass">
          <el-input v-model.trim="form.cssClass" :placeholder="$t('dicData.placeholder_cssClass')" />
        </el-form-item>
        <el-form-item :label="$t('dicData.displaySort')" prop="dictSort">
          <el-input-number v-model="form.dictSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item :label="$t('dicData.listClass')" prop="listClass">
          <el-select v-model="form.listClass">
            <el-option
              v-for="item in listClassOptions"
              :key="item.value"
              :label="item.label + '(' + item.value + ')'"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('dicData.status')" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('common.remark')" prop="remark">
          <el-input v-model.trim="form.remark" type="textarea" :placeholder="$t('common.pleaseInput')"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('common.confirm')}}</el-button>
        <el-button @click="cancel">{{$t('common.cancel')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listData, getData, delData, addData, updateData } from "@/api/system/dict/data";
import { optionselect as getDictOptionselect, getType } from "@/api/system/dict/type";

export default {
  name: "Data",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      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: "危险"
        }
      ],
      // 类型数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictType: undefined,
        dictLabel: undefined,
        dictLabelUS: undefined,
        dictLabelRU: undefined,
        dictLabelES: undefined,
        status: undefined,
        createTime: [],
        language: ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictLabel: [
          { required: true, message: this.$t('dicData.rules_dictLabel_message'), trigger: "blur" }
        ],
        dictLabelUS: [
          { required: true, message: this.$t('dicData.rules_dictLabelUS_message'), trigger: "blur" }
        ],
        dictLabelRU: [
          { required: true, message: this.$t('dicData.rules_dictLabelRU_message'), trigger: "blur" }
        ],
        dictLabelES: [
          { required: true, message: this.$t('dicData.rules_dictLabelES_message'), trigger: "blur" }
        ],
        dictValue: [
          { required: true, message: this.$t('dicData.rules_dictValue_message'), trigger: "blur" }
        ],
        dictSort: [
          { required: true, message: this.$t('dicData.rules_dictSort_message'), trigger: "blur" }
        ]
      }
    };
  },
  created() {
    const dictId = this.$route.params && this.$route.params.dictId;
    this.getType(dictId);
    this.getTypeList();
  },
  methods: {
    /** 查询字典类型详细 */
    getType(dictId) {
      getType(dictId).then(response => {
        this.queryParams.dictType = response.data.dictType;
        this.defaultDictType = response.data.dictType;
        this.getList();
      });
    },
    /** 查询字典类型列表 */
    getTypeList() {
      getDictOptionselect().then(response => {
        this.typeOptions = [];
        let dictNameStr = "";
        response.data.forEach(item =>{
          dictNameStr = "";
          if(this.$i18n.locale === 'chinese'){
            dictNameStr = item.dictName;
          }
          if(this.$i18n.locale === 'english'){
            dictNameStr = item.dictNameUS;
          }
          if(this.$i18n.locale === 'russia'){
            dictNameStr = item.dictNameRU;
          }
          if(this.$i18n.locale === 'spanish'){
            dictNameStr = item.dictNameES;
          }
          this.typeOptions.push({
            dictId: item.dictId,
            dictType: item.dictType,
            dictName: dictNameStr
          })
        })
      });
    },
    /** 查询字典数据列表 */
    getList() {
      let params;
      this.loading = true;
      params = {
        pageNum:this.queryParams.pageNum,
        pageSize:this.queryParams.pageSize,
        language:this.queryParams.language,
        beginTime: this.queryParams.beginTime,
        endTime: this.queryParams.endTime,

        dictType: this.queryParams.dictType,
        dictLabel: this.queryParams.dictLabel,
        dictLabelUS: this.queryParams.dictLabelUS,
        dictLabelRU: this.queryParams.dictLabelRU,
        dictLabelES: this.queryParams.dictLabelES,
        status: this.queryParams.status
      }
      listData(params).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dictCode: undefined,
        dictLabel: undefined,
        dictLabelUS: undefined,
        dictLabelRU: undefined,
        dictLabelES: undefined,
        dictValue: undefined,
        cssClass: undefined,
        listClass: 'default',
        dictSort: 0,
        status: "0",
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/system/dict" };
      this.$tab.closeOpenPage(obj);
    },
    /** 重置按钮操作 */
    resetQuery() {
/*      this.queryParams.dictType = "";*/
      this.queryParams.dictLabel = "";
      this.queryParams.dictLabelUS = "";
      this.queryParams.dictLabelRU = "";
      this.queryParams.dictLabelES = "";
      this.queryParams.status = "";
      this.queryParams.createTime = [];
      this.queryParams.beginTime = '';
      this.queryParams.endTime = '';
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('dicData.addDicdata');
      this.form.dictType = this.queryParams.dictType;
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictCode)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictCode = row.dictCode || this.ids
      getData(dictCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('dicData.editDicdata');
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.dictCode != undefined) {
            updateData(this.form).then(response => {
              this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
              this.$modal.msgSuccess(this.$t('common.editSuccess'));
              this.open = false;
              this.getList();
            });
          } else {
            addData(this.form).then(response => {
              this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
              this.$modal.msgSuccess(this.$t('common.addSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictCodes = row.dictCode || this.ids;
      this.$modal.confirm(this.$t('common.isDelete')).then(function() {
        return delData(dictCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.deleteSuccess'));
        this.$store.dispatch('dict/removeDict', this.queryParams.dictType);
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.language = this.$i18n.locale;
      this.download('system/dict/data/export', {
        ...this.queryParams
      }, `data_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
