<template>
  <!-- 入库审批 -->
  <view class="plan-approval-container">
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      left-icon="back"
      title="入库单审批详情"
      @clickLeft="back"
    ></uni-nav-bar>
    <template>
      <view class="tab-title">
        <view
          class="tab-item"
          :class="0 === pickTabIndex ? 'active' : ''"
          @click="changeChartsTab(0)"
          >基本信息
        </view>
        <view
          class="tab-item"
          :class="1 === pickTabIndex ? 'active' : ''"
          @click="changeChartsTab(1)"
        >
          入库明细
        </view>
      </view>
    </template>
    <view class="content">
      <!-- 基本信息 -->
      <view class="baseinfo" v-if="pickTabIndex === 0">
        <view class="col">
          <text class="label">入库单号</text>
          <text class="desc">{{ baseInfo.bizNum }}</text>
        </view>
        <!-- <view class="col">
					<text class="label">来源类型</text>
					<text class="desc">{{ baseInfo.planName }}</text>
				</view> -->
        <view class="col">
          <text class="label">来源单号</text>
          <text class="desc">{{ baseInfo.documentSource }}</text>
        </view>
        <view class="col">
          <text class="label">入库场景</text>
          <text class="desc">{{ dictOpt.bizScene[baseInfo.bizScene] }}</text>
        </view>

        <view class="col">
          <text class="label">入库登记人</text>
          <text class="desc">{{ baseInfo.createUserName }}</text>
        </view>

        <view class="col">
          <text class="label">供应商</text>
          <text class="desc">{{ baseInfo.supplierName }}</text>
        </view>
        <view class="col">
          <text class="label">送货人</text>
          <text class="desc">{{ baseInfo.deliveryName }}</text>
        </view>
        <view class="col">
          <text class="label">送货人电话</text>
          <text class="desc">{{ baseInfo.deliveryPhone }}</text>
        </view>
        <view class="col">
          <text class="label">紧急状态</text>
          <text class="desc">{{ dictOpt.urgent[baseInfo.urgent] }}</text>
        </view>
        <view class="col">
          <text class="label">入库时间</text>
          <text class="desc">{{ baseInfo.verifyTime }}</text>
        </view>
        <view class="col">
          <text class="label">备注</text>
          <text class="desc">{{ baseInfo.remark }}</text>
        </view>
      </view>
      <view class="equipment" v-if="pickTabIndex === 1">
        <uni-collapse accordion v-model="accordionVal">
          <uni-collapse-item
            v-for="(item, index) in planEquiList"
            :show-animation="true"
            :key="index"
            :open="false"
            :typeOpen="1"
          >
            <template v-slot:title>
              <view class="collapse-title">
                {{ item.assetName }}({{ item.assetCode }})
                <text>
                  {{ item.warehouseLedgerDetails.length }}{{ item.minPackUnit }}
                </text>
              </view>
            </template>
            <view class="collapse-item-wrapper">
              <view class="info-container">
                <view class="col">
                  <text class="label">类型</text>
                  <text class="desc">{{
                    getDictValue('物品类型', item.assetType)
                  }}</text>
                </view>
                <view class="col">
                  <text class="more">入库数量</text>
                  <text class="desc">
                    {{ item.outInNum }}{{ item.minPackUnit }}
                    <span style="margin-left: 15rpx"
                      >{{ item.outInNum * item.measurementUnit
                      }}{{ item.unit }}</span
                    >
                  </text>
                </view>
                <view class="col">
                  <text class="label">批次号</text>
                  <text class="desc">{{ item.batchNum }}</text>
                </view>
                <view class="col">
                  <text class="more">计量单位</text>
                  <text class="desc"
                    >{{ item.measurementUnit }}{{ item.unit }}/{{
                      item.minPackUnit
                    }}</text
                  >
                </view>
                <view
                  class="col"
                  v-for="(itm, index) in tableHeader"
                  :key="index"
                >
                  <text class="label">{{ itm.label }}</text>
                  <text class="desc">{{ item[itm.prop] }}</text>
                </view>
                <view class="col row">
                  <text class="label">货位</text>
                  <text class="desc"
                    >{{ item.warehouseName }}-{{ item.areaName }}-{{
                      item.shelfCode
                    }}-{{ item.cargoSpaceCode }}</text
                  >
                </view>
                <view class="col row" v-if="baseInfo.assetType == 3">
                  <text class="label">质检单</text>
                  <text
                    class="desc text-primary"
                    @tap.stop.prevent="viewFile(item.contentImage)"
                    >查看</text
                  >
                </view>
              </view>

              <uni-table
                border
                stripe
                emptyText="暂无更多数据"
                style="margin-top: 30rpx"
              >
                <!-- 表头行 -->
                <uni-tr>
                  <uni-th align="left" width="240rpx">批次号</uni-th>
                  <uni-th align="left" width="170rpx"
                    >{{ getDictValue('物品类型', item.assetType) }}编号</uni-th
                  >
                  <uni-th align="left" width="170rpx">{{
                    dateHeader(item.warehouseLedgerDetails)
                  }}</uni-th>
                  <uni-th align="left" width="170rpx">包装编码</uni-th>
                </uni-tr>
                <!-- 表格数据行 -->
                <uni-tr
                  v-for="(el, index) in item.warehouseLedgerDetails"
                  :key="index"
                >
                  <uni-td>
                    {{ el.batchNum }}
                  </uni-td>
                  <uni-td>{{ el.onlyCode }}</uni-td>
                  <uni-td>{{
                    el.manufactureTime || el.procurementTime
                  }}</uni-td>
                  <uni-td>{{ el.num }}</uni-td>
                </uni-tr>
              </uni-table>
            </view>
          </uni-collapse-item>
        </uni-collapse>
      </view>
    </view>
    <!-- <Approval v-if="isApproval" :devOpsType="0"
			:planType="options.planType" :myHandleId="options.id" :id="options.workOrderId" /> -->
    <view
      class="approval-container"
      v-if="isApproval && baseInfo.verifyStatus === 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>
    <u-overlay :show="show" @click="show = false">
      <view class="warp">
        <image v-if="imgs" :src="imgs"></image>
        <div v-else class="warp-tips">暂无数据显示</div>
      </view>
    </u-overlay>
  </view>
