<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body width="40%">
    <el-form ref="dataForm" :model="UserDeviceForm" :rules="rules" label-width="100px">
      <el-form-item label="设备名称" prop="deviceName">
        <el-input v-model="UserDeviceForm.deviceName" clearable/>
      </el-form-item>
      <el-form-item label="设备IMEI号" prop="imei">
        <el-input v-model="UserDeviceForm.imei" clearable/>
      </el-form-item>
      <el-form-item label="设备入网号" prop="networkNumber">
        <el-input v-model="UserDeviceForm.networkNumber" clearable/>
      </el-form-item>
      <el-form-item label="用户名称" prop="userId">
        <el-select v-model="UserDeviceForm.userId" filterable clearable style="width:100%">
          <el-option v-for="user in userList" :key="user.id" :label="user.name" :value="user.id"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">保存</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUserList, addUserDevice, updateUserDevice } from '@/api/busAdmin/userDevice'

export default {
  name: 'EditUserDevice',
  data() {
    return {
      dialogFormVisible: false,
      dialogStatus: '',
      UserDeviceForm: {
        id: '',
        deviceName: '',
        imei: '',
        networkNumber: '',
        userId: ''
      },
      textMap: {
        update: '编辑',
        create: '新增'
      },
      userList: [],
      rules: {
        deviceName: [{ required: true, message: '设备名称不能为空', trigger: ['blur', 'change'] }],
        imei: [{ required: true, message: '设备IMEI号不能为空', trigger: ['blur', 'change'] }],
        networkNumber: [{ required: true, message: '设备入网号不能为空', trigger: ['blur', 'change'] }],
        userId: [{ required: true, message: '用户名称不能为空', trigger: ['blur', 'change'] }]
      }
    }
  },
  mounted() {
    this.fetchUserList()
  },
  methods: {
    fetchUserList() {
      getUserList().then(response => {
        this.userList = response.data
      })
    },
    initDialog(dialogFormVisible, dialogStatus, row = null) {
      this.dialogFormVisible = dialogFormVisible
      this.dialogStatus = dialogStatus
      if (this.dialogStatus === 'create') {
      // 新增时，清空表格和校验
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (this.dialogStatus === 'update') {
      // 更新时，填充数据
        console.log(row)
        this.UserDeviceForm.id = row.id
        this.UserDeviceForm.deviceName = row.deviceName
        this.UserDeviceForm.imei = row.imei
        this.UserDeviceForm.networkNumber = row.networkNumber
        this.UserDeviceForm.userId = row.userId
      }
    },
    resetForm() {
      this.UserDeviceForm = {
        id: '',
        deviceName: '',
        imei: '',
        networkNumber: '',
        userId: ''
      }
    },
    cancel() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    saveData() {
      if (this.dialogStatus === 'create') {
        this.createData()
      } else if (this.dialogStatus === 'update') {
        this.updateData()
      }
    },
    createData() {
      console.log('create data')
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          console.log('create valid success')
          console.log(this.UserDeviceForm)
          addUserDevice(this.UserDeviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('添加成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            } else {
              this.$message.error('添加失败')
            }
          })
        } else {
          console.log('create valid error')
        }
      })
    },
    updateData() {
      console.log('update data')
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          console.log('update valid success')
          console.log(this.UserDeviceForm)
          updateUserDevice(this.UserDeviceForm).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            } else {
              this.$message.error('修改失败')
            }
          })
        } else {
          console.log('update valid error')
        }
      })
    }

  }
}
</script>

<style scoped>
.el-form-item {
  margin-bottom: 22px;
  margin-left: 20px;
  margin-right: 50px;
}
</style>
