<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: gtz
 * @LastEditTime: 2021-12-13 16:56:03
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" ref="dataForm" v-loading="formLoading" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="车辆编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="车辆编码"></el-input>
      </el-form-item>
      <el-form-item label="车辆名称" prop="vehicleName">
        <el-input v-model="dataForm.vehicleName" placeholder="车辆名称"></el-input>
      </el-form-item>
      <el-form-item label="缩写" prop="vehicleAlias">
        <el-input v-model="dataForm.vehicleAlias" 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="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: '',
          code: '',
          ip: '',
          vehicleName: '',
          vehicleAlias: '',
          note: ''
        },
        formLoading: false
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.formLoading = true
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/vehicle/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(`/vehicle/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(`/vehicle/${!this.dataForm.id ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'vehicleName': this.dataForm.vehicleName,
                'ip': this.dataForm.ip,
                'vehicleAlias': this.dataForm.vehicleAlias,
                'note': this.dataForm.note
              })
            }).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>
