<template>
  <el-col :span="16" :offset="6">
    <el-form label-width="100px" ref="formRef" :model="form">
      <el-form-item label="审批建议" prop="reason" style="margin-bottom: 20px">
        <el-input
          type="textarea"
          v-model="form.reason"
          placeholder="请输入审批建议"
        />
      </el-form-item>
    </el-form>
    <div style="margin-left: 10%; margin-bottom: 20px; font-size: 14px">
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        :loading="isSaveLoading"
        @click="submit"
        v-if="currentBtn === '上交'"
        >上交
      </el-button>
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        :loading="isSaveLoading"
        @click="submit"
        v-if="currentBtn === '接收'"
        >接收
      </el-button>
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        :loading="isSaveLoading"
        @click="submit"
        v-if="currentBtn === '检查'"
        >已检
      </el-button>
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        :loading="isSaveLoading"
        @click="submit"
        v-if="currentBtn === '发出'"
        >发出
      </el-button>
      <el-button
        icon="el-icon-edit-outline"
        type="success"
        size="mini"
        :loading="isSaveLoading"
        @click="submit(1)"
        v-if="['user_receive'].includes(this.taskDefinitionKey)"
        >报工
      </el-button>
      <!-- <el-button
        icon="el-icon-edit-outline"
        type="danger"
        size="mini"
        :loading="isSaveLoading"
        @click="submit(0)"
        v-if="['user_receive'].includes(this.taskDefinitionKey)"
        >未收
      </el-button> -->
    </div>
  </el-col>
</template>

<script>
  import { EventBus } from './eventBus';
  import {
    getList,
    surrender
  } from '@/api/bpm/components/inspectionManage/index.js';
  export default {
    name: 'measuringToolInspection_submit',
    props: {
      businessId: {
        default: ''
      },
      taskId: {
        default: ''
      },
      id: {
        default: ''
      },
      taskDefinitionKey: {
        default: ''
      }
    },
    data() {
      return {
        isSaveLoading: false,
        // 0默认; 10使用人上交; 20班组收到; 21班组上交; 30负责人收; 31负责人发出; 32负责人收;
        // 33负责人检查; 34负责人发; 40班组长收; 41班组长发; 50使用人已收; 51使用人未收
        toolsStatus: 0,
        currentBtn: '',
        form: {
          reason: ''
        }
      };
    },
    async mounted() {
      EventBus.$on('getData', ({ logList }) => {
        console.log('------mounted----------');
        console.log(logList);
        let currentBtn = '';
        switch (this.taskDefinitionKey) {
          // 使用人上交
          case 'user_submit':
            currentBtn = '上交';
            break;
          // 负责人收
          case 'team_leader_receive':
            currentBtn = '接收';
            break;
          // 负责人发
          case 'team_leader_submit':
            currentBtn = '发出';
            break;
          // 量具管理员收
          case 'leader_receive':
            currentBtn = '接收';
            break;
          // 量具管理员发
          case 'leader_send':
            currentBtn = '发出';
            break;
          // 负责人收
          case 'team_leader_receive1':
            currentBtn = '接收';
            break;
          // 负责人发
          case 'team_leader_send':
            currentBtn = '发出';
            break;
          // 使用人收
          case 'user_receive':
            break;
        }
        this.currentBtn = currentBtn;
      });
    },
    destroyed() {
      EventBus.$off('getData');
    },
    methods: {
      async submit(boolen) {
        // if (!this.form.reason) {
        //   return this.$message.error('请输入审批意见');
        // }
        let data = await this.getTableValue();
        let status = 10;
        let isSubmit = 0;
        switch (this.taskDefinitionKey) {
          // 使用人上交
          case 'user_submit':
            status = 10;
            break;
          // 负责人收
          case 'team_leader_receive':
            status = 20;
            break;
          // 负责人发
          case 'team_leader_submit':
            status = 21;
            break;
          // 量具管理员收
          case 'leader_receive':
            status = 30;
            break;
          // 量具管理员发
          case 'leader_send':
            status = 31;
            break;
          // 负责人收
          case 'team_leader_receive1':
            status = 40;
            break;
          // 负责人发
          case 'team_leader_send':
            status = 41;
            break;
          // 使用人收
          case 'user_receive':
            if (boolen) {
              // 已收
              status = 50;
            } else {
              // 未收
              status = 51;
            }
            break;
        }
        console.log(data);
        if (
          this.taskDefinitionKey === 'leader_receive' &&
          !data.planDeviceList[0].leaderVerifyTime
        ) {
          this.$message.error('请先确认收到');
          return false;
        }
        if (
          this.taskDefinitionKey === 'team_leader_receive1' &&
          !data.planDeviceList[0].teamVerifyTime
        ) {
          this.$message.error('请先确认收到');
          return false;
        }
        let params = {
          bpmTaskId: this.taskId,
          bpmTaskReason: this.form.reason,
          node: this.taskDefinitionKey,
          status,
          isSubmit,
          planId: data.planId
        };
        if (status === 33 || status >= 50) {
          console.log(data.planDeviceList);
          let boolen = data.planDeviceList.every(
            (item) => item.qualityStatus !== null
          );
          console.log(boolen);
          if (boolen) {
            params.planDeviceToolsList = data.planDeviceList.map((item) => {
              return {
                deviceId: item.deviceId,
                qualityStatus: item.qualityStatus,
                isLose: item.isLose
              };
            });
          } else {
            this.$message.error('请完成质检');
            return false;
          }
        } else if (status === 10) {
          console.log(data.planDeviceList);
          let boolen = data.planDeviceList.every(
            (item) => item.isLose !== null
          );
          console.log(boolen);
          if (boolen) {
            params.planDeviceToolsList = data.planDeviceList.map((item) => {
              return {
                deviceId: item.deviceId,
                qualityStatus: item.qualityStatus,
                isLose: item.isLose
              };
            });
          } else {
            this.$message.error('设备是否遗失');
            return false;
          }
        }
        console.log(params);
        surrender(params).then(() => {
          this.$emit('handleAudit', {
            status: 1,
            title: '提交'
          });
        });
      },
      async storeManagerApprove() {
        let res = await this.getTableValue();
        console.log(res);
        // let storageData = res.returnStorageData;
        // // 入库来源storageSource 0-正常  1-外部(外部跳过内部审核流程)
        // storageData.storageSource = 1;
        // console.log(storageData);
        // try {
        //   this.isSaveLoading = true;
        //   await outin.save(storageData);
        //   approveTaskWithVariables({
        //     id: this.taskId,
        //     reason: this.form.reason,
        //     variables: {
        //       pass: true
        //     }
        //   }).then((res) => {
        //     if (res.code != '-1') {
        //       this.$emit('handleAudit', {
        //         status: 1,
        //         title: '入库'
        //       });
        //     }
        //     this.isSaveLoading = false;
        //   });
        // } catch (error) {
        //   this.isSaveLoading = false;
        //   this.$message.error('保存失败');
        // }
      },
      /** 处理转办审批人 */
      handleUpdateAssignee() {
        this.$emit('handleUpdateAssignee');
      },
      /** 退回 */
      handleBackList() {
        this.$emit('handleBackList');
      },
      getTableValue() {
        return new Promise((resolve) => {
          this.$emit('getTableValue', async (data) => {
            resolve(await data);
          });
        });
      }
    }
  };
</script>

<style lang="scss"></style>
