<template>
  <div class="mountEquipment-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="180px">
      <el-form-item label="品牌" prop="brand" :rules="{
                    required: true, message: '品牌不能为空', trigger: 'blur'
                  }">
        <el-input maxlength="50" clearable v-model="ruleForm.brand" size="small" placeholder="请输入品牌"/>
      </el-form-item>
      <el-form-item label="型号" prop="model" :rules="[
        {min: 0, max: 50, message: '最多50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.model" size="small" placeholder="请输入型号"/>
      </el-form-item>
      <el-form-item label="设备名称" prop="name" :rules="[{
                    required: true, message: '设备名称不能为空', trigger: 'blur'
                  },{min: 1, max: 50, message: '长度在1 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.name" size="small" placeholder="请输入设备名称"/>
      </el-form-item>
      <el-form-item label="设备类型" prop="type" :rules="{
                    required: true, message: '设备类型不能为空', trigger: 'change'
                  }">
        <el-select  clearable v-model="ruleForm.type" size="small"
                   placeholder="请选择设备类型">
          <el-option v-for="(value,key,index) in typeList" :key="key" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
        {min: 0, max: 255, message: '最多255 个字符', trigger: 'blur'}]">
        <el-input show-word-limit maxlength="255" clearable type="textarea" rows="5" v-model="ruleForm.note" size="small" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item label="状态" props="status">
        <span v-show="ruleForm.status == 1" style="color: #666">在线</span>
        <span v-show="ruleForm.status == 2" style="color: #666">离线</span>
        <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="2" size="small"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium" @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import mountEquipmentApi from '@/api/basicData/mountEquipment'

  export default {
    name: "mountEquipmentForm",
    data() {
      return {
        addBtnLoading: false
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      typeList: {
        type: Object,
        default() {
          return {};
        }
      },
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'mountEquipmentAdd'
            if (this.ruleForm.id) {
              method = 'mountEquipmentEdit'
            }
            let ruleForm=JSON.parse(JSON.stringify(this.ruleForm))
            if(ruleForm.registerTime){
              ruleForm.registerTime=new Date(ruleForm.registerTime).getTime() / 1000
            }else{
              ruleForm.registerTime = ''
            }
            this.addBtnLoading = true
            mountEquipmentApi[method](ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.$emit('submit',2)
                this.$emit('onBack')
              }else{
                this.$emit('submit',1)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .mountEquipment {

  }
</style>
