<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-05-28 10:48:46
 * @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>
    <div class="drawer-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { DetectAreaSystemAttrDetail, DetectAreaSystemAttrUpdate, DetectAreaSystemAttrAdd } from '@/api/basicData/Equipment/equipmentDetectAreaSystemAttr'
import { equipmentInfoList } from '@/api/basicData/Equipment/equipmentInfo'

export default {
  props: {
    detectAreaSystemId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        equipmentId: ''
      },
      equipmentArr: [],
      dataRule: {
        equipmentId: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.AttributeName')]), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      equipmentInfoList({
        current: 1,
        size: 999,
        name: ''
      }).then(response => {
        this.equipmentArr = response.data.records
      })
      this.dataForm.id = id || ''
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          DetectAreaSystemAttrDetail(this.dataForm.id).then(res => {
            this.dataForm.equipmentId = res.data.equipmentId
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'equipmentId': this.dataForm.equipmentId,
            'detectAreaSystemId': this.detectAreaSystemId,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            DetectAreaSystemAttrUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            DetectAreaSystemAttrAdd(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.drawer-footer {
  width: 100%;
  margin-top: 50px;
  border-top: 1px solid #e8e8e8;
  padding: 10px 50px;
  text-align: left;
  background: #fff;
}
</style>
