<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: zwq
 * @LastEditTime: 2021-04-16 13:13:50
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form
      :model="listQuery"
      :inline="true"
      size="medium"
      label-width="100px"
    >
      <el-form-item :label="$t('module.basicData.materialsType.TypeName')" prop="typeName">
        <el-input v-model="listQuery.typeName" :placeholder="$i18nForm(['placeholder.input', this.$t('module.basicData.materialsType.TypeName')])" style="width:200px" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.materialsType.TypeCode')" prop="typeCode">
        <el-input v-model="listQuery.typeCode" :placeholder="$i18nForm(['placeholder.input', this.$t('module.basicData.materialsType.TypeCode')])" style="width:200px" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()"> {{ 'btn.search' | i18nFilter }} </el-button>
        <el-button type="primary" @click="addNew()"> {{ 'btn.add' | i18nFilter }} </el-button>
      </el-form-item>
    </el-form>
    <base-table
      :page="listQuery.current"
      :limit="listQuery.size"
      :table-config="tableProps"
      :table-data="list"
      :is-loading="listLoading"
    >
      <method-btn
        slot="handleBtn"
        :width="trueWidth"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
    <dataDictionary-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import { dataDictionaryTypeList, dataDictionaryTypeDelete } from '@/api/basicData/dataDictionaryType'
import DataDictionaryAdd from './components/dataDictionary-add.vue'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import i18n from '@/lang'
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */

const tableBtn = [
  {
    type: 'edit',
    btnName: 'btn.edit'
  },
  {
    type: 'list',
    btnName: i18n.t('module.basicData.dataDictionary.list')
  },
  {
    type: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'typeName',
    label: i18n.t('module.basicData.materialsType.TypeName'),
    align: 'center'
  },
  {
    prop: 'typeCode',
    label: i18n.t('module.basicData.materialsType.TypeCode'),
    align: 'center'
  },
  {
    prop: 'remark',
    label: i18n.t('module.basicData.visual.Remarks'),
    align: 'center'
  }
]

export default {
  name: 'Factory',
  components: { Pagination, BaseTable, MethodBtn, DataDictionaryAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 250,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        typeName: '',
        typeCode: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.typeName}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          dataDictionaryTypeDelete(raw.data.id).then(response => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
        }).catch(() => {})
      } else if (raw.type === 'list') {
        this.toList(raw.data.id)
      } else {
        this.addNew(raw.data.id)
      }
    },
    getList() {
      this.listLoading = true
      dataDictionaryTypeList(this.listQuery).then(response => {
        if (response.data.records) {
          this.list = response.data.records
        } else {
          this.list.splice(0, this.list.length)
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    toList(id) {
      this.$router.push({
        name: 'dataDictionaryData',
        query: {
          dictTypeId: id
        }
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
