<template>
  <view class="plan-approval-container">
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      left-icon="back"
      title="申领单审批详情"
      @clickLeft="back"
    ></uni-nav-bar>
    <view class="content">
      <!-- 基本信息 -->
      <view class="baseinfo">
        <view class="col">
          <text class="label">申领单号</text>
          <text class="desc">{{ baseInfo.code }}</text>
        </view>
        <view class="col">
          <text class="label">来源类型</text>
          <text class="desc"
            >{{
              baseInfo.sourceType == 1 ? '保养工单' : '维修工单'
            }}-申请备品备件</text
          >
        </view>
        <view class="col">
          <text class="label">来源工单号</text>
          <text class="desc">{{ baseInfo.sourceCode }}</text>
        </view>
        <view class="col">
          <text class="label">期望完成时间</text>
          <text class="desc">{{ baseInfo.expectedPerformanceTime }}</text>
        </view>
        <view class="col">
          <text class="label">申领部门</text>
          <text class="desc">{{ baseInfo.applyDeptName }}</text>
        </view>
        <view class="col">
          <text class="label">申领人</text>
          <text class="desc">{{ baseInfo.applyUserName }}</text>
        </view>
        <view class="col">
          <text class="label">申领人电话</text>
          <text class="desc">{{ baseInfo.applyUserPhone }}</text>
        </view>
      </view>
      <view class="detail-title"> 申请明细 </view>
      <view class="detail-list">
        <view
          class="list-item"
          v-for="(item, index) in baseInfo.sparePartsApplyDetailList"
          :key="item.id"
        >
          <div class="item-name">
            <div class="name-text">{{ item.informationName }}</div>
            <div class="name-intro">
              ({{ item.informationCode }}/{{ item.modelType }})
            </div>
          </div>
          <div>
            <text class="item-num">{{ item.num }}</text>
            <text class="item-unit">{{ item.measuringUnit }}</text>
          </div>
        </view>
      </view>
    </view>

    <view class="approval-container" v-if="isApproval && baseInfo.status == 1">
      <view class="approval_btn-wrapper">
        <text class="approval-btn refuse" @click="handleRefuse">驳回</text>
        <text class="approval-btn primary" @click="handlePass(true)">通过</text>
      </view>
      <uni-popup ref="inputDialog" type="dialog">
        <uni-popup-dialog
          ref="inputClose"
          mode="input"
          title="驳回原因"
          placeholder="请输入内容"
          :before-close="true"
          @close="handleClose"
          @confirm="timeoutCauseConfirm"
        ></uni-popup-dialog>
      </uni-popup>
    </view>
  </view>
</template>

<script>
import { post, get, postJ } from '@/utils/api.js'
export default {
  data () {
    return {
      options: {},
      baseInfo: {},
      isApproval: false
    }
  },
  onLoad (options) {
    this.options = options
    this.isApproval = options.type === 'approval'
  },
  onShow () {
    this.getInfo()
  },
  computed: {},
  methods: {
    getInfo () {
      get(
        this.apiUrl + `/sparePartsApply/detail/${this.options.workOrderCode}`
      ).then(res => {
        if (res?.success) {
          this.baseInfo = res.data
        }
      })
    },

    // 审批
    handleRefuse () {
      this.$refs.inputDialog.open()
    },
    handleClose () {
      this.$refs.inputDialog.close()
    },
    handlePass (checked, cause) {
      const params = {
        checked,
        handleType: 0,
        myHandleId: this.options.id,
        id: this.baseInfo.id,
        type: 9,
        workOrderId: this.options.workOrderId
      }
      if (cause) {
        params.cause = cause
      }
      postJ(this.apiUrl + '/plan/info/audit', params).then(res => {
        if (res?.success) {
          uni.navigateTo({
            url: `/pages/home/backlog/result?type=${
              checked ? 'approve' : 'refuse'
            }&status=0`
          })
        }
      })
    },
    timeoutCauseConfirm (value) {
      if (!value) {
        uni.showToast({
          title: '请输入驳回原因',
          icon: 'none'
        })
        return
      }

      this.handlePass(false, value)
      this.$refs.inputDialog.close()
    }
  }
}
</script>

<style lang="scss" scoped>
.tab-title {
  width: 100%;
  padding: 0 30rpx;
  margin-bottom: 12rpx;
  display: flex;
  height: $tab-height;
  line-height: $tab-height;
  background-color: #ffffff;
  border-bottom: 1px solid #f2f2f2;
  box-sizing: border-box;

  .tab-item {
    width: 30%;
    text-align: center;
    font-size: 32rpx;
    color: $uni-text-color-grey;
  }

  .tab-item.active {
    color: $j-primary-border-green;
    border-bottom: 1px solid $j-primary-border-green;
  }
}

.plan-approval-container {
  display: flex;
  flex-direction: column;
  padding-bottom: 80rpx;

  /deep/.uni-collapse-item {
    background-color: rgba(21, 122, 44, 0.372549019607843);
  }

  .content {
    flex: 1;
    overflow: auto;

    .baseinfo {
      padding: 0 22rpx;
      font-size: 28rpx;

      .col {
        display: flex;
        padding: 10rpx 0;
        border-bottom: 1rpx dashed #d7d7d7;

        .label {
          display: inline-block;
          width: 200rpx;
          text-align: right;
          margin-right: 20rpx;
          font-weight: bold;
        }

        .desc {
          flex: 1;
          word-break: break-all;
        }
      }
    }
  }

  .collapse-title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 28rpx;
    height: 80rpx;
    padding: 8rpx;
    box-sizing: border-box;
  }

  .collapse-item-wrapper {
    padding: 12rpx;

    .info-container {
      margin-bottom: 20rpx;
    }
  }
}
.pd-list-item {
  padding: 10rpx;
  border-bottom: 1px solid #d7d7d7;
  .item {
    display: flex;
    font-size: 28rpx;
    justify-content: space-between;
    padding: 10rpx;
  }
}
.detail-title {
  width: 96%;
  margin: 20rpx auto 0;
  background: #e7e7e7;
  line-height: 68rpx;
  text-align: center;
  border: 1rpx solid #e7e7e7;
}
.detail-list {
  width: 96%;
  margin: 0 auto;
  border-left: 1rpx solid #e7e7e7;
  border-right: 1rpx solid #e7e7e7;
  border-bottom: 1rpx solid #e7e7e7;
  font-size: 30rpx;
  .list-item {
    border-bottom: 1rpx solid #e7e7e7;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 20rpx;
    .item-name {
      .name-text {
        color: #333;
      }
      .name-intro {
        color: #aaaaaa;
      }
    }
    .item-num {
      width: 80rpx;
      display: inline-block;
    }
    .item-unit {
      display: inline-block;
    }
  }
  .list-item:last-child {
    border: none;
  }
}
.approval-container {
  width: 100vw;
  height: 80rpx;

  .approval_btn-wrapper {
    display: flex;
    position: fixed;
    bottom: 0;
    width: 100vw;
    height: 80rpx;
    background-color: #fff;
  }

  .approval-btn {
    flex: 1;
    line-height: 78rpx;
    text-align: center;
    border: 1rpx solid $j-primary-border-green;
    &.refuse {
      color: red;
    }
    &.primary {
      background-color: $j-primary-border-green;
      color: #fff;
    }
  }
}
</style>
