<!--
 * @Author: zwq
 * @Date: 2020-12-29 16:37:56
 * @LastEditors: zwq
 * @LastEditTime: 2021-03-30 18:39:46
 * @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="100px" @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="attrValue">
        <el-input v-model="dataForm.attrValue" :placeholder="$i18nForm(['placeholder.input', $t('module.basicData.visual.AttributeValue')])" 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 { substrateBatchAttrDetail, substrateBatchAttrUpdate, substrateBatchAttrAdd } from '@/api/orderManage/substrateBatchAttr'

export default {
  props: {
    subId: {
      type: String,
      default: () => {
        return ''
      }
    }
  },
  data() {
    return {
      visible: false,
      btnLoading: false,
      dataForm: {
        id: 0,
        attrName: '',
        attrValue: ''
      },
      dataRule: {
        attrName: [
          { required: true, message: this.$i18nForm(['placeholder.input', this.$t('module.basicData.visual.AttributeName')]), trigger: 'blur' }
        ],
        attrValue: [
          { 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) {
          substrateBatchAttrDetail(this.dataForm.id).then(res => {
            this.dataForm.attrName = res.data.name
            this.dataForm.attrValue = res.data.value
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.btnLoading = true
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            'name': this.dataForm.attrName,
            'value': this.dataForm.attrValue,
            'subId': this.subId,
            'id': this.dataForm.id
          }
          if (this.dataForm.id) {
            substrateBatchAttrUpdate(data).then(res => {
              this.$message({
                message: this.$t('module.basicData.visual.success'),
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            })
          } else {
            substrateBatchAttrAdd(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>
