<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: gtz
 * @LastEditTime: 2022-03-05 12:03:46
 * @Description:
-->
<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" v-loading="formLoading" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="工艺编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="工艺编码"></el-input>
      </el-form-item>
      <el-form-item label="工艺号" prop="craftCode">
        <el-input v-model="dataForm.craftCode" placeholder="工艺号"></el-input>
      </el-form-item>
      <el-form-item label="对应PLC值" prop="plcValue">
        <el-input v-model="dataForm.plcValue" placeholder="对应PLC值"></el-input>
      </el-form-item>
      <!-- <el-form-item label="当前状态" prop="alarmInfo"
        ><el-switch
          v-model="dataForm.alarmInfo"
          active-color="#13ce66"
          inactive-color="#ff4949"
          active-value="1"
          inactive-value="0"
        >
        </el-switch>
      </el-form-item> -->
      <el-form-item label="说明" prop="content">
        <el-input v-model="dataForm.content" placeholder="说明"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: '',
          code: '',
          content: '',
          plcValue: '',
          craftCode: ''
        },
        dataRule: {
          code: [
            { required: true, message: '工艺编码不能为空', trigger: 'blur' }
          ],
          craftCode: [
            { required: true, message: '工艺号不能为空', trigger: 'blur' }
          ],
          plcValue: [
            { required: true, message: '对应PLC值不能为空', trigger: 'blur' }
          ]
        },
        formLoading: false
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.formLoading = true
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/craftInfo/get`),
              method: 'post',
              data: this.$http.adornData({id})
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data
              } else {
                this.$message.error(data.msg)
              }
              this.formLoading = false
            })
          } else {
            this.$http({
              url: this.$http.adornUrl(`/craftInfo/codeGenerator`),
              method: 'post',
              data: this.$http.adornData()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.data
              } else {
                this.$message.error('编码生成失败')
              }
              this.formLoading = false
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/craftInfo/${!this.dataForm.id ? 'add' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'content': this.dataForm.content,
                'plcValue': this.dataForm.plcValue,
                'craftCode': this.dataForm.craftCode
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
