<template>
  <view class="container">
    <view class="userInfo_box" v-if="serviceType != 'addCard'">
      <view class="userInfo_list border_bottom">
        <text>姓名</text>
        <text>{{userInfo.memberName}}</text>
      </view>
      <view class="userInfo_list border_bottom">
        <text>手机号</text>
        <text>{{common.desensitization(userInfo.userMobile,3,7)}}</text>
      </view>
      <view class="userInfo_list border_bottom">
        <text>身份证号</text>
        <text>{{common.desensitization(userInfo.certNum,4,14) || '-'}}</text>
      </view>
      <view class="userInfo_list border_bottom" v-if="userInfo.guardianCertNum">
        <text>监护人身份证号</text>
        <text>{{common.desensitization(userInfo.guardianCertNum,4,14)}}</text>
      </view>
      <view class="userInfo_list" data-type="open" @click="selectItem('open', $event)">
        <text class="userInfo_listRela">成员关系</text>
        <view class="relationCode_text" v-if="relationCodeIndex == null"><view>请选择亲友成员与本人关系 <text class="colorRed">(必选)</text></view><image class="public_right_img" :src="iconUrl.icon_rightGreen"></image></view>
        <text v-if="relationCodeIndex != null">{{relationCodeList[relationCodeIndex].dictLabel}}</text>
      </view>
    </view>
    <!-- 有卡全部绑定 -->
    <view class="cardList_box" v-if="serviceType == 'addMember'">
      <view class="cardList_topText colorCustom_F08">
        就诊人"{{userInfo.memberName}}"名下已关联
        <text>{{userInfo.cardNum}}张就诊卡</text>
        <text v-if="userInfo.hosNum != 0">、{{userInfo.hosNum}}个住院号</text>
      </view>
      <view class="card_item displayFlexRow" v-for="(item, index) in cardList" :key="index" @click="chooseCard(index)">
        <template v-if="item.cardType == 1">
          <view class="member_item_info displayFlexCol">
            <view class="info_memberMsg_name ">{{item.memberName}}</view>
            <view class="info_memberCard" >
              就诊卡：{{item.cardNo}}
            </view>
          </view>
          <view class="set_default displayFlexRow" @click.stop="autoBindCard(index)" v-if="userInfo.cardNum > 1">
            <text>设为默认卡</text>
            <image :src="item.check ? iconUrl.circle_active : iconUrl.circle" />
          </view>
        </template>
        <template v-if="item.cardType == 14">
          <view class="member_item_info displayFlexCol">
            <view class="info_memberMsg_name ">{{item.memberName}}</view>
            <view class="info_memberCard" >
              住院号：{{item.cardNo}}
            </view>
          </view>
          <view class="set_default" :class="item.inHospitalStatus == 1?'colorCustom':'colorCustom_999'">
            {{item.inHospitalStatus == 1 ? '在院' : '离院'}}
          </view>
        </template>
      </view>
    </view>
  
    <!-- 无卡就诊人显示 -->
    <view class="no_card_tips_box displayFlexLeft" v-if="serviceType == 'noCard_idCard'">
      <text class="view colorCustom_F08" style="margin-bottom: 40upx;">查无关联就诊卡、住院号</text>
      <text class="text">1、您可直接建档，先预约，到院后到窗口办卡；</text>
      <text class="text mb0">2、若您手中有就诊卡，可手动添加。</text>
    </view>
    <template v-if="serviceType == 'diMobile'">
      <!-- 手机号不同 -->
      <view class="no_card_tips_box">
        <view class="view colorCustom_F08">当前手机尾号{{common.strSub(userInfo.userMobile,7,userInfo.userMobile.length)}}未查询到就诊卡，系统查询到该身份证绑定了其他手机号码，请确认</view>
        <view class="text mb0">该身份证绑定了其他手机号码</view>
      </view>
      <!-- 手机号不同 -->
      <view class="no_card_tips_box">
        <view class="view">请选择已绑定手机号码进行校验</view>
        <view class="choice_card_box">
          <view class="choice_card_item_box displayFlexRow border_bottom" v-for="(item, index) in mobileList" :key="index" @click="choiceMobile(index)">
            <view class="choice_card_info_box displayFlexCol">
              <text>{{item.mobile}}</text>
              <text>请输入手机号码中间4位进行确认</text>
            </view>
            <image v-if="index == mobileIndex" :src="iconUrl.circle_active" />
            <image v-if="index != mobileIndex" :src="iconUrl.circle" />
          </view>
        </view>
      </view>
    </template>
    
  
    <!-- 姓名与查询出来的不一致--手动录入 -->
    <view class="no_card_tips_box displayFlexCol" v-if="serviceType == 'diMemberName_idCard'">
      <text class="color62626D">该身份信息已注册过</text>
      <text>就诊人姓名与身份信息不匹配，查询档案与您输入的信息不一致；如果您是本人，请进行人脸认证。</text>
    </view>
  
    <!-- 姓名与查询出来的不一致--人脸添加 -->
    <view class="no_card_tips_box displayFlexCol" v-if="serviceType == 'diMemberName_face'">
      <text class="color62626D">该身份信息已注册过</text>
      <text>就诊人姓名与身份信息不匹配，查询档案与您输入的信息不一致，请确认：</text>
      <view class="query_card_info displayFlexCol">
        <text>{{cardList[0].MemberName}}</text>
        <text>身份证号：{{cardList[0].IdCardNo}}</text>
      </view>
    </view>
    <!-- 姓名与查询出来的不一致--人脸添加 -->
    <view class="no_card_tips_box query_member_mobile displayFlexCol" v-if="serviceType == 'diMemberName_face'">
      <text>如需绑定该就诊人，请校验手机号码 </text>
      <text>如需修改手机号码，请持身份证到医院窗口修改</text>
      <view class="query_card_info displayFlexCol">
        <text>{{common.desensitization(cardList[0].Telephone,3,7)}}</text>
        <text>请输入手机号码中间4位进行确认</text>
      </view>
    </view>
    <!-- 通过卡添加人时 切输入的卡号验证通过返回信息正确 -->
    <view class="userInfoTow" v-if="serviceType == 'addCard'  && step != 1">
      <view class="userInfoTow_text colorCustom_F08">当前{{redioCardType=='1'?'就诊卡':'住院号'}}“{{addCardNo}}”，查询到以下就诊人信息，请选择正确的一个添加！</view>
      <radio-group @change="radioChange('changCardListIndex', $event)">
        <view class="userInfoTow_box" v-for="(item, index) in cardList" :key="index">
          <view class="displayFlexBetween">
            <view class="userInfoTow_boxName">{{item.memberName}}</view>
            <radio class="addCardMask_radio" :value="String(index)" :checked="changCardListIndex == index"></radio>
          </view>
          <view class="userInfoTow_boxinfo">
            <view class="userInfoTow_boxinfo_text">手机号：{{item.mobile}}</view>
            <view class="userInfoTow_boxinfo_text">身份证号：{{item.certNum || '-'}}</view>
            <!-- 判断如果没有身份证号，展示监护人身份信息 -->
            <template v-if="!item.certNum">
              <view class="userInfoTow_boxinfo_text">监护人姓名：{{item.guardianName || '-'}}</view>
              <view class="userInfoTow_boxinfo_text">监护人身份证号：{{item.guardianCertNum || '-'}}</view>
            </template>
          </view>
          <view class="userInfo_list" data-type="open" @click="selectItem('open', $event)" v-if="changCardListIndex == index">
            <text class="userInfo_listRela">成员关系</text>
            <view class="relationCode_text" v-if="relationCodeIndex == null">
              <view>请选择亲友成员与本人关系 <text class="colorRed">(必选)</text></view>
            </view>
            <text v-if="relationCodeIndex != null">{{relationCodeList[relationCodeIndex].dictLabel}}</text>
            <image class="public_right_img" :src="iconUrl.icon_rightGreen"></image>
          </view>
        </view>
      </radio-group>
      
    </view>
    <view class="footer_btn_box">
      <view class="public_btn backgroundCustom_F08" @click="jumpCheckReportForm" v-if="serviceType == 'addMember'">全部添加</view>
      <view class="public_btn backgroundCustom_F08" data-tag="createFile" @click="jumpCheckReportForm('createFile')" v-if="serviceType == 'noCard_idCard'">无卡建档，可预约</view>
      <view class="public_btn backgroundCustom" data-tag="addCard" @click="jumpCheckReportForm('addCard')" v-if="serviceType == 'noCard_idCard'">我有卡，手动添加</view>
      <view class="public_btn backgroundCustom_F08" @click="jumpCheckReportForm" v-if="serviceType == 'diMobile' || serviceType == 'diMemberName_face'">获取短信验证码</view>
      <view class="public_btn backgroundCustom_F08" @click="clickCardBtn" v-if="serviceType == 'addCard' && step == 2">下一步</view>
    </view> 
  </view>
  
  
  <!-- 手动添加就诊卡 -->
  <view class="add_card_mask displayFlexRow" v-if="showAddCard">
    <view class="add_card_box displayFlexCol">
      <text class="add_card_title">添加就诊人</text>
      <text class="add_card_tips" v-if="step == 3">发送到手机号：{{cardList[0].mobile}}</text>
      <view class="page-section" v-if="step == 1">
        <radio-group @change="radioChange('redioCardType', $event)">
          <radio class="addCardMask_radio" value="1" :checked="redioCardType == 1">就诊卡</radio>
          <radio class="addCardMask_radio" value="14" :checked="redioCardType == 14">住院号</radio>
        </radio-group>
      </view>
      <input class="add_card_input" type="text" :value="step == 1 ? addCardNo : provingCode" :placeholder="step == 1 ?(redioCardType == '1'? '请输入就诊卡号' : '请输入住院号'):'请输入验证码' " :data-name="step == 1 ? 'addCardNo' : 'provingCode'" @input="getInputVal"/>
      <text class="add_card_tips" v-if="step == 3 && seconds > 0">{{seconds}}秒可重发</text>
      <text class="add_card_tips colorCustom" v-if="step == 3 && seconds == 0" @click="getProvingCode">点击发送验证码</text>
      <view class="add_card_footer_btn displayFlexRow">
        <text class="backgroundCustom" @click="hideAddCardModel">取消</text>
        <text class="backgroundCustom_F08" @click="clickCardBtn">确定</text>
      </view>
    </view>
  </view>
  
  <!-- 手机号不同--检验中间四位，并发送验证码校验 -->
  <view class="add_card_mask displayFlexRow" v-if="showDiMobile">
    <view class="add_card_box displayFlexCol">
      <text class="add_card_title">请输入手机号码中间4位</text>
      <text class="add_card_tips">手机号：{{serviceType == "diMemberName_face" ? cardList[0].mobile : mobileList[mobileIndex].mobile}}</text>
      <input class="add_card_input" type="number" :value="step == 1 ? subMobile : provingCode" :placeholder="step == 1 ? '请输入手机号码中间4位' : '请输入验证码'" :data-name="step == 1 ? 'subMobile' : 'provingCode'" @input="getInputVal"/>
      <text class="add_card_tips" v-if="step == 2 && seconds > 0">{{seconds}}秒可重发</text>
      <text class="add_card_tips colorCustom" v-if="step == 2 && seconds == 0" @click="getProvingCode">点击重新发送验证码</text>
      <view class="add_card_footer_btn displayFlexRow">
        <text class="backgroundCustom" @click="hideAddCardModel">取消</text>
        <text class="backgroundCustom_F08" @click="clickMobileBtn">确定</text>
      </view>
    </view>
    
  </view>
  <!-- 筛选条件 -->
  <view class="mainScreenBox " v-if="relationsShow">
    <view class="cententBox">
      <text class="screenTitle">选择成员关系</text>
      <view class="screenList">
        <text v-for="(item, index) in relationCodeList" :key="index" @click="selectItem('select', $event, index)" class="" :class="relationCodeIndex == index?'active_screen':''" >{{item.dictLabel}}</text>
      </view>
      <view class="public_btn backgroundCustom" @click="selectItem('open', $event)">确定</view>
    </view>
  </view>
  <!-- 遮罩 -->
  <view class="mask" v-if="relationsShow" @click="selectItem('cancel', $event)"></view>