</template>

<script>
import { post, get, postJ } from '@/utils/api.js'
import Approval from '@/components/Approval.vue'
import Info from './components/Info'
import { tableHeader } from '@/pages/warehouse/common.js'
import Content from './components/Content'
import dictMixin from '@/mixins/dictMixins'
export default {
  components: {
    Approval,
    Content,
    Info
  },
  mixins: [dictMixin],
  data () {
    return {
      pickTabIndex: 0,
      accordionVal: '',
      options: {
        id: '',
        workOrderId: '',
        planType: '',
        devOpsType: ''
      },
      baseInfo: {},
      planEquiList: [],
      isApproval: false,
      dictOpt: {
        bizScene: {
          1: '生产入库',
          2: '采购入库',
          3: '赠送入库',
          4: '借用入库',
          5: '退还入库',
          6: '其他入库'
        },
        urgent: {
          1: '普通',
          2: '重要',
          3: '紧急'
        }
      },
      show: false,
      imgs: ''
    }
  },
  computed: {
    tableHeader () {
      return tableHeader(this.baseInfo.assetType)
    }
  },
  onLoad (options) {
    this.options = options
    this.isApproval = options.type === 'approval'
    this.requestDict('物品类型')
  },
  onShow () {
    this.getInfo()
  },
  methods: {
    dateHeader (warehouseLedgerDetails) {
      return warehouseLedgerDetails.some(item => !!item.manufactureTime)
        ? '生产日期'
        : '采购日期'
    },
    viewFile (list) {
      this.show = true
      this.imgs = this.apiUrl + list[0]?.accessUrl || ''
    },
    //切换选项卡
    changeChartsTab (index) {
      this.pickTabIndex = index
    },
    getInfo () {
      get(
        this.apiUrl +
          `/outInWarehouse/select/detail/${this.options.workOrderId}`
      ).then(res => {
        if (res?.success) {
          this.baseInfo = res.data.outInWarehouse
          this.planEquiList = res.data.warehouseLedgerInfos
        }
      })
    },
    // 审批
    handleRefuse () {
      this.$refs.inputDialog.open()
    },
    handleClose () {
      this.$refs.inputDialog.close()
    },
    handlePass (checked, cause) {
      const params = {
        approlId: this.options.id,
        handleResult: checked ? 2 : 0,
        bizId: this.options.workOrderId,
        handleCause: cause,
        handleStatus: 1
      }

      postJ(this.apiUrl + '/outInWarehouse/changeStatus', params).then(res => {
        if (res?.success) {
          uni.navigateTo({
            url: `/pages/home/backlog/result?type=${
              checked ? 'approve' : 'refuse'
            }&status=${this.devOpsType}`
          })
        }
      })
    },
    timeoutCauseConfirm (value) {
      if (!value) {
        uni.showToast({
          title: '请输入驳回原因',
          icon: 'none'
        })
        return
      }

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

<style lang="scss" scoped>
.warp {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  .warp-tips {
    font-size: 28rpx;
    color: #ffffff;
  }
}
.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: 160rpx;
          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;
      display: flex;
      flex-wrap: wrap;
      .col {
        width: 50%;
        &.row {
          width: 100%;
        }
      }
    }
  }
}

.pd-list-item {
  padding: 10rpx;
  border-bottom: 1px solid #d7d7d7;

  .item {
    display: flex;
    font-size: 28rpx;
    justify-content: space-between;
    padding: 10rpx;
  }
}

.info-container {
  padding: 0 20rpx;

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

    .label {
      width: 100rpx;
      text-align: right;
      margin-right: 28rpx;
      color: #333;
      font-weight: bold;
    }
    .more {
      width: 130rpx;
      text-align: right;
      margin-right: 28rpx;
      color: #333;
      font-weight: bold;
    }
    .desc {
      flex: 1;
    }
  }
}
.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>
