<template>
  <el-form ref="form" :rules="rules" label-width="150px" :model="form">
    <el-form-item label="产线" prop="proLineId">
      <el-select
        v-model="form.proLineId"
        placeholder="产线"
        style="width: 100%"
        @change="selectLine"
      >
        <el-option
          v-for="item in proLineIdList"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="吸附垫寿命上限值" prop="adsorptionPadCeiling">
      <el-input-number
        v-model="form.adsorptionPadCeiling"
        :min="0"
        :max="9999999999"
        style="width: 100%"
      ></el-input-number>
    </el-form-item>
    <el-form-item label="辅助边框寿命上限值" prop="auxiliaryFrameCeiling">
      <el-input-number
        v-model="form.auxiliaryFrameCeiling"
        :min="0"
        :max="9999999999"
        style="width: 100%"
      ></el-input-number>
    </el-form-item>
  </el-form>
</template>
<script>
import { setAlarmValueGet, setAlarmValue } from '@/api/consumablesManagement'
export default {
  name: 'alarmAdd',
  data() {
    return {
      form: {
        proLineId: '',
        adsorptionPadCeiling: 0,
        auxiliaryFrameCeiling: 0
      },
      rules: {
        proLineId: [
          { required: true, message: '请选择产线', trigger: 'change' }
        ],
        adsorptionPadCeiling: [
          { required: true, message: '请输入吸附垫寿命上限值', trigger: 'blur' }
        ],
        auxiliaryFrameCeiling: [
          {
            required: true,
            message: '请输入辅助边框寿命上限值',
            trigger: 'blur'
          }
        ]
      },
      proLineIdList: JSON.parse(localStorage.getItem('publicList'))
        .proLineVoList
    }
  },
  methods: {
    selectLine(val) {
      this.form.proLineId = val
      setAlarmValueGet({ id: val }).then((res) => {
        if (res.code === 0 && res.data) {
          this.form.adsorptionPadCeiling = res.data.adsorptionPadCeiling
            ? res.data.adsorptionPadCeiling
            : 0
          this.form.auxiliaryFrameCeiling = res.data.auxiliaryFrameCeiling
            ? res.data.auxiliaryFrameCeiling
            : 0
        } else {
          this.form.adsorptionPadCeiling = 0
          this.form.auxiliaryFrameCeiling = 0
        }
      })
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          setAlarmValue({ ...this.form }).then((res) => {
            if (res.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.$emit('successSubmit')
            }
          })
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
    }
  }
}
</script>