</template>

<script lang="ts" setup>
import { ref, reactive, computed, getCurrentInstance } from 'vue';
import { useStore } from 'vuex';
import { useOnLoad, usePreserMember, useGetDefaultMember } from '@/hook';
import { common } from '@/utils';
import icon from '@/utils/icon';
import {
  dataList,
  queryMemberCardList_V3,
  addBaseMember_V3,
  updateToHealthCard_V2,
  setDefaultClinicCard_V3,
  queryMemberByCard_V3,
  sendVerificationCode_V3,
  addBaseMemberByEncryptionStore_V3,
  checkMobile_V3,
  checkVerificationCode_V3,
  setDefaultMemberV3
} from '@/pagesPersonal/service/patientManagement';

const { proxy } = getCurrentInstance();
const app = getApp();
const store = useStore();
const iconUrl = ref(icon);
const serviceType = ref("");
const pageType = ref("");
const reqData = ref<any>({});
const userInfo = ref<any>({});
const cardList = ref<any[]>([]);
const mobileList = ref<any[]>([]);
const mobileIndex = ref(0);
const checkedIndex = ref("0");
const showAddCard = ref(false);
const showDiMobile = ref(false);
const step = ref(1);
const addCardNo = ref("");
const subMobile = ref("");
const pcId = ref("");
const provingCode = ref("");
const seconds = ref(0);
const redioCardType = ref('1');
const changCardListIndex = ref(0);
const relationCodeList = ref<any[]>([]);
const relationCodeIndex = ref<number | null>(null);
const relationsShow = ref(false);
let timer: any = null;
let relationIndex: number | null = null;

