<!--
 * @Author: zwq
 * @Date: 2021-11-18 14:16:25
 * @LastEditors: zwq
 * @LastEditTime: 2022-09-09 16:26:17
 * @Description:
-->
<template>
  <el-dialog title="修改"
      :before-close="handleClose"
       :visible.sync="visible">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="加工炉" prop="kilnId">
        <el-select
          v-model="dataForm.kilnId"
          filterable
          placeholder="请选择加工炉"
        >
          <el-option
            v-for="item in kilnInfoArr"
            :key="item.id"
            :label="item.kilnName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="dataForm.status"
          filterable
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in statusList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :loading="btnLoad" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    kilnInfoArr: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      visible: false,
      btnLoad: false,
      dataForm: {
        id: '',
        kilnId: '',
        status: ''
      },
      statusList: [
        {
          value: 0,
          label: '等待执行'
        },
        {
          value: 1,
          label: '执行中'
        },
        {
          value: 2,
          label: '执行完成'
        }
      ],
      dataRule: {
        kilnId: [
          { required: true, message: '加工炉不能为空', trigger: 'change' }
        ],
        status: [{ required: true, message: '状态不能为空', trigger: 'change' }]
      }
    }
  },
  methods: {
    init (id, status, kilnId) {
      this.btnLoad = false
      this.dataForm = {
        id: '',
        kilnId: '',
        status: ''
      }
      this.dataForm.id = id
      this.dataForm.status = status
      this.dataForm.kilnId = kilnId
      this.visible = true
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.btnLoad = true
          this.$http({
            url: this.$http.adornUrl('/currTask/updateCurrTaskStatus'),
            method: 'post',
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              kilnId: this.dataForm.kilnId,
              status: this.dataForm.status
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.btnLoad = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.btnLoad = false
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    handleClose () {
      this.visible = false
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>
