<!-- 仓库维护 -->
<template>
  <div class="container">
    <my-table :data="data" :columns="columns" :table-filter="tableFilter" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>
    <my-modal-form v-model="show" :title="title" :config="formConfig" :rule-validate="ruleValidate" :data="formData" :type="formType" @confirm="cfm"></my-modal-form>
  </div>
</template>

<script>
import { DataDictionary, AddDataDictionary, UpdateDataDictionary, DeleteDataDictionary, GetGroupCodeList } from '@/api/baseData'
import MyTable from '_c/my-table/my-table'
import MyModalForm from '_c/my-modal-form/my-modal-form'
export default {
  name: 'warehouse_maintenance',
  components: {
    MyTable,
    MyModalForm
  },
  data () {
    return {
      title: '',
      show: false,
      data: [],
      deleteId: '',
      formType: '',
      formData: {},
      ruleValidate: {
        key: [
          { required: true, message: '键值不能为空', trigger: 'blur' }
        ],
        value: [
          { required: true, message: '值不能为空', trigger: 'blur' }
        ]
      },
      formConfig: [
        {
          type: 'input',
          label: '键',
          key: 'key',
          placeholder: '请输入键'
        },
        {
          type: 'input',
          label: '值',
          key: 'value',
          placeholder: '请输入值'
        },
        {
          type: 'input',
          label: '备注',
          key: 'remark',
          placeholder: '请输入备注'
        }
      ],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '键',
          key: 'key',
          minWidth: 150
        },
        {
          title: '值',
          key: 'value',
          minWidth: 150
        },
        {
          title: '备注',
          key: 'remark',
          minWidth: 150
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          align: 'center',
          width: '200px',
          render: (h, params) => {
            return h('div', [
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-edit',
                    size: '20',
                    color: '#087BE1'
                  },
                  style: {
                    marginRight: '20px'
                  },
                  on: {
                    click: () => {
                      this.formData = { ...params.row }
                      this.title = '修改'
                      this.formType = 'edit'
                      this.show = true
                    }
                  }
                },
                '修改'
              ),
              h(
                'Icon',
                {
                  props: {
                    custom: 'iconfont icon-delete',
                    size: '20',
                    color: 'red'
                  },
                  on: {
                    click: () => {
                      this.$MyModal.show({ text: '是否删除？' }).$on('confirm', () => {
                        DeleteDataDictionary({
                          id: params.row.id
                        }).then(res => {
                          if (res.code === 0) {
                            this.$Message.info(res.msg)
                            this.getList()
                          }
                        })
                      })
                    }
                  }
                },
                '删除'
              )
            ])
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Select',
          value: 'groupCode',
          placeholder: '字段类型',
          list: []
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '添加',
          align: 'right',
          e: 'add'
        }
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0
      }
    }
  },
  methods: {
    cfm (type, formData) {
      if (type === 'add') {
        AddDataDictionary({
          ...formData,
          groupCode: this.params.groupCode
        }).then(res => {
          if (res.code === 0) {
            this.show = false
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      } else if (type === 'edit') {
        UpdateDataDictionary({
          ...formData,
          groupCode: this.params.groupCode
        }).then(res => {
          if (res.code === 0) {
            this.show = false
            this.$Message.info(res.msg)
            this.getList()
          }
        })
      }
    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.key = option.key
          this.params.groupCode = option.groupCode
          this.getList()
          break
        case 'add':
          if (!this.params.groupCode) return this.$Message.error('请选择字段类型')
          this.title = '添加'
          this.formType = 'add'
          this.show = true
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getGroupCodeList () {
      GetGroupCodeList().then(res => {
        if (res.code === 0) {
          this.tableFilter[1].list = res.result.map(item => {
            return {
              label: item.text,
              value: item.value
            }
          })
        }
      })
    },
    getList () {
      DataDictionary({
        ...this.params
      }).then(res => {
        if (res.code === 0) {
          this.data = res.result.list
          this.params.total = res.result.totalCount
        }
      })
    }
  },
  mounted () {
    this.getList()
    this.getGroupCodeList()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
</style>
