<!--
 * @Author: gtz
 * @Date: 2021-11-19 10:10:52
 * @LastEditors: gtz
 * @LastEditTime: 2021-12-13 17:00:12
 * @Description: file content
 * @FilePath: \mt-qj-wms-ui\src\views\basic\components\equipmentInfo-add.vue
-->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" v-loading="formLoading" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="名称" prop="kilnName">
        <el-input v-model="dataForm.kilnName" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="别名" prop="kilnAlias">
        <el-input v-model="dataForm.kilnAlias" placeholder="别名"></el-input>
      </el-form-item>
      <el-form-item label="英文名" prop="en">
        <el-input v-model="dataForm.en" placeholder="英文名"></el-input>
      </el-form-item>
      <el-form-item label="ip" prop="ip">
        <el-input v-model="dataForm.ip" placeholder="ip"></el-input>
      </el-form-item>
      <el-form-item label="端口号" prop="portNumber">
        <el-input v-model="dataForm.portNumber" placeholder="端口号"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="note">
        <el-input v-model="dataForm.note" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: null,
          description: null,
          en: null,
          ip: null,
          kilnAlias: null,
          kilnName: null,
          note: null,
          portNumber: null
        },
        dataRule: {
          kilnName: [
            { required: true, message: '名称不能为空', trigger: 'blur' }
          ],
          ip: [
            { required: true, message: 'ip不能为空', trigger: 'blur' }
          ],
          portNumber: [
            { required: true, message: '端口号不能为空', trigger: 'blur' }
          ]
        },
        formLoading: true
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.formLoading = true
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/kilnInfo/get`),
              method: 'post',
              data: this.$http.adornData({ id })
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              } else {
                this.$message.error(data.msg)
              }
              this.formLoading = false
            })
          } else {
            this.$http({
              url: this.$http.adornUrl(`/kilnInfo/codeGenerator`),
              method: 'post',
              data: this.$http.adornData()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.data
              } else {
                this.$message.error('编码生成失败')
              }
              this.formLoading = false
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kilnInfo/${!this.dataForm.id ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'description': this.dataForm.description,
                'en': this.dataForm.en,
                'ip': this.dataForm.ip,
                'kilnAlias': this.dataForm.kilnAlias,
                'kilnName': this.dataForm.kilnName,
                'note': this.dataForm.note,
                'portNumber': this.dataForm.portNumber
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
