<template>
  <el-dialog :visible.sync="visible" :title="'btn.add' | i18nFilter" class="dialog" @close="close">
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      label-width="100px"
      @keyup.enter.native="dataFormSubmit()"
    >
      <el-form-item
        :label="this.$t('module.equipmentManager.inspectionManage.equipmentName')"
        prop="id"
      >
        <el-select
          v-model="dataForm.id"
          filterable
          :placeholder="this.$t('module.equipmentManager.inspectionManage.equipmentName')"
          @change="selectOrder()"
        >
          <el-option
            v-for="item in list"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('module.equipmentManager.inspectionManage.equipmentCode')" prop="code">
        <el-input
          v-model="dataForm.code"
          :placeholder="$i18nForm(['placeholder.input', $t('module.equipmentManager.inspectionManage.equipmentCode')])"
          clearable
          disabled
        />
      </el-form-item>
    </el-form>

    <el-row style="text-align: right">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.save' | i18nFilter }}</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { equipmentList, equipmentAdd } from '@/api/equipment/inspectionManager'
export default {
  data() {
    return {
      visible: false,
      list: [],
      dataForm: {
        id: '',
        code: ''
      },
      dataRule: {
        id: [
          {
            required: true,
            message: this.$i18nForm(['placeholder.input', this.$t('module.equipmentManager.inspectionManage.equipmentName')]),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.$nextTick(() => {
        equipmentList({
          current: 1,
          size: 999
        }).then(res => {
          if (res.data) {
            this.list = res.data
          } else {
            this.list = []
          }
        })
      })
    },
    selectOrder() {
      this.list.map(item => {
        if (item.id === this.dataForm.id) {
          this.dataForm.code = item.code
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          equipmentAdd({ equipmentId: this.dataForm.id }).then(res => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1000,
              onClose: () => {
                this.close()
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    },
    close() {
      this.dataForm.id = ''
      this.dataForm.code = ''
      this.visible = false
    }
  }
}
</script>

<style scoped>
.dialog >>> .el-dialog__body {
  padding: 30px 24px;
}
</style>
