<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="100px"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="设备编号" prop="devcode">
            <el-input
              v-model.trim="deviceForm.devcode"
              clearable
              :disabled="dialogStatus === 'create' ? false : true"
              type="text"
              placeholder="必填"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="IMEI号" prop="imei">
            <el-input
              v-model.trim="deviceForm.imei"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
              type="text"
              placeholder="必填"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="SIM卡号" prop="iccid">
            <el-input
              v-model.trim="deviceForm.iccid"
              clearable
              :disabled="dialogStatus === 'detail' ? true : false"
              type="text"
              placeholder="必填"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div
      v-show="dialogStatus === 'detail' ? false : true"
      slot="footer"
      class="dialog-footer"
    >
      <el-button type="primary" @click="saveData">
        保存
      </el-button>
      <el-button @click="dialogFormVisible = false">
        取消
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addDeviceImei, updateDeviceImei } from '@/api/device/deviceImei'
export default {
  name: 'EditDeviceImei',
  data() {
    return {
      dialogFormVisible: false, // 对话框显示隐藏
      dialogStatus: '', // 对话框类型：create,update,detail
      // 对话框标题
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      },
      // 表单内容
      deviceForm: {
        devcode: '',
        imei: '',
        iccid: ''
      },
      // 表单校验规则
      rules: {
        devcode: [{ required: true, message: '设备编号必填', trigger: ['blur', 'change'] }],
        imei: [{ required: true, message: 'IMEI号必填', trigger: ['blur', 'change'] }],
        iccid: [{ required: true, message: 'SIM卡号必填', trigger: ['blur', 'change'] }]
      }
    }
  },
  methods: {
    // 重置表单
    resetForm() {
      this.deviceForm = {
        devcode: '',
        imei: '',
        iccid: ''
      }
    },
    // 父子组件通信
    initDialog(dialogStatus, row = null) {
      console.log(row, '====21==')
      this.dialogFormVisible = true
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update' || this.dialogStatus === 'detail') {
        console.log(typeof row.communication)
        this.deviceForm = {
          devcode: row.devcode,
          imei: row.imei,
          iccid: row.iccid
        }
      }
    },
    // 保存数据
    saveData() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 修改数据
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.deviceForm.communication = this.deviceForm.communication.toString()
          updateDeviceImei(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        console.log(this.deviceForm)
        if (valid) {
          this.deviceForm.communication = this.deviceForm.communication.toString()
          addDeviceImei(this.deviceForm).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild') // 刷新父组件数据列表
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    }
  }
}
</script>