useOnLoad((options) => {
  getRelationCode();
  let queryData = options.queryData ? JSON.parse(options.queryData) : {};
  if (queryData.userMobile) {
    queryData.mobile = queryData.userMobile;
    queryData.isSHA = true;
  }
  serviceType.value = options.serviceType || "";
  reqData.value = queryData;
  userInfo.value = queryData;
  redioCardType.value = options.redioCardType || '1';
  pageType.value = options.pageType || '';
  
  // 通过卡查询绑卡
  if (serviceType.value == 'addCard') {
    showAddCard.value = true;
  }
  // 添加就诊人 查询当前信息就诊卡
  if (serviceType.value == 'addMember') {
    queryCardList(reqData.value);
  }
});

// 获取亲友成员关系字典
const getRelationCode = async () => {
  let { resp } = await dataList({
    Page: {
      "PIndex": 1,
      "PSize": 10
    },
    dictType: "MemberAccountRelationCode"
  });
  if (common.isNotEmpty(resp)) {
    // 过滤本人字典，后端字典会自动插入前端过滤
    resp = resp.filter((item: any) => item.dictValue != '1');
  }
  relationCodeList.value = resp || [];
};

/**
 * 查询人下的卡
 */
const queryCardList = async (reqDataParam: any) => {
  let userInfoVal = userInfo.value;
  let { resp, resData } = await queryMemberCardList_V3(reqDataParam);
  if (resData.RespCode == "10000") {
    if (serviceType.value == 'addMember') {

      // 查询到的卡卡列表去重手机号
      let mList = common.unique(resp, "mobile");
      // 判断当前返回的手机号是否与填写的手机号一致
      if (reqData.value.userMobile != mList[0].mobile) {
        mobileList.value = mList;
        serviceType.value = "diMobile";
        return;
      }
    }
    if (serviceType.value == 'diMobile') {
      serviceType.value = 'addMember';
      showDiMobile.value = false;
    }
    let cList = resp;
    if (common.isNotEmpty(resp)) {
      userInfoVal.cardNum = resp.filter((item: any) => item.cardType == 1).length;
      userInfoVal.hosNum = resp.filter((item: any) => item.cardType == 14).length;
      userInfo.value = userInfoVal;
      cList = [];
      for (let item of resp) {
        if (item.cardType == 1) {
          cList.unshift(item);
        }
        if (item.cardType == 14) {
          cList.push(item);
        }
      }
      if (cList[0].cardType == 1) {
        cList[0].check = true;
      }
    }

    cardList.value = cList;
  }
};

