<!--
 * @Author: gtz
 * @Date: 2022-03-04 10:22:13
 * @LastEditors: gtz
 * @LastEditTime: 2022-03-05 12:09:38
 * @Description: file content
 * @FilePath: \mt-qj-wms-ui\src\views\common\order-auto-submit.vue
-->

<template>
  <el-dialog
    :title="!row ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" v-loading="formLoading" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="自动任务类型" prop="autoTaskType">
        <el-select v-model="dataForm.autoTaskType" clearable @change="changeAutoTaskType">
          <el-option :value="1" label="淡化炉加工"></el-option>
          <el-option :value="2" label="多功能炉加工"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="加工类型" prop="processType">
        <el-select v-model="dataForm.processType" clearable>
          <el-option :value="0" label="初始加工" />
          <el-option :value="1" label="复加工" />
        </el-select>
      </el-form-item>
      <el-form-item v-if="dataForm.autoTaskType === 2" label="开始加工炉">
        <el-form-item label="加工起点" prop="startPosition" style="margin-bottom: 24px">
          <el-select v-model="dataForm.startPosition" :disabled="!(dataForm.autoTaskType)">
            <el-option value="YYT003" label="液压台3"></el-option>
            <el-option value="YYT004" label="液压台4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加工炉" prop="firstPosition" style="margin-bottom: 24px">
          <el-select v-model="dataForm.firstPosition" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in kilnList" :key="item.id" :value="item.id + ',' + item.code" :label="item.kilnName" />
          </el-select>
        </el-form-item>
        <el-form-item label="工艺" prop="firstProcess" style="margin-bottom: 24px">
          <el-select v-model="dataForm.firstProcess" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in processList" :key="item.id" :value="item.code" :label="item.craftCode" />
          </el-select>
        </el-form-item>
      </el-form-item>
      <el-form-item label="清洗炉">
        <el-form-item label="加工起点" v-if="dataForm.autoTaskType !== 2" prop="startPosition" style="margin-bottom: 24px">
          <el-select v-model="dataForm.startPosition" :disabled="!(dataForm.autoTaskType)">
            <el-option value="YYT003" label="液压台3"></el-option>
            <el-option value="YYT004" label="液压台4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="加工炉" prop="secPosition" style="margin-bottom: 24px">
          <el-select v-model="dataForm.secPosition" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in kilnList" :key="item.id" :value="item.id + ',' + item.code" :label="item.kilnName" />
          </el-select>
        </el-form-item>
        <el-form-item label="工艺" prop="secProcess" style="margin-bottom: 24px">
          <el-select v-model="dataForm.secProcess" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in processList" :key="item.id" :value="item.code" :label="item.craftCode" />
          </el-select>
        </el-form-item>
      </el-form-item>
      <el-form-item label="结束加工炉">
        <el-form-item label="加工炉" prop="thirdPosition" style="margin-bottom: 24px">
          <el-select v-model="dataForm.thirdPosition" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in kilnList" :key="item.id" :value="item.id + ',' + item.code" :label="item.kilnName" />
          </el-select>
        </el-form-item>
        <el-form-item label="工艺" prop="thirdProcess" style="margin-bottom: 24px">
          <el-select v-model="dataForm.thirdProcess" :disabled="!(dataForm.autoTaskType)">
            <el-option v-for="item in processList" :key="item.id" :value="item.code" :label="item.craftCode" />
          </el-select>
        </el-form-item>
      </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 {
    props: {
      kilnList: {
        type: Array,
        default: () => []
      },
      processList: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        visible: false,
        dataForm: {
          autoTaskType: null,
          processType: null,
          startPosition: null,
          firstPosition: null,
          firstProcess: null,
          secPosition: null,
          secProcess: null,
          thirdPosition: null,
          thirdProcess: null
        },
        dataRule: {
          autoTaskType: [
            { required: true, message: '自动任务类型不能为空', trigger: 'blur' }
          ],
          processType: [
            { required: true, message: '加工类型不能为空', trigger: 'blur' }
          ],
          startPosition: [
            { required: true, message: '加工起点不能为空', trigger: 'blur' }
          ],
          firstPosition: [
            { required: true, message: '加工炉不能为空', trigger: 'blur' }
          ],
          firstProcess: [
            { required: true, message: '工艺不能为空', trigger: 'blur' }
          ],
          secPosition: [
            { required: true, message: '加工炉不能为空', trigger: 'blur' }
          ],
          secProcess: [
            { required: true, message: '工艺不能为空', trigger: 'blur' }
          ],
          thirdPosition: [
            { required: true, message: '加工炉不能为空', trigger: 'blur' }
          ],
          thirdProcess: [
            { required: true, message: '工艺不能为空', trigger: 'blur' }
          ]
        },
        dataList: [],
        formLoading: false,
        row: null,
        editIndex: null
      }
    },
    methods: {
      init (list) {
        this.dataList = list
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      },
      changeAutoTaskType () {
        this.dataForm.startPosition = null
        this.dataForm.firstPosition = null
        this.dataForm.firstProcess = null
        this.dataForm.secPosition = null
        this.dataForm.secProcess = null
        this.dataForm.thirdPosition = null
        this.dataForm.thirdProcess = null
      },
      setIdenCardNum () {
        this.formLoading = true
        if (this.dataForm.idenCardNum) {
          this.$http({
            url: this.$http.adornUrl(`/orderInfo/getTaskInfoByIdenCardNum`),
            method: 'get',
            params: this.$http.adornParams({
              'idenCardNum': this.dataForm.idenCardNum
            })
          }).then(({data}) => {
            console.log(data)
            if (data && data.code === 0) {
              this.dataForm = data.data
              this.dataForm.targetQuantity = data.data.quantity
              this.dataForm.targetWeight = data.data.weight
              this.dataForm.weight = null
              this.dataForm.quantity = null
            } else {
              this.$message.warning(data.msg)
            }
            this.formLoading = false
          })
        } else {
          this.$message.warning('请输入标识卡号')
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const requestData = {
              taskCreateParamList: []
            }
            if (this.dataForm.autoTaskType === 1) {
              requestData.taskCreateParamList.push({
                autoTaskStep: 1,
                autoTaskType: this.dataForm.autoTaskType,
                craftCode: this.dataForm.secProcess,
                detParams: this.dataList,
                kilnId: this.dataForm.secPosition.split(',')[0],
                processType: this.dataForm.processType,
                startPosition: this.dataForm.startPosition,
                targetPosition: this.dataForm.secPosition.split(',')[1]
              })
              requestData.taskCreateParamList.push({
                autoTaskStep: 2,
                autoTaskType: this.dataForm.autoTaskType,
                craftCode: this.dataForm.thirdProcess,
                detParams: this.dataList,
                kilnId: this.dataForm.thirdPosition.split(',')[0],
                processType: this.dataForm.processType,
                startPosition: this.dataForm.secPosition.split(',')[1],
                targetPosition: this.dataForm.thirdPosition.split(',')[1]
              })
            } else if (this.dataForm.autoTaskType === 2) {
              requestData.taskCreateParamList.push({
                autoTaskStep: 1,
                autoTaskType: this.dataForm.autoTaskType,
                craftCode: this.dataForm.secProcess,
                detParams: this.dataList,
                kilnId: this.dataForm.firstPosition.split(',')[0],
                processType: this.dataForm.processType,
                startPosition: this.dataForm.startPosition,
                targetPosition: this.dataForm.firstPosition.split(',')[1]
              })
              requestData.taskCreateParamList.push({
                autoTaskStep: 2,
                autoTaskType: this.dataForm.autoTaskType,
                craftCode: this.dataForm.secProcess,
                detParams: this.dataList,
                kilnId: this.dataForm.secPosition.split(',')[0],
                processType: this.dataForm.processType,
                startPosition: this.dataForm.firstPosition.split(',')[1],
                targetPosition: this.dataForm.secPosition.split(',')[1]
              })
              requestData.taskCreateParamList.push({
                autoTaskStep: 3,
                autoTaskType: this.dataForm.autoTaskType,
                craftCode: this.dataForm.thirdProcess,
                detParams: this.dataList,
                kilnId: this.dataForm.thirdPosition.split(',')[0],
                processType: this.dataForm.processType,
                startPosition: this.dataForm.secPosition.split(',')[1],
                targetPosition: this.dataForm.thirdPosition.split(',')[1]
              })
            }
            this.$http({
              url: this.$http.adornUrl(`/currTask/createAutoTask`),
              method: 'post',
              data: this.$http.adornData(requestData)
            }).then(({data}) => {
              console.log(data)
              if (data && data.code === 0) {
                this.$message.success('任务提交成功')
                this.dataList = []
                sessionStorage.setItem('autoTaskList', '')
                this.$emit('refreshDataList')
              } else {
                this.$message.warning(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
