<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="deviceForm" label-well-code="right" label-width="120px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="devcode">
            <el-input v-model.trim="deviceForm.devcode" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备唯一标识" prop="nodeid">
            <el-input v-model.trim="deviceForm.nodeid" :disabled="isEditMode" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备名称" prop="name">
            <el-input v-model.trim="deviceForm.name" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="验证码">
            <el-input v-model.trim="deviceForm.nodeid" disabled type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备类型" prop="deviceType">
            <el-input v-model.trim="deviceForm.deviceType" type="text" placeholder="必填" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备型号" prop="model">
            <el-input v-model.trim="deviceForm.model" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="厂商ID" prop=".manufacturerId">
            <el-input v-model.trim="deviceForm.manufacturerId" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="厂商名称" prop="manufactureName">
            <el-input v-model.trim="deviceForm.manufacturerName" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备协议类型" prop="protocalType">
            <el-input v-model.trim="deviceForm.protocalType" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备安全模式" prop="isSecure">
            <el-input v-model.trim="deviceForm.isSecure" :disabled="isEditMode" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="超时时间" prop="timeout">
            <el-input v-model.trim="deviceForm.timeout" :disabled="isEditMode" type="text" placeholder="非必填" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="saveBtnDisable" type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="cancel">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDevice, updateDevice } from '@/api/device/nbdevice'

export default {
  name: 'EditNbDevice',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      deviceForm: {
        id: '', // id
        nbDeviceId: '', // nb设备编号
        devcode: '', // 厂家设备编号
        verifyCode: '', // 模块IMEI号
        nodeid: '', // 设备唯一标识
        timeout: 0, // 超时时间
        name: '', // 设备名称
        manufacturerId: 'birmm', // 厂商id
        manufacturerName: 'birmm', // 厂商名称
        deviceType: 'BIRMM', // 设备类型
        model: 'birmm-nb', // 设备型号
        protocalType: 'CoAP', // 设备协议类型
        isSecure: 'FALSE'// 设备安全模式
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      deviceTypeList: null, // 设备类型列表
      deviceModelList: null, // 设备型号列表
      showDeviceType: true, // 是否显示设备类型选项
      showModelType: true, // 是否显示型号选项
      isEditMode: false,
      saveBtnDisable: false,
      rules: {
        name: [{ required: true, message: '设备名称不能为空', trigger: ['blur', 'change'] }],
        devcode: [{ required: true, message: '设备编号不能为空', trigger: ['blur', 'change'] }],
        nodeid: [{ required: true, message: '设备唯一标识必填', trigger: ['blur', 'change'] }],
        verifyCode: [{ required: true, message: '验证码必填', trigger: ['blur', 'change'] }]
      } // 前端校验规则
    }
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus
      this.dialogFormVisible = dialogFormVisible
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
        this.isEditMode = false
      } else if (dialogStatus === 'update') { // 如果是修改，将row中数据填写到输入框中
        this.deviceForm = {
          id: row.id, // id
          nbDeviceId: row.nbDeviceId, // nb设备编号
          devcode: row.devcode, // 厂家设备编号
          verifyCode: row.verifyCode, // 模块IMEI号
          nodeid: row.nodeid, // 设备唯一标识
          timeout: row.timeout, // 超时时间
          name: row.name, // 设备名称
          manufacturerId: row.manufacturerId, // 厂商id
          manufacturerName: row.manufacturerName, // 厂商名称
          deviceType: row.deviceType, // 设备类型
          model: row.model, // 设备型号
          protocalType: row.protocalType, // 设备协议类型
          isSecure: row.isSecure// 设备安全模式
        }
        this.isEditMode = true
      }
    },
    // 重置表单
    resetForm() {
      this.deviceForm = {
        id: '', // id
        nbDeviceId: '', // nb设备编号
        devcode: '', // 厂家设备编号
        verifyCode: '', // 模块IMEI号
        nodeid: '', // 设备唯一标识
        timeout: 0, // 超时时间
        name: '', // 设备名称
        manufacturerId: 'birmm', // 厂商id
        manufacturerName: 'birmm', // 厂商名称
        deviceType: 'BIRMM', // 设备类型
        model: 'birmm-nb', // 设备型号
        protocalType: 'CoAP', // 设备协议类型
        isSecure: 'FALSE'// 设备安全模式
      }
    },
    // 保存数据
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        this.deviceForm.verifyCode = this.deviceForm.nodeid
        if (valid) {
          this.saveBtnDisable = true
          addDevice(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.saveBtnDisable = false
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
                this.saveBtnDisable = false
              })
            }
          }).catch(_ => {
            this.saveBtnDisable = false
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateDevice(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