/**
 * 选择默认就诊卡
 */
const autoBindCard = (index: number) => {
  let cList = cardList.value;
  cList.forEach((item: any) => {
    item.check = false;
  });
  cList[index].check = true;
  cardList.value = cList;
};

/**
 * 选择就诊卡
 */
const chooseCard = (index: number) => {
  checkedIndex.value = String(index);
};

/**
 * 当手机号不同时，选择手机号进行校验
 */
const choiceMobile = (index: number) => {
  mobileIndex.value = index;
};

/**
 * 全部按钮
 */
const jumpCheckReportForm = async (tag?: string) => {
  // var index = checkedIndex.value;
  let sType = serviceType.value;
  // let tagVal = tag;
  // 验证是否选择了亲友成员关系
  if (checkRelation()) return;
  // 添加就诊人
  let memberId = "";
  if (sType == 'addMember') {
    let { resp } = await addBaseMember_V3(reqData.value);
    if (common.isNotEmpty(resp)) {
      memberId = resp[0].memberId;
      addMemberSuccess(resp);
    }
  }
  // 人脸录入
  else if (sType == 'noCard_idCard') {
    // 建档
    if (tag == "createFile") {
      addMemberFile();
    } else {
      showAddCard.value = true;
    }
  }
  // 手机号不同
  else if (sType == "diMobile" || sType == 'diMemberName_face') {
    showDiMobile.value = true;
    
  }
  //添加健康卡
  if (pageType.value == "addHealthCard") {
    /**获取健康卡code   未授权会弹出授权框 */
    let wechatCode = await common.getAuthorize();
    
    let memberInfo = {
      memberId: memberId
    };
    uni.hideLoading();
    if (common.isNotEmpty(wechatCode)) {
      //如果已经授权
      app.globalData.wechatCode = wechatCode;
      
      //点击健康卡升级
      let param = '{ "MemberId": "' + memberInfo.memberId + '","WechatCode":"' + wechatCode + '","DefaultCardNo":"' + cardList.value[0].CardNo + '","DefaultCardType":"' + cardList.value[0].CardType + '"}';
      let { resData } = await updateToHealthCard_V2({
        'data': common.desEncrypt(param, app.globalData.apiSecretKey)
      });
      uni.hideLoading();
      if (resData.RespCode == 10000) {
        //升级健康卡成功
        common.showToast('升级成功', () => {
          uni.navigateTo({
            url: '../memberList/memberList'
          });
        });
      }
    }
  }
};


