<template>
  <view class="Scanning_Result">
    <view class="Scanning_top">
      <view class="Scanning_top_icon">
        <text class="cubeic-ok newicon newicon-duigou"></text>
        <view class="text1"> 签到成功 </view>
      </view>
      <view class="Scanning_top_text">
        您已到达{{queryObj.startDeptName}}，请核对填写以下物品数量
      </view>
    </view>

    <view class="Scanning_cont">
      <view class="Scanning_cont_head">
        <view class="Scanning_cont_head_item" :class="{active: parentIndex == i}" v-for="(item, i) in dataList" :key="item.id" @click="parentIndex = i">
          {{item.name}}
        </view>
      </view>

      <view class="Scanning_cont_list">
        <view class="Scanning_cont_list_item Scanning_cont_list_head">
          <view class="name">
            被服类型
          </view>
          <view class="value">
            <view>
              被服数量
            </view>
          </view>
        </view>
        <scroll-view scroll-y="true" class="Scanning_cont_list_scroll" v-if="dataList[parentIndex]">
          <view class="Scanning_cont_list_item" @click="clickRow(item)" v-for="(item, j) in dataList[parentIndex].children" :key="item.id">
            <view class="name">
              {{item.name}}
            </view>
            <view class="value">
              <view>
                {{item.clothesWashingDTO ? item.clothesWashingDTO.recyclingNum : ''}}
              </view>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>

    <view class="foot_btn">
      <view class="btn" @click="onClick()"> 汇总交接 </view>
    </view>
    <!-- 填写数字弹窗 -->
    <selectNum v-if="numModels.disjunctor" :disjunctor="numModels.disjunctor" @ok="numOk" @cancel="numCancel" :content="numModels.content" :dataName="numModels.dataName">
    </selectNum>
    <!-- 弹窗 -->
    <showModel :title="models1.title" :icon="models1.icon" :disjunctor="models1.disjunctor" :content="models1.content"
      @ok="ok1" @cancel="cancel1" :operate="models1.operate"></showModel>
    <!-- 弹窗 -->
    <showModel :title="models2.title" :icon="models2.icon" :disjunctor="models2.disjunctor" :content="models2.content"
      @ok="ok2" @cancel="cancel2" :operate="models2.operate"></showModel>
  </view>
</template>
<script>
  import {
    get,
    post,
    SM,
    webHandle
  } from "../../../http/http.js";
  export default {
    data() {
      return {
        hosId: uni.getStorageSync('userData').user.currentHospital.id,
        queryObj: {}, //路由传递过来的数据
        // 填写数字弹窗model
        numModels: {
          disjunctor: false,
        },
        dataList: [],
        parentIndex: 0,
        coopData: {},
        // 弹窗model
        models1: {
          disjunctor: false,
        },
        // 弹窗model
        models2: {
          disjunctor: false,
        },
      };
    },
    methods: {
      //确定
      ok2() {
        this.models2.disjunctor = false;
        let bussList = this.dataList.map(v => v.children).flat().map(v => v.clothesWashingDTO).map(v => ({...v, exception: 0, recyclingNum: 0,}));
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/transflow/createOrTakeOrder", {
          type: 'clothingGet',
          id: 0,
          startDeptId: this.queryObj.startDeptId,
          hosId: this.hosId,
          bussList,
          orderId: this.queryObj.orderId,
          handoverId: this.queryObj.nameId || undefined,
        }).then((res) => {
          uni.hideLoading();
          if (res.state == 200) {
            uni.showModal({
              title: "提示",
              content: "交接成功",
              showCancel: false,
              success: (result) => {
                if (result.confirm) {
                  console.log("用户点击确定");
                  uni.navigateTo({
                    url: `../../receiptpage/receiptpage`,
                  });
                }
              },
            });
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        });
      },
      //取消
      cancel2() {
        this.models2.disjunctor = false;
      },
      // 弹窗
      showModel2() {
        this.models2 = {
          disjunctor: true,
          title: "提示",
          content: `<strong class="red">${this.queryObj.startDeptName}</strong>无需填写回收数量，您确认吗？`,
          icon: "warn",
          operate: {
            ok: "确定",
            cancel: "取消",
          },
        };
      },
      //确定
      ok1() {
        this.models1.disjunctor = false;
        let bussList = this.dataList.map(v => v.children).flat().map(v => v.clothesWashingDTO).map(v => ({...v, exception: 0}));
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/transflow/createOrTakeOrder", {
          type: 'clothingGet',
          id: 0,
          startDeptId: this.queryObj.startDeptId,
          hosId: this.hosId,
          bussList,
          orderId: this.queryObj.orderId,
          handoverId: this.queryObj.nameId || undefined,
        }).then((res) => {
          uni.hideLoading();
          if (res.state == 200) {
            uni.showModal({
              title: "提示",
              content: "交接成功",
              showCancel: false,
              success: (result) => {
                if (result.confirm) {
                  console.log("用户点击确定");
                  uni.navigateTo({
                    url: `../../receiptpage/receiptpage`,
                  });
                }
              },
            });
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        });
      },
      //取消
      cancel1() {
        this.models1.disjunctor = false;
      },
      // 被服回收弹窗
      showModel1() {
        let bussList = this.dataList.map(v => v.children).flat();
        let num = bussList.reduce((prev, current) => prev + current.clothesWashingDTO.recyclingNum, 0);
        let content = '';
        if(this.queryObj.name){
          content = `<strong class="red">${this.queryObj.startDeptName}</strong>已与<strong class="red">${this.queryObj.name}</strong>交接<strong class="red">${num}件</strong>被服，交接人为<strong class="red">${this.queryObj.name}</strong>，您确认交接吗？`;
        }else{
          content = `<strong class="red">${this.queryObj.startDeptName}</strong>已交接<strong class="red">${num}件</strong>被服，您确认交接吗？`;
        }
        this.models1 = {
          disjunctor: true,
          title: "提示",
          content,
          icon: "warn",
          operate: {
            ok: "确定",
            cancel: "取消",
          },
        };
      },
      // 填写数量-确认
      numOk(data) {
        console.log(data);
        this.numModels.disjunctor = false;
        this.updateNum(data);
      },
      // 填写数量-取消
      numCancel() {
        this.numModels.disjunctor = false;
      },
      onClick(){
        this.showModel1();
      },
      clickRow(data){
        this.coopData = data;
        this.numModels = {
          disjunctor: true,
          content: `请输入被服数量`,
          dataName: data.name,
        };
      },
      updateNum(data){
        this.dataList[this.parentIndex].children.forEach(v => {
          if(v.id == this.coopData.id){
            v.clothesWashingDTO && (v.clothesWashingDTO.recyclingNum = data.num);
          }
        })
      },
      getInfo(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/transflow/scanInfo", {
          type: 'clothingGet',
          hosId: this.hosId,
          startDeptId: this.queryObj.startDeptId,
          code: this.queryObj.quiltCode,
          orderId: this.queryObj.orderId,
        }).then((result) => {
          uni.hideLoading();
          if (result.state == 200) {
            let dataList = result.data.treeList || [];
            this.dataList = dataList.slice(0, 3);
          } else {
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败！",
            });
          }
        });
      },
      getNoFillInQuantityIds(){
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        post("/simple/data/fetchDataList/taskTypeConfig", {
            "idx": 0,
            "sum": 10,
            "taskTypeConfig": {
                "taskTypeDTO": {
                    "hosId": {
                        "id": this.hosId
                    },
                    "ordinaryField": {
                        "key": "ordinary_field",
                        "value": "clothingGet"
                    }
                }
            }
        }).then((result) => {
          uni.hideLoading();
          if (result.status == 200) {
            let dataList = result.list || [];
            if(dataList.length){
              let flag = dataList[0].noFillInQuantityIds.split(',').includes(this.queryObj.startDeptId);
              if(flag){
                this.showModel2();
              }
            }
          } else {
            uni.showToast({
              icon: "none",
              title: result.msg || "接口获取数据失败！",
            });
          }
        });
      },
    },
    onLoad(options) {
      console.log(options, "result");
      this.queryObj = options;
      this.getInfo();
      this.getNoFillInQuantityIds();
      // #ifdef APP-PLUS
      webHandle("no", "app");
      // #endif
      // #ifdef H5
      webHandle("no", "wx");
      // #endif
    },
  };
