<template>
  <text @click.stop="selectAccountHandler(data, index)">
    <text class="text">{{data.patientName}}</text><text>{{data.hosNum}}</text>
    <!-- 取消发血弹窗 -->
    <selectAccount2 @click.stop.native v-if="hosModels2.disjunctor" :content="hosModels2.content" :disjunctor="hosModels2.disjunctor" @ok="hosOk2"
      @cancel="hosCancel2" :tips="hosModels2.tips" :cancelBloodHandover="cancelBloodHandover">
    </selectAccount2>
  </text>
</template>

<script>
  import {
    post
  } from "@/http/http.js";
  export default {
    data() {
      return {
        // 弹窗model
        hosModels2: {
          disjunctor: false,
        },
        
        coopData: {},
        isShowTips: false,
      };
    },
    props: {
      data: {
        type: Object,
      },
      index: {
        type: Number,
      },
      delivery: {
        type: Number,
        default: 0,
      },
      cancelBlood: {
        type: Number,
        default: 0,
      },
      cancelBloodHandover: {
        type: Number,
        default: 0,
      },
      dataList: {
        type: Object,
      },
      type: {
        type: String,
      },
    },
    methods: {
      cancelBloodHandler(data){
        console.log(data);
        console.log(this.coopData);
        uni.showLoading({
          title: "加载中",
          mask: true,
        });
        let postData = {
          "type": "bloodTake",
          service: 'cancelBlood',
          bloodIds: this.coopData.children.map(v => v.children).flat().map(v => v.id).toString(),
          closeOrder: this.isShowTips ? 1 : 0,
          handoverUser: data.accountId,
          cancelReason: data.cancelReason.id,
        };
      
        post('/transflow/extra', postData).then(res => {
          uni.hideLoading();
          if(res.state == 200){
             uni.showModal({
               title: "提示",
               content: "操作成功！",
               showCancel: false,
               success: (res) => {
                 if (res.confirm) {
                   console.log("用户点击确定");
                   if(this.type == 'start'){
                     if(this.isShowTips){
                       uni.navigateTo({
                         url: `/pages/receiptpage/receiptpage`,
                       });
                     }else{
                       // 触发自定义事件并传递参数给上一页
                       uni.$emit('refresh', { refresh: true });
                       
                       // 跳转回上一页
                       uni.navigateBack({
                           delta: 1
                       });
                     }
                   }else if(this.type == 'end'){
                     if(this.isShowTips){
                       uni.navigateTo({
                         url: `/pages/receiptpage/receiptpage`,
                       });
                     }else{
                       this.$emit('refresh');
                     }
                   }
                 } else if (res.cancel) {
                   console.log("用户点击取消");
                 }
               },
             });
          }else{
            uni.showToast({
              icon: "none",
              title: res.msg || "接口获取数据失败！",
            });
          }
        })
      },
      // 确认
      hosOk2(data) {
        console.log(data);
        const {
          accountName,
          account,
          accountId,
          cancelReason,
        } = data;
        
        if (!cancelReason) {
          //没有填写交接人
          uni.showModal({
            title: "提示",
            content: "请填写取消原因！",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                console.log("用户点击确定");
              } else if (res.cancel) {
                console.log("用户点击取消");
              }
            },
          });
          return;
        }
        
        // 必填交接人
        if(this.cancelBloodHandover == 1){
          if (!accountName && !account) {
            //没有填写交接人
            uni.showModal({
              title: "提示",
              content: "请填写交接人工号！",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log("用户点击确定");
                } else if (res.cancel) {
                  console.log("用户点击取消");
                }
              },
            });
            return;
          } else if ((!accountName && account) || (accountName && !account)) {
            //没有填写交接人
            uni.showModal({
              title: "提示",
              content: "请填写正确的交接人工号！",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  console.log("用户点击确定");
                } else if (res.cancel) {
                  console.log("用户点击取消");
                }
              },
            });
            return;
          }
        }
        
        this.hosModels2.disjunctor = false;
        this.cancelBloodHandler(data);
      },
      // 取消
      hosCancel2() {
        this.hosModels2.disjunctor = false;
      },
      // 取消发血弹窗
      showSelectAccount2(isShowTips = false) {
        this.isShowTips = isShowTips;
        this.hosModels2 = {
          content: `请填写取消原因和交接人信息`,
          disjunctor: true,
          tips: isShowTips ? '本患者取消发血后，工单将关闭！' : '',
        };
      },
      // 取消发血
      selectAccountHandler(data, index){
        console.log(this.cancelBlood)
        if(this.cancelBlood != 1){
          return;
        }
        this.coopData = data;
        let arr = this.dataList.children.filter((v, i) => i !== index);
        if(arr.length){
          let flag = false;
          if(this.type == 'end'){
            // 其他患者的血都是"病房接收"状态
            flag = arr.map(v => v.children).flat().map(v => v.children).flat().every(v => v.state.value == 5);
          }
          this.showSelectAccount2(flag);
        }else{
          // 没有其他患者
          this.showSelectAccount2(true);
        }
      },
    },
  };
</script>

<style lang="less" scoped>
  .text{
    margin-right: 16rpx
  }
</style>