const addMemberFile = async () => {
  let { resp } = await addBaseMember_V3(reqData.value);
  if (common.isNotEmpty(resp)) {
    common.showModal('建档成功', () => {
      addMemberSuccess(resp);
    });
  }
};

/**
 * 修改默认就诊卡
 */
const setDefaultCard = async (memberId: string, cardInfo: any) => {
  let data = {
    memberId: memberId,
    cardNo: cardInfo.cardNo,
    cardType: cardInfo.cardType,
    encryptionStore: cardInfo.encryptionStore,
  };
  let res = await setDefaultClinicCard_V3(data);
  if (res.resData.RespCode == 10000) {
     // Success handling if needed
  }
};

/**
 * 获取输入框的值
 */
const getInputVal = (e: any) => {
  let name = e.currentTarget.dataset.name;
  let val = e.detail.value;
  if (name === 'addCardNo') addCardNo.value = val;
  if (name === 'provingCode') provingCode.value = val;
  if (name === 'subMobile') subMobile.value = val;
};

// 通过卡号查询就诊人信息
const clickCardBtn = async () => {
  // 添加就诊卡第一步
  if (step.value == 1) {
    if (common.isEmpty(addCardNo.value)) {
      common.showToast(redioCardType.value == '1' ? "请输入就诊卡号" : '请输入住院号');
      return;
    }
    let querData = {
      cardNo: addCardNo.value,
      cardType: redioCardType.value
    };
    let { resp } = await queryMemberByCard_V3(querData);
    if (common.isNotEmpty(resp)) {
      // 判断通过卡号 查询到的就诊信息，在通过姓名加手机号查询出来的信息大于1个人，让用户选择绑定那个人
      let cardListResp = resp.filter((item: any) => common.isNotEmpty(item.certNum));
      // 判断如果查询到是空 跳转添加界面补全患者信息
      if (common.isEmpty(cardListResp)) {
        common.goToUrl(`/pagesPersonal/st1/business/patientManagement/addMember/addMember?pageType=cardAddMember&queryBean=${encodeURIComponent(JSON.stringify(resp[0]))}`);
      } else {
        serviceType.value = 'addCard';
        step.value = 2;
        showAddCard.value = false;
        cardList.value = cardListResp;
      }
    }
  } else if (step.value == 2) {
    // 验证是否选择了亲友成员关系
    if (checkRelation()) return;
    showAddCard.value = true;
    step.value = 3;
  } else {
    if (common.isEmpty(pcId.value)) {
      common.showToast("请发送验证码");
      return;
    }
    if (common.isEmpty(provingCode.value)) {
      common.showToast("请输入验证码");
      return;
    }
    
    let querData = {
      memberId: cardList.value[changCardListIndex.value].memberId,
      cardEncryptionStore: cardList.value[changCardListIndex.value].encryptionStore,
      pcId: pcId.value,
      verificationCode: provingCode.value,
      relationCode: relationCodeList.value[relationCodeIndex.value!].dictValue
    };
    let res = await addBaseMemberByEncryptionStore_V3(querData);
    let resp = res.resp;
    if (common.isNotEmpty(resp)) {
      addMemberSuccess(resp);
    }
  }
};

/**
 * 手动添加就诊卡确定按钮操作
 */
