<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-04 10:40:51
 * @Description:
-->
<template>
  <div class="app-container">
    <el-form
      :model="formData"
      :inline="true"
      size="medium"
      label-width="100px"
    >
      <el-form-item v-if="false" :label="$t('module.basicData.ScrapInfo.PlateId')" prop="basalId">
        <el-input v-model="formData.basalId" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.PlateId')])" style="width:200px" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.ScrapInfo.TimePeriod')" prop="time">
        <el-date-picker
          v-model="formData.timeSlot"
          type="daterange"
          format="yyyy-MM-dd"
          :start-placeholder="$t('module.basicData.ScrapInfo.StartTime')"
          :end-placeholder="$t('module.basicData.ScrapInfo.EndTime')"
          :range-separator="$t('module.basicData.ScrapInfo.To')"
          clearable
        />
        <!-- value-format="yyyy-MM-dd" -->
      </el-form-item>
      <el-form-item :label="$t('module.basicData.ScrapInfo.name')" prop="equipmentId">
        <el-select v-model="formData.equipmentId" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.ScrapInfo.name')])" clearable :style="{width: '100%'}" filterable>
          <el-option
            v-for="(item, index) in device"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </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="formData.current"
      :limit.sync="formData.size"
      @pagination="getList()"
    />
    <ScrapInfo-add v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
  </div>
</template>

<script>
import { getScrapList, delScrapInfo } from '@/api/quality-manage/scrap'
import { scrapReasonList } from '@/api/dict'
import ScrapInfoAdd from './ScrapInfo-add.vue'
// import ScrapInfoCause from './ScrapInfoCause.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 { timeFormatter } from '@/filters'
import i18n from '@/lang'
import { getEqList } from '@/api/equipment/maintain'
// import DictFilter from '@/components/BaseTable/subcomponents/DataDictFilter'
/**
 * 表格表头配置项 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: 'createTime',
  //   label: i18n.t('module.basicData.factory.createTime'),
  //   filter: timeFormatter
  // },
  {
    prop: 'source',
    label: i18n.t('module.basicData.ScrapInfo.source'),
    align: 'center'
  },
  {
    prop: 'equipmentName',
    label: i18n.t('module.basicData.ScrapInfo.name'),
    align: 'center'
  },
  {
    prop: 'substrateId',
    label: i18n.t('module.basicData.ScrapInfo.PlateId'),
    align: 'center'
  },
  {
    prop: 'registerPersonName',
    label: i18n.t('module.basicData.ScrapInfo.RegisterPerson'),
    align: 'center'
  },
  {
    prop: 'updateTime',
    label: i18n.t('module.basicData.ScrapInfo.updateTime'),
    filter: timeFormatter,
    align: 'center'
  },
  {
    prop: 'scrapReason',
    label: i18n.t('module.basicData.ScrapInfo.cause'),
    align: 'center'
  }
  // {
  //   prop: 'scrapReasonId',
  //   label: i18n.t('module.basicData.ScrapInfo.cause'),
  //   subcomponent: ScrapInfoCause,
  //   align: 'center'
  //   // filter: scrapReasonList
  // }
]

export default {
  name: 'ScrapInfo',
  components: { Pagination, BaseTable, MethodBtn, ScrapInfoAdd },
  data() {
    return {
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      formData: {
        timeSlot: null,
        basalId: '',
        current: 1,
        size: 10,
        id: ''
      },
      listQuery: {
        current: 1,
        size: 10
      },
      dict: {
        scrap: []
      },
      device: []
    }
  },
  created() {
    this.getDict()
    this.getList()
  },
  methods: {
    handleClick(raw) {
      if (raw.type === 'delete') {
        this.$confirm(i18n.t('deleteMsgBox.content'), i18n.t('deleteMsgBox.hint'), {
          confirmButtonText: i18n.t('btn.confirm'),
          cancelButtonText: i18n.t('btn.cancel'),
          type: 'warning'
        }).then(() => {
          delScrapInfo({ id: raw.data.id }).then(response => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
        }).catch(() => {})
      } else {
        this.addNew(raw.data.id)
      }
    },
    getList() {
      this.listLoading = true
      if (this.formData.timeSlot) {
        this.formData.startTime = this.formData.timeSlot[0]
        this.formData.endTime = this.formData.timeSlot[1]
      } else {
        this.formData.startTime = ''
        this.formData.endTime = ''
      }
      getScrapList(this.formData).then(response => {
        if (response.data.records) {
          this.list = response.data.records
          console.log(this.list)
        } else {
          this.list.splice(0, this.list.length)
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    async getDict() {
      const result1 = await getEqList({
        current: 1,
        size: 999
      })
      if (result1.code === 0) {
        this.device = result1.data.records
      }
      const result = await scrapReasonList()
      this.dict.scrap = result
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>

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