<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
  >
    <div>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="100px" label-position="right">
        <el-form-item label="类别名称" prop="sensorTypeName">
          <el-input v-model="formData.sensorTypeName" placeholder="输入设备类别名称" />
        </el-form-item>
        <el-form-item label="说明">
          <el-input v-model="formData.sensorCatDesc" type="textarea" rows="5" placeholder="输入说明" />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ACTION_ADD, ACTION_UPDATE } from '@/utils/actionType'
import { createSensorCat, updateSensorCat } from '@/api/goaf/sensorCatApi'

export default {
  name: 'SensorCat',
  props: {
    title: {
      type: String,
      default: ''
    }
  },

  data() {
    return {
      dialogVisible: false,
      actionType: '',
      formData: {
        sensorTypeId: undefined,
        sensorTypeName: '',
        sensorCatDesc: ''
      },
      rules: {
        sensorTypeName: [
          { required: true, message: '请填写名称', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    formSuccess() {
      this.$emit('formSuccess')
    },

    // Show Add Model
    showAddModel() {
      this.actionType = ACTION_ADD
      this.resetFormData()
      this.dialogVisible = true
    },

    // Show Edit Model
    showEditModel(data) {
      this.actionType = ACTION_UPDATE
      this.resetFormData()
      this.dialogVisible = true
      this.formData = data
    },

    // Reset Form Data
    resetFormData() {
      this.formData = {
        sensorTypeId: undefined,
        sensorTypeName: '',
        sensorCatDesc: ''
      }
    },

    // Submit
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleActionCommand()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

    // Handle ACTION Command
    handleActionCommand() {
      const actionType = this.actionType
      switch (actionType) {
        case ACTION_ADD:
          createSensorCat(this.formData).then((resp) => {
            const { code, msg } = resp
            if (code === 0) {
              this.dialogVisible = false
              this.$message.success(msg)
              this.formSuccess()
            } else {
              this.$message.error(msg)
            }
          })
          break

        case ACTION_UPDATE:
          updateSensorCat(this.formData).then((resp) => {
            console.log('this.formData', this.formData)
            const { code, msg } = resp
            if (code === 0) {
              this.dialogVisible = false
              this.$message.success(msg)
              this.formSuccess()
            } else {
              this.$message.error(msg)
            }
          })
          break
      }
    }
  }
}
</script>