const clickMobileBtn = async () => {
  let MobileBoj = mobileList.value[mobileIndex.value];
  let frontNum = MobileBoj.mobile.substr(0, 3); //手机号前四位
  let afterNum = MobileBoj.mobile.substr(-4); //手机号后三位
  // 手机号不同
  if (step.value == 1) {
    if (common.isEmpty(subMobile.value)) {
      common.showModal('请输入手机号码中间4位');
      return;
    }
    let querData = {
      mobile: frontNum + subMobile.value + afterNum,
      cardEncryptionStore: MobileBoj.encryptionStore
    };
    let res = await checkMobile_V3(querData);
    if (res) {
      step.value = 2;
      // 发送手机验证码
      getProvingCode();
    }
  } else if (step.value == 2) {
    if (common.isEmpty(provingCode.value)) {
      common.showToast("请输入验证码");
      return;
    }
    if (await verifyCode()) return;
    let querData = {
      ...reqData.value,
      userMobile: frontNum + subMobile.value + afterNum
    };
    queryCardList(querData);
  }
};

/**
 * 发送手机验证码
 */
const getProvingCode = async () => {
  let encryptionStore = '';
  let mobile = '';
  if (serviceType.value == "diMobile") {
    encryptionStore = mobileList.value[mobileIndex.value].encryptionStore;
    mobile = mobileList.value[mobileIndex.value].mobile;
  } else {
    encryptionStore = cardList.value[0].encryptionStore;
    mobile = cardList.value[0].mobile;
  }

  let queryData = {
    mobile: mobile,
    cardEncryptionStore: encryptionStore
  };
  let res = await sendVerificationCode_V3(queryData);
  let resp = res.resp;
  if (!common.isEmpty(resp)) {
    pcId.value = resp[0].pcId;
    common.showModal('发送成功！');
    // Using a local timer ref or variable
    if (timer) clearInterval(timer);
    seconds.value = 60;
    timer = setInterval(() => {
      seconds.value--;
      if (seconds.value <= 0) {
        clearInterval(timer);
      }
    }, 1000);
    // publicFn.countDown.call(this, timer) // Legacy call, replaced with local logic or adapted
  }
};

// 检验验证码
const verifyCode = async () => {
  return new Promise(async (resolve, reject) => {
    if (common.isEmpty(pcId.value)) {
      common.showModal('请先获取验证码');
      resolve(true); // Should block if no code? Original resolved true which means "stop" in caller?
      // In caller: if (await this.checkVerificationCode_V3()) return
      // So returning true stops execution.
    } else {
      let reqData = {
        pcId: pcId.value || '',
        verificationCode: provingCode.value,
      };
      let { resp, resData } = await checkVerificationCode_V3(reqData);
      if (resData.RespCode == 10000) {
        resolve(false); // Success, don't stop
      } else {
        resolve(true); // Fail, stop
      }
    }
  });
};

/**
 * 关闭手动添加就诊卡/手机号不同校验弹窗
 */
const hideAddCardModel = () => {
  // 为通过卡号添加 且为第二步
  if (serviceType.value == 'addCard') {
    if (step.value == 3) {
      showAddCard.value = false;
      step.value = 2;
    } else {
      common.navigateBack(1);
    }
    return;
  }
  step.value = 1;
  showAddCard.value = false;
  showDiMobile.value = false;
};

// radio单选选中赋值
const radioChange = (type: string, e: any) => {
  if (type === 'redioCardType') redioCardType.value = e.detail.value;
  if (type === 'changCardListIndex') changCardListIndex.value = e.detail.value;
};

// 添加人成功统一调用
const addMemberSuccess = async (res: any) => {
  let querData = {
    memberId: res[0].memberId
  };
  // 设置默认就诊人
  let { resp, resData } = await setDefaultMemberV3(querData);
  if (resData.RespCode == "10000") {
    let cList = cardList.value;
    // 判断是否选中默认就诊卡
    for (let i = 0; i < cList.length; i++) {
      // 有选中默认就诊卡，调用设置默认就诊卡接口
      if (cList[i].check) {
        setDefaultCard(res[0].memberId, cList[i]);
      }
    }
    // 添加成功并且设置默认人完成后 重新查询并保存全局就诊人数据信息
    await usePreserMember();
    // 判断是否有存储跳转地址
    if (app.globalData.toUrl) {
      // 判断为（selecteCardOrHos）选择患者界面进入添加
      if (app.globalData.isSelectAdd) {
        app.globalData.isSelectAdd = false;
        common.navigateBack(1);
        return;
      }
      let member = await useGetDefaultMember({
        cardType: app.globalData.cardType,
        url: app.globalData.toUrl
      });
      if (common.isNotEmpty(member)) {
        common.goToUrl(app.globalData.toUrl, {
          skipWay: "redirectTo"
        });
      }
      /**如果是成功  并且全局存了url变量 跳转到全局URL */
    } else {
      common.navigateBack(1);
    }
  }
};

