<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-05 14:08:41
 * @Description:
-->
<template>
  <el-drawer
    :append-to-body="true"
    :show-close="false"
    :visible.sync="visible"
    size="40%"
  >
    <div slot="title" style=" background-color:#02BCFF;font-size:1.5em;color:white;padding:5px 20px">
      {{ !dataForm.id ? 'btn.add' : 'btn.edit' | i18nFilter }}
    </div>
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="130px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item :label="$t('module.basicData.visual.AttributeName')" prop="attrName">
        <el-input v-model="dataForm.attrName" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.AttributeName')])" clearable />
      </el-form-item>
      <el-form-item :label="$t('module.basicData.visual.AttributeValue')" prop="attrContent">
        <el-input v-model="dataForm.attrContent" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.AttributeValue')])" clearable />
      </el-form-item>
      <!-- <el-form-item :label="$t('module.basicData.visual.Remarks')" prop="remark">
        <el-input v-model="dataForm.remark" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.Remarks')])" clearable />
      </el-form-item> -->
    </el-form>
    <div class="drawer-footer">
      <el-button @click="visible = false">{{ 'btn.cancel' | i18nFilter }}</el-button>
      <el-button type="primary" :loading="btnLoading" @click="dataFormSubmit()">{{ 'btn.confirm' | i18nFilter }}</el-button>
    </div>
  </el-drawer>
</template>

<script>
import { equipmentInfoAttrDetail, equipmentInfoAttrUpdate, equipmentInfoAttrAdd } from '@/api/basicData/Equipment/equipmentInfoAttr'

export default {
  props: {
    equipmentId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: 0,
        attrName: '',
        attrContent: '',
        remark: ''
      },
      dataRule: {
        attrName: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.AttributeName')]), trigger: 'blur' }
        ],
        attrContent: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.AttributeValue')]), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || ''
      this.btnLoading = false
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          equipmentInfoAttrDetail(this.dataForm.id).then(res => {
            console.log(res)
            this.dataForm = res.data
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.btnLoading = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'attrName': this.dataForm.attrName,
            'attrContent': this.dataForm.attrContent,
            'remark': this.dataForm.remark,
            'equipmentId': this.equipmentId,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            equipmentInfoAttrUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            equipmentInfoAttrAdd(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>
