<!--
 * @Descripttion:
 * @version:
 * @Author: fzq
 * @Date: 2022-03-03 09:51:25
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-08 16:56:40
-->
<template>
  <div class="app-container">
    <head-form
      :placeholder-name="placeholderName"
      :key-name="keyName"
      @getDataList="getList"
      @add="addNew"
    />
    <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()"
    />
    <processInfo-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>import i18n from '@/lang'
import HeadForm from '@/components/basicData/HeadForm'
import BaseTable from '@/components/BaseTable'
import processInfoAdd from './components/processInfo-add'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import { list, locaDelete } from '@/api/basicData/Cache/processLocation'
/**
 * 表格表头配置项 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: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'workSequenId',
    label: i18n.t('module.basicData.processLocation.workSequenId'),
    align: 'center'
  },
  {
    prop: 'locationId',
    label: i18n.t('module.basicData.processLocation.locationId'),
    align: 'center'
  },
  {
    prop: 'portAttrId',
    label: i18n.t('module.basicData.processLocation.portAttrId'),
    align: 'center'
  },
  {
    prop: 'isProcess',
    label: i18n.t('module.basicData.processLocation.isProcess'),
    align: 'center'
  },
  {
    prop: 'equipmentMark',
    label: i18n.t('module.basicData.processLocation.equipmentMark'),
    align: 'center'
  },
  {
    prop: 'sequence',
    label: i18n.t('module.basicData.processLocation.sequence'),
    align: 'center'
  },
  {
    prop: 'goodsShelves',
    label: i18n.t('module.basicData.processLocation.goodsShelves'),
    align: 'center'
  },
  {
    prop: 'locationName',
    label: i18n.t('module.basicData.processLocation.locationName'),
    align: 'center'
  }
]

export default {
  name: 'Area',
  components: { Pagination, BaseTable, MethodBtn, HeadForm, processInfoAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      addOrUpdateVisible: false,
      keyName: i18n.t('module.basicData.visual.keyword'),
      placeholderName: i18n.t('module.basicData.processLocation.locationName'),
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      areaList: [],
      total: 0,
      listLoading: true,
      listQuery: {
        current: 1,
        size: 10,
        locationId: '',
        workSequenId: '',
        locationName: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleClick(raw) {
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.id}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          locaDelete(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 === 'edit') {
        this.addNew(raw.data.id)
      } else {
        this.addNew(raw.data.id, true)
      }
    },
    getList(key) {
      this.listLoading = true
      this.listQuery.locationName = key
      list(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
        // console.log(response)
        this.listLoading = false
      })
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, true)
      })
    }
  }
}
</script>

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