<template>
  <div class="equipment-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="productSn" :rules="[{
                    required: true, message: '产品序列号不能为空', trigger: 'blur'
                  },{min: 1, max: 50, message: '长度在1 到 50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.productSn" size="small" placeholder="请输入产品序列号"/>
      </el-form-item>
      <el-form-item label="飞控序列号" prop="flightSn" :rules="[
      {min: 0, max: 50, message: '最多50 个字符', trigger: 'blur'}]">
        <el-input maxlength="50" clearable v-model="ruleForm.flightSn" 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="registerTime">
        <el-date-picker
          placeholder="选择注册时间"
          size="small"
          clearable
          value-format="yyyy-MM-dd"
          v-model="ruleForm.registerTime"
          type="date">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="直播地址" prop="liveUrl">
        <el-input clearable v-model="ruleForm.liveUrl" size="small" placeholder="请输入直播地址"/>
      </el-form-item>
      <el-form-item label="任务类型" prop="taskType" :rules="[{
                    required: true, message: '任务类型不能为空', trigger: 'change'
                  }]">
        <el-select  clearable multiple  v-model="ruleForm.taskType" size="small"
                   placeholder="请选择任务类型">
          <el-option v-for="(value,key,index) in taskTypeList" :key="key" :label="value" :value="key"/>
        </el-select>
      </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 equipmentApi from '@/api/basicData/equipment'

  export default {
    name: "equipmentForm",
    data() {
      return {
        addBtnLoading: false
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      typeList: {
        type: Object,
        default() {
          return {};
        }
      },
      taskTypeList: {
        type: Object,
        default() {
          return {};
        }
      },
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'addDriver'
            if (this.ruleForm.id) {
              method = 'editDriver'
            }
            let ruleForm=JSON.parse(JSON.stringify(this.ruleForm))
            // if(ruleForm.registerTime){
            //   ruleForm.registerTime=new Date(ruleForm.registerTime).getTime() / 1000
            // }else{
            //   ruleForm.registerTime = ''
            // }
            if(ruleForm&&ruleForm.taskType.length>0){
              ruleForm.taskType=ruleForm.taskType.join(',')
            }
            this.addBtnLoading = true
            equipmentApi[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">
  .equipment {

  }
</style>