</script>
<style lang="less" scoped>
  .Scanning_Result {
    height: 100vh;
    display: flex;
    flex-direction: column;
    background-color: #fafbfd;
    .Scanning_top {
      flex-shrink: 0;
      .Scanning_top_icon {
        padding-top: 26rpx;
        display: flex;
        justify-content: center;
        align-items: center;

        .cubeic-ok {
          font-size: 58rpx;
          color: #35b34a;
        }

        .text1 {
          font-size: 48rpx;
          font-weight: bold;
        }
      }
      .Scanning_top_text{
        text-align: center;
        font-size: 28rpx;
        font-weight: bold;
        padding: 8rpx 0 23rpx 0;
      }
    }

    .Scanning_cont {
      flex: 1;
      min-height: 0;
      display: flex;
      flex-direction: column;
      width: 710rpx;
      margin: 0 20rpx;
      background-color: #fff;

      .Scanning_cont_head{
        flex-shrink: 0;
        height: 78rpx;
        display: flex;
        border-top: 1rpx solid #EEEEEE;
        border-bottom: 1rpx solid #EEEEEE;
        .Scanning_cont_head_item{
          flex: 1;
          font-size: 32rpx;
          font-weight: bold;
          display: flex;
          justify-content: center;
          align-items: center;
          position: relative;
          &.active{
            color: #49B856;
            &::before{
              content: '';
              width: 70rpx;
              height: 10rpx;
              background-color: #49B856;
              position: absolute;
              left: 50%;
              bottom: 0;
              transform: translateX(-50%);
              border-radius: 6rpx;
            }
          }
          &::after{
            content: '';
            width: 2rpx;
            height: 44rpx;
            background-color: #D1D1D1;
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
          }
          &:last-of-type::after{
            opacity: 0;
          }
        }
      }

      .Scanning_cont_list{
        flex: 1;
        min-height: 0;
        display: flex;
        flex-direction: column;
        .Scanning_cont_list_scroll{
          flex: 1;
          min-height: 0;
        }
        .Scanning_cont_list_item{
          height: 70rpx;
          display: flex;
          align-items: center;
          font-size: 28rpx;
          border: 1rpx solid #272727;
          border-top: none;
          &.Scanning_cont_list_head{
            font-weight: bold;
            font-size: 28rpx;
            border-top: 1rpx solid #272727;
            flex-shrink: 0;
          }
          .name,
          .value{
            padding: 0 40rpx;
            flex: 1;
            display: flex;
            align-items: center;
            height: 100%;
            border-right: 1rpx solid #272727;
          }
          .value {
            view{
              width: 4em;
              text-align: center;
            }
          }
        }
      }
    }

    .foot_btn {
      flex-shrink: 0;
      line-height: 88rpx;
      display: flex;
      justify-content: center;

      .btn {
        height: 88rpx;
        flex: 1;
        margin-right: 1%;
        background-image: linear-gradient(to right, #72c172, #3bb197);
        color: #fff;
        border-radius: 8rpx;
        font-size: 28rpx;
        text-align: center;
        &:last-of-type{
          margin-right: 0;
        }
      }
    }
  }
</style>
