<template>
  <div class="app-container">
    <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-row>
        <el-col :span="8">
          <el-form-item :label="$t('字典名称')" prop="name">
            <el-input v-model="queryParams.name" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('是否启用')" prop="enableFlag">
            <el-select v-model="queryParams.enableFlag" clearable>
              <el-option v-for="item in dict.type.sys_yes_no" v-bind="item" :key="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['md:sapDict:add']">{{$t('新增')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['md:sapDict:edit']">{{$t('修改')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['md:sapDict:remove']">{{$t('删除')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['md:sapDict:export']">{{$t('导出')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="toDictData" v-hasPermi="['md:sapDict:edit']">{{$t('字典数据')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column :label="$t('字典名称')" prop="name"/>
      <el-table-column :label="$t('是否启用')" prop="enableFlag">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.sys_yes_no" :value="row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.option')" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button v-hasPermi="['md:sapDict:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)">{{$t('修改')}}</el-button>
          <el-button v-hasPermi="['md:sapDict:remove']" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)">{{$t('删除')}}</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-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item :label="$t('字典名称')" prop="name">
          <el-input v-model.trim="form.name" clearable/>
        </el-form-item>
        <el-form-item :label="$t('是否启用')" prop="enableFlag">
          <el-radio-group v-model="form.enableFlag">
            <el-radio v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.value">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </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 { addSapDict, delSapDict, getSapDict, listSapDict, updateSapDict } from '@/api/mes/md/sapDict'

export default {
  dicts: ['mes_client_type', 'sys_yes_no'],
  data() {
    return {
      optType: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户表格数据
      list: [],
      propertyTypeOptions: [
        { label: this.$t('客户特性'), value: 1 },
        { label: this.$t('产品特性'), value: 2 },
        { label: this.$t('订单特性'), value: 3 },
        { label: this.$t('品牌特性'), value: 4 }
      ],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        enableFlag: 'Y'
      },
      // 表单参数
      form: {
        id: null,
        name: '',
        dictList: [{ name: '', value: '', enableFlag: 'Y' }]
      },
      // 表单校验
      rules: {
        name: [{ required: true, max: 32, message: this.$t('字典名称不能为空，长度在32字以内'), trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询客户列表 */
    getList() {
      this.loading = true
      listSapDict(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total
      }).finally(() => this.loading = false)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    toDictData() {
      this.$router.push('/md/sapDictData')
    },
    /**
     * 删除列
     */
    onAddItem() {
      this.form.dictList.push({ name: '', value: '', enableFlag: 'Y' })
    },
    /**
     * 删除列
     */
    onDelItem(item) {
      if (item.id) {
        item.enableFlag = 'N'
      } else {
        let index = this.form.dictList.indexOf(item)
        if (index !== -1) {
          this.form.dictList.splice(index, 1)
        }
      }
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        name: '',
        dictList: [{ name: '', value: '', enableFlag: 'Y' }]
      }
      this.resetForm('form')
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('添加字典')
      this.optType = 'add'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (!row.id && this.ids.length !== 1) this.$message.error(this.$t('只能选择一条数据'))
      this.reset()
      const id = row.id || this.ids[0]
      getSapDict(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = this.$t('修改字典')
        this.optType = 'edit'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSapDict(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('修改成功'))
              this.open = false
              this.getList()
            })
          } else {
            addSapDict(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('新增成功'))
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function() {
        return delSapDict(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess(this.$t('删除成功'))
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/sapDict/export', {
        ...this.queryParams
      }, `sapDict_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
