<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-03 16:49:10
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="140px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.equipment.EquipmentName')" prop="equipmentId">
        <el-select v-model="dataForm.equipmentId" filterable :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.equipment.EquipmentName')])" clearable>
          <el-option
            v-for="item in equipmentArr"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>

      </el-form-item>
      <el-form-item :label="$t('module.basicData.EquipmentScrapGrade.ScrapGrade')" prop="scrapsGradeId">
        <el-select v-model="dataForm.scrapsGradeId" filterable :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.EquipmentScrapGrade.ScrapGrade')])" clearable>
          <el-option
            v-for="item in scrapsArr"
            :key="item.id"
            :label="item.dataName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { EquipmentScrapGradeDetail, EquipmentScrapGradeUpdate, EquipmentScrapGradeAdd } from '@/api/basicData/EquipmentScrapGrade'

import { equipmentInfoList } from '@/api/basicData/Equipment/equipmentInfo'
import { dataDictionaryDataList } from '@/api/basicData/dataDictionary'

export default {
  data() {
    return {
      visible: false,
      scrapsArr: [],
      equipmentArr: [],
      dataForm: {
        id: 0,
        equipmentId: '',
        scrapsGradeId: '',
        remark: ''
      },
      dataRule: {
        equipmentId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.equipment.EquipmentName')]), trigger: 'blur' }
        ],
        scrapsGradeId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.EquipmentScrapGrade.ScrapGrade')]), trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      equipmentInfoList({
        current: 1,
        size: 999
      }).then(response => {
        if (response.data.records) {
          this.equipmentArr = response.data.records
        } else {
          this.equipmentArr.splice(0, this.equipmentArr.length)
        }
      })
      dataDictionaryDataList({
        current: 1,
        size: 999,
        dictTypeId: '2'
      }).then(response => {
        if (response.data.records) {
          this.scrapsArr = response.data.records
        } else {
          this.scrapsArr.splice(0, this.scrapsArr.length)
        }
      })
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          EquipmentScrapGradeDetail(this.dataForm.id).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'equipmentId': this.dataForm.equipmentId,
            'scrapsGradeId': this.dataForm.scrapsGradeId,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            EquipmentScrapGradeUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            EquipmentScrapGradeAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>
