<template>
  <view class="order-list content">
    <view class="swiper-box">
      <template v-if="tableData.length">
        <u-checkbox-group 
            v-model="checked"
            @change="checkChange"
            iconPlacement="left" 
            shape='circle'
            placement="column">
          <view class="order-card" v-for="(item, index) in tableData" :key="index">
            <view class="order-card-title">
              <u-checkbox class="check-item" activeColor="#4680F9" :name='item.Hcontent'></u-checkbox>
              {{work == 'in'?$t('spare.orderCode')+':'+item.orderCode:$t('spare.applyCode')+':'+item.takeCode}}
            </view>
            <view class="order-info">
              <p class="info-line">
                <text class="label">{{$t('spare.spareName')}}:</text>
                <text class="info"> {{item.materialName || ''}}</text>
              </p>
              <p class="info-line">
                <text class="label">{{$t('spare.orderNum')}}:</text>
                <text class="info"> {{item.number}}</text>
              </p>
              <template v-if="work == 'in'">
                <p class="info-line">
                  <text class="label">{{$t('spare.alreadyInNumber')}}:</text>
                  <text class="info"> {{item.entryNum}}</text>
                </p>
                <p class="info-line">
                  <text class="label">{{$t('spare.canInNumber')}}:</text>
                  <text class="info"> {{item.number-item.entryNum}}</text>
                </p>
              </template>
              <template v-else>
                <p class="info-line">
                  <text class="label">{{$t('spare.canOutNumber')}}:</text>
                  <text class="info"> {{item.number-item.outNum}}</text>
                </p>
                <p class="info-line">
                  <text class="label">{{$t('spare.alreadyOutNumber')}}:</text>
                  <text class="info"> {{item.outNum}}</text>
                </p>
                <p class="info-line">
                  <text class="label">{{$t('spare.storeHaveNumber')}}:</text>
                  <text class="info"> {{item.totalNum}}</text>
                </p>
              </template>
            </view>
          </view>
        </u-checkbox-group>
      </template>
      <template v-else>
        <u-empty mode="list" icon="https://cdn.uviewui.com/uview/empty/list.png">
        </u-empty>
      </template>
    </view>
    <view class="examine" v-if="tableData.length != 0">
      <u-checkbox-group
        v-model="checkAll"
        placement="column"
        @change="checkAllChange"
      >
        <u-checkbox shape='circle' activeColor="#4680F9" label="全选" :name="true"></u-checkbox>
      </u-checkbox-group>
      <view class="exam-wrap">
        <button @click='confirm' class='exam-btn'>{{$t('spare.sureSelect')}}</button>
      </view>
    </view>
    <u-loading-page 
    color="#666" 
    loading-mode="semicircle"
    :loading="loading">
    </u-loading-page>
  </view>
</template>

<script>
import {getOrderDetailByPurveyId,getOrderDetailBydeptId} from '../../api/warehousing'
export default {
  components:{
  },
  data() {
    return {
      tableData:[],
      loading:false,
      keyword:'',
      checkAll:[],
      checked:[],
      work:'',
    };
  },
  onLoad(options) {
    this.purveyId = options.purveyId
    this.dept = options.dept
    this.work = options.work
    this.getList()
  },
  methods: {
    //确认选择
    confirm(){
      let pages = getCurrentPages();
      let prevPage = pages[pages.length - 2]; //上一个页面
      if(this.work == 'in'){
        this.checked.forEach(j=>{
          let item = JSON.parse(j)
          let boo = true
          prevPage.$vm.details.forEach(i=>{
            if(item.id == i.orderDtlId && i.sourceType == '101'){
              boo = false
            }
          })
          if(boo){
            item.count=0
            item.shelfList = [
              {
                shelf:0,
                floor:0,
                seat:0,
                sign:0,
                num:0,
              }
            ]
            item.unit=item.materialUnit
            item.remark=''
            item.orderDtlId = item.id
            item.sourceType = '101'
            item.maxNumber = item.number - item.entryNum
            item.cess = parseInt(item.cess)
            prevPage.$vm.details.push(item)
          }
        })
      }else{
        this.checked.forEach(j=>{
          let item = JSON.parse(j)
          let boo = true
          prevPage.$vm.details.forEach(i=>{
            if(item.id == i.takeId && i.sourceType == '101'){
              boo = false
            }
          })
          if(boo){
            item.count=0
            item.unit=item.materialUnit
            item.remark=''
            item.takeId = item.id
            item.sourceType = '101'
            prevPage.$vm.details.push(item)
          }
        })
      }
      uni.navigateBack();
    },
    //获取订单列表
    getList() {
      this.loading = true
      if(this.work == 'in'){
        let data = {
          purveyId:this.purveyId
        };
        getOrderDetailByPurveyId(data).then((res) => {
          this.loading = false
          if(res.data.code == 0){
            this.tableData = res.data.data
            this.tableData.forEach(item=>{
              item.Hcontent = JSON.stringify(item)
            })
          }
        }).catch(()=>{
          this.loading = false
        })
      }else{
        getOrderDetailBydeptId(this.dept).then(res=>{
          this.loading = false
          if(res.data.code == 0){
            this.tableData = res.data.data
            this.tableData.forEach(item=>{
              item.Hcontent = JSON.stringify(item)
            })
          }
        }).catch(()=>{
          this.loading = false
        })
      }
    },
    //审批选择
    checkChange(item){
      console.log(item);
      this.checked = [...item]
      if(this.checked.length == this.tableData.length && this.tableData.length!=0){
        this.checkAll = [true]
      }else{
        this.checkAll = []
      }
    },
    //全选
    checkAllChange(item){
      if(item[0]){
        this.checked = this.tableData.map(item=>{
          return item.Hcontent
        })
      }else{
        this.checked = []
      }
    }
  },
};
</script>

<style>
/* #ifndef APP-NVUE */
view {
  /* display: flex; */
  box-sizing: border-box;
  /* flex-direction: column; */
}

page {
	box-sizing: border-box;
	/* padding: 40rpx 40rpx 80rpx; */
	height: 100%;
}
/* #endif*/
</style>

<style lang="scss" scoped>
@import '../../common/css/public.scss';
.order-list {
  // height: calc(100vh - var(--window-top)) ;
  // overflow: hidden;
  .swiper-box{
    padding: 4rpx 0 24rpx;
  }
  background-color: #f8f8f8;
  .qrcode-wrap{
    padding: 24rpx;
    text-align: center;
    .info-line{
      text-align: center;
      font-size: 32rpx;
      margin-top: 24rpx;
    }
  }
}
.examine{
    position: fixed;
    bottom: 0;
    background-color: #fff;
    height: 80rpx;
    line-height: 80rpx;
    padding: 24rpx;
    width: 100%;
    display: flex;
    flex-direction: row;
    align-items: center;
    .exam-wrap{
      height: 80rpx;
      line-height: 80rpx;
      flex: 1;
      display: flex;
      justify-content: flex-end;
      flex-direction: row;
      font-size: 24rpx;
      align-items: center;
      .num{
        font-size: 32rpx;
        color: #4680F9;
        margin-right: 32rpx;
      }
      .exam-btn{
        height: 60rpx;
        border: none;
        background-color: #4680F9;
        color: #fff;
        font-size: 32rpx;
        line-height: 60rpx;
        margin: 0;
        border-radius: 80rpx;
        padding: 0 40rpx;

      }
      .disabled{
        color: rgba(0,0,0,.3);
        background-color: #f2f2f2;
      }
    }
  }
.popupShow {
	overflow: hidden;
	position: fixed;
}
.order-card .order-card-title{
  justify-content: left;
}
</style>