// 亲友关系选择
const selectItem = (type: string, e: any, index?: number) => {
  if (type == 'open') {
    relationIndex = relationCodeIndex.value;
    relationsShow.value = !relationsShow.value;
  }
  if (type == 'cancel') {
    relationCodeIndex.value = relationIndex;
    relationsShow.value = false;
  }
  if (type == 'select' && index !== undefined) {
    relationCodeIndex.value = index;
  }
};

// 校验是否选择亲友关系
const checkRelation = () => {
  if (relationCodeIndex.value === null) {
    common.showModal("请先选择亲友成员与本人关系");
    return true;
  }
  reqData.value.relationCode = relationCodeList.value[relationCodeIndex.value].dictValue;
  return false;
};
</script>

<style lang="scss" scoped>

.container {
  padding: 30upx;
  box-sizing: border-box;
  overflow: auto;
}

.userInfo_box {
  background: white;
  padding: 0 30upx;
  box-sizing: border-box;
  border-radius: 24upx;
  margin-bottom: 30upx;
}

.userInfo_list {
  height: 110upx;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
}


.userInfo_list > text:nth-child(1) {
  width: 35%;
  font-size: 32upx;
  color: #8A8A99;
}
.userInfo_list > text.userInfo_listRela{
  width: 30%;
}
.userInfo_list > text:nth-child(2) {
  width: 60%;
  font-size: 32upx;
  font-weight: bold;
  color: #222326;
}
.userInfo_list > view.relationCode_text{
  width: 70%;
  font-size: 28upx;
  color: var(--dominantColor);
  position: relative;
}


/* 无卡 */
.no_card_tips_box {
  padding: 36upx 30upx;
  box-sizing: border-box;
  background: white;
  border-radius: 24upx;
  margin-bottom: 30upx;
}
.no_card_tips_box .text {
  font-size: 28upx;
  color: #62626D;
  margin-bottom: 14upx;
}

.no_card_tips_box .view {
  font-size: 32upx;
  font-weight: bold;
  color: #222326;
  margin-bottom: 21upx;
  line-height: 44upx;
}
.no_card_tips_box .mb0{
  margin-bottom: 0;
}
/* ====== */

.cardList_box {
  background: white;
  padding: 30upx;
  box-sizing: border-box;
  border-radius: 24upx;
}
.cardList_topText{
  font-size: 32upx;
  font-family: PingFang SC;
  font-weight: bold;
  margin: 6upx 3upx 0 ;
  line-height: 48upx;
  margin-bottom: 35upx;
}

.card_item {
  padding: 35upx 30upx;
  box-sizing: border-box;
  justify-content: space-between;
  box-shadow: 0px 0px 58px 0px rgba(0,0,0,0.05);
  margin-bottom: 24upx;
  border-radius: 20upx;
}
.card_item .member_item_info{
  align-items: flex-start;
}
.card_item .member_item_info .info_memberMsg_name {
  font-size: 32upx;
  font-family: PingFang SC;
  font-weight: 800;
  color: #222326;
  margin-bottom: 21upx;
}
.card_item .member_item_info .info_memberCard {
  font-size: 28upx;
  font-family: PingFang SC;
  color: #62626D;
}

.set_default text{
  font-size: 28upx;
  font-family: PingFang SC;
  color: #979AA8;
}
.set_default image{
  width: 40upx;
  height: 40upx;
  margin-left: 16upx;
}


.choice_card_box {
  width: 100%;
  padding: 0 30upx;
  box-sizing: border-box;
  box-shadow: 0px 0px 58px 0px rgba(0,0,0,0.05);
  border-radius: 20upx;
  justify-content: space-between;
}

.choice_card_item_box {
  padding: 30upx 0;
  justify-content: space-between;
}

.choice_card_item_box:last-child::before {
  border-top: 0;
}

.choice_card_info_box {
  align-items: flex-start;
}

.choice_card_info_box text:nth-child(1) {
  font-size: 32upx;
  color: #222326;
  margin-bottom: 27upx; 
}

.choice_card_info_box text:nth-child(2) {
  color: #8A8A99;
}

.choice_card_box image {
  width: 42upx;
  height: 42upx;
}



.footer_btn_box  {
  width: 100%;
  margin-top: 80upx;
}

.public_btn  {
  margin-bottom: 32upx;
}

.query_card_info {
  width: 100%;
  padding: 36upx 30upx;
  box-sizing: border-box;
  border-radius: 20upx;
  box-shadow: 0px 0px 40px 0px rgba(0,0,0,0.06);
  align-items: flex-start;
}

.query_card_info text:nth-child(1) {
  font-size: 32upx;
  color: #222326;
  margin-bottom: 16upx;
}

.query_card_info text:nth-child(2) {
  font-size: 28upx;
  color: #8A8A99;
  margin-bottom: 0 !important;
}

.query_member_mobile text:nth-child(1) {
  margin-bottom: 16upx;
}

.query_member_mobile text:nth-child(2) {
  margin-bottom: 34upx;
}


/* 添加就诊卡 */
.add_card_mask {
  width: 100%;
  height: 100%;
  background-color: rgba(0,0,0,0.6);
  position: fixed;
  top: 0;
  left: 0;
}

.add_card_box {
  width: 620upx;
  padding: 48upx;
  box-sizing: border-box;
  background: white;
  border-radius: 20upx;
}
.add_card_box .addCardMask_radio{
  margin-top: 28upx;
  margin-right: 20upx;
}
.add_card_title {
  font-size: 36upx;
  font-weight: bold;
  color: #222326;
}

.add_card_tips {
  font-size: 28upx;
  color: #8A8A99;
  margin-top: 27upx;
}

.add_card_input {
  width: 100%;
  height: 100upx;
  line-height: 100upx;
  font-size: 32upx;
  color: #8A8A99;
  padding-left: 53upx;
  box-sizing: border-box;
  background: #F0F1F6;
  margin-top: 40upx;
  border-radius: 100upx;
}

.add_card_footer_btn {
  width: 100%;
  height: 88upx;
  margin-top: 81upx;
  justify-content: space-between;
}

.add_card_footer_btn text {
  width: 49%;
  line-height: 88upx;
  font-size: 34upx;
  font-weight: bold;
  color: #43434A;
  text-align: center;
  border-radius: 88upx;
}

.add_card_footer_btn text:nth-child(1) {
  border: 1upx solid #E6E6E6;
}

.color62626D{
  color: #62626D;
}
.userInfoTow{
  margin-bottom: 30upx;
}
.userInfoTow_text{
  font-size: 28upx;
  font-family: PingFang SC;
  font-weight: 500;
  line-height: 45upx;
}
.userInfoTow_box{
  background: #FFFFFF;
  box-shadow: 0upx 0upx 58upx 0upx rgba(0,0,0,0.05);
  border-radius: 20upx;
  margin-top: 30upx;
  padding: 30upx;
}
.userInfoTow_boxName{
  font-size: 32upx;
  font-weight: bold;
}
.userInfoTow_boxinfo{
  padding: 30upx 30upx 0;
  box-sizing: border-box;
  margin-top: 20upx;
  background: #F8F8FA;
  border-radius: 10upx;
}
.userInfoTow_boxinfo_text{
  padding-bottom: 30upx;
  font-size: 28upx;
  font-family: PingFang SC;
  color: #62626d;
}


/* 弹窗 */
.mainScreenBox {
  width: 100%;
  max-height: 800upx;
  background: white;
  border-radius: 24upx 24upx 0 0;
  position: fixed;
  /* top: 100upx; */
  left: 0;
  bottom: 0;
  z-index: 15;
}

.cententBox {
  max-height: 640upx;
  padding: 40upx 30upx 0;
  box-sizing: border-box;
  overflow: auto;
}

.screenTitle {
  display: inline-block;
  font-weight: 800;
  font-size: 44upx;
  color: #333;
  margin-bottom: 36upx;
}


.screenList {
  display: flex;
  flex-direction: row;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}

.screenList text {
  display: inline-block;
  width: 30%;
  line-height: 80upx;
  margin-right: 5%;
  margin-bottom: 30upx;
  font-size: 40upx;
  text-align: center;
  color: #333;
  padding: 0 20upx;
  background: #F5F7F7;
  border-radius: 80upx;
}

.active_screen {
  color: var(--dominantColor) !important;
  border: 1px solid var(--dominantColor);
}

.screenList text:nth-child(3n) {
  margin-right: 0upx;
}

</style>
