<script setup>
/**
 * name：
 * user：yytjsj
 * date：2024年9月25日 10:25:00
 */
import { ref } from "vue";
import { $api, $response, $image } from "@/api";
import { onShow } from "@dcloudio/uni-app";
import { useStore } from "@/store";
const $store = useStore();

let buyInfo = ref({}); // 购买信息
let groupInfo = ref({});
let comboInfo = ref({});
let itemsInfo = ref({});
let userInfo = ref(""); // 病人信息
let truePrice = ref(0); // 总价
let yytjInfo = ref({}); // 预约信息

const mountedAction = async () => {
  uni.showLoading({
    title: "加载中",
  });
  console.log($store.getYytjInfo(), "123123");
  buyInfo.value = $store.getBuyInfo();
  userInfo.value = buyInfo.value.group_id
    ? $store.getGroupInfo()
    : $store.getUser();
    let time = setTimeout(() => {
      getnmrList();
      clearTimeout(time);
    }, 500);
};

const getnmrList = async () => {
  yytjInfo.value = $store.getYytjInfo();
  console.log(yytjInfo.value);
  const response = await $api("BuyInfo", buyInfo.value);
  $response(response, () => {
    if (buyInfo.value.group_id && response.data.group_info.length) {
      // 团检
      groupInfo.value = response.data.group_info[0];
    }
    comboInfo.value = response.data.combo_info;
    itemsInfo.value = response.data.items_info;
    truePrice.value = response.data.true_price;
    
    if(!yytjInfo.value?.nmr_list?.length){
      yytjInfo.value.nmr_list = response.data.nmr_list.map((val) => {
        return {
          ...val,
          time: "",
        };
      });;
      yytjInfo.value.nmr_list.push({
        item_id: -1,
        name: "体检时间",
        time: ""
      })
    }
    uni.hideLoading();
  });
};

const comfrimyy = async () => {
  let plan_id = "";
  let plan_nmr_id = "";
  // if(!yytjInfo.value?.doctor_name){
  //   uni.$lu.toast("请选择医生");
  //   return;
  // }
  // console.log(yytjInfo.value?.nmr_list);
  // for(let i = 0; i < yytjInfo.value?.nmr_list?.length; i++){
  //   if(!yytjInfo.value?.nmr_list[i].time){
  //     uni.$lu.toast("请选择预约时间");
  //     return;
  //   }
  // }

  // if(yytjInfo.value?.nmr_list?.length > 1){
  //   plan_nmr_id = yytjInfo.value?.nmr_list[0]?.id;
  // }
  // if(yytjInfo.value?.nmr_list?.length > 1){
  //   plan_id = yytjInfo.value?.nmr_list[yytjInfo.value?.nmr_list.length - 1]?.id;
  // }

  console.log(groupInfo.value);


  let obj = {
    person_id: buyInfo.value.person_id,
    type: buyInfo.value.group_id ? 2 : 1,
    hospital: buyInfo.value.hospital,
    group_id: buyInfo.value.group_id,
    combo_id: buyInfo.value?.combo_id || (groupInfo.value?.combo_id?"":userInfo.value?.combo_id) || "",
    item_ids: buyInfo.value.item_ids,
    plan_id: plan_id,
    $plan_nmr_id: plan_nmr_id,
  }
  console.log(obj);

  const response = await $api("OrderCreate", obj);
  // return
  $response(response, () => {
    if(response.status){
      if(response.data.action == "pay"){
        StartPay();
      }else{
        uni.navigateTo({
          url: "/pages/buy/done/done?id="+response.data.orderid
        })
      }
    }
  })
};

const StartPay = async (id) => { //继续支付
  uni.showLoading()
  const response = await $api('StartPay', {
   openid: localStorage.getItem('OPENID'),
   id: id,
  })
  uni.hideLoading()
  $response(response, () => {
   if (response.status) {
    let info = response.data.info
    let p = '&appid=' + info.appid + '&sub_org_code=' + info.sub_org_code + '&ghzid=' + info
     .ghzid + '&orderid=' + info.orderid + '&order_desc=' + info.order_desc + '&amount=' +
     info.amount + '&notify_url=' + info.notify_url + '&timestamp=' + info.timestamp +
     '&nonce=' + info.nonce + '&signature=' + info.signature;
    console.log(p);
    wx.miniProgram.navigateTo({
     url: '/pages/other/entry/index?path=/pages/physical-examination/payment/index' +
      p
    })
   }
  })
 }

const config_ref = ref(null);
const configRef = (e) => {
  if (!config_ref.value) {
    config_ref.value = e;
    mountedAction();
  }
};

onShow(() => {
  if (!!config_ref.value) {
    mountedAction();
  }
});

const toRouter = (url, status) => {
  if(status){
    uni.setStorageSync("yytjInfoS", yytjInfo.value);
  }
  uni.navigateTo({
    url: url,
  });
};
</script>
<template>
  <view>
    <view v-if="!!$store.config">
      <view :ref="configRef"></view>
    </view>
    <view class="px-20rpx box-border pb-200rpx comboInfo">
      <view
        style="box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0, 0, 0, 0.04)"
        class="p-16rpx pb-20rpx pt-30rpx rounded-15rpx bg-#fff"
      >
        <view
          class="flex w-full b-0 b-solid b-#E1ECEE mb-7rpx"
          :class="{
            'b-b-1 pb-25rpx': itemsInfo?.items?.length
          }"
          v-if="!buyInfo.group_id && comboInfo?.items?.length"
        >
          <!-- 1 个检套餐 显示套餐信息 -->
          <image
            v-if="comboInfo.img"
            :src="$image(comboInfo.img)"
            class="w-190rpx h-190rpx mr-37rpx"
            mode="widthFix"
          />
          <view class="flex flex-col grow">
            <text>{{ comboInfo.combo_name }}</text>
            <view class="mt-13rpx mb-18rpx">
              <uni-tag
                v-for="(val, i) in comboInfo.tags"
                :key="i"
                size="small"
                :inverted="true"
                :text="val.text"
                class="mr-8rpx"
                :custom-style="
                  'background-color:' +
                  val.color +
                  ';border-color:' +
                  val.color +
                  '; color:' +
                  val.text_color +
                  ';'
                "
              />
            </view>
            <view v-if="comboInfo.tags2?.length">
              <text
                v-for="(val, i) in comboInfo.tags2"
                :key="i"
                class="text-#8B8B8B text-20rpx line-height-[1] px-5rpx b-0 b-solid b-#8B8B8B"
                :class="i == comboInfo.tags2?.length - 1 ? 'b-r-0' : 'b-r-2'"
                >{{ val }}</text
              >
            </view>
            <view class="mt-20rpx w-full flex items-end">
              <text class="text-18rpx text-#EC3D15">¥</text>
              <text class="text-38rpx text-#EC3D15 mx-10rpx -mb-8rpx">{{
                comboInfo.price
              }}</text>
              <text class="text-18rpx text-#878787 line-through">{{
                comboInfo.original_price
              }}</text>
              <text class="text-18rpx text-#878787 ml-auto mr-55rpx"
                >已售{{ comboInfo.sale_count }}</text
              >
            </view>
          </view>
        </view>
        <view
          v-if="buyInfo.group_id"
          class="b-0 b-solid b-#E1ECEE pb-25rpx mb-7rpx"
          :class="{
            'b-b-1': itemsInfo?.items?.length
          }"
        >
          <!-- 3团检套餐 4团检自选 都要显示头像和名字 -->
          <view class="flex items-center">
            <image
              src="@/static/assets/userm.png"
              class="w-70rpx h-70rpx mr-35rpx"
            />
            <view class="flex items-center grow">
              <text class="text-#707070 text-24rpx">您好，</text>
              <text class="text-#0E0E0E text-32rpx">{{ groupInfo.name }}</text>
            </view>
          </view>
          <view class="text-24rpx mt-40rpx">
            <!-- 3团检套餐 4团检自选 都要显示单位部门等信息 -->
            <view class="pb-30rpx">
              <text class="text-#8B8B8B">单位名称：</text>
              <text>{{ groupInfo.group_name }}</text>
            </view>
            <view class="pb-30rpx">
              <text class="text-#8B8B8B">部门名称：</text>
              <text>{{ groupInfo.bumen_name }}</text>
            </view>
            <view
              class="pb-30rpx"
              v-if="groupInfo.combo_name || comboInfo?.combo_name"
            >
              <text class="text-#8B8B8B">套餐名称：</text>
              <text>{{ groupInfo?.combo_name || comboInfo?.combo_name }}</text>
            </view>
            <view>
              <text class="text-#8B8B8B">体检额度：</text>
              <text>{{ groupInfo.tongshou_xiane }}</text>
            </view>
          </view>
        </view>
        <view class="pt-15rpx" v-if="itemsInfo?.items?.length">
          <view class="text-30rpx text-#0E0E0E pb-30rpx"> 自选项目 </view>
          <view class="text-#8B8B8B text-24rpx">
            <view
              class="flex mb-20rpx"
              v-for="(val, index) in itemsInfo?.items.length > 3
                ? 3
                : itemsInfo?.items"
              :key="index"
            >
              <text class="whitespace-nowrap"
                >{{ itemsInfo?.items[index]?.keshi_name }} -
              </text>
              <view class="line-clamp-2">
                <text
                  class="ml-10rpx text-#0E0E0E"
                  v-for="(v, i) in itemsInfo?.items[index]?.children"
                  :key="i"
                  >{{ v.name }}</text
                >
              </view>
            </view>
          </view>
        </view>
      </view>
      <view
        style="box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0, 0, 0, 0.04)"
        class="p-16rpx pb-20rpx pt-30rpx rounded-15rpx bg-#fff my-16rpx"
      >
        <view
          class="text-#0E0E0E text-30rpx b-0 b-solid b-b-1 b-#E1ECEE pb-20rpx mb-35rpx"
          >预约人信息</view
        >
        <view>
          <view class="text-28rpx text-#000 bold mb-20rpx">{{
            userInfo.name
          }}</view>
          <view class="text-24rpx text-#8B8B8B mb-30rpx between">
            <text>{{ userInfo.sex == 1 ? "男性" : "女性" }}</text>
            <text class="ml-20rpx">{{
              userInfo.married == 1
                ? "已婚"
                : userInfo.married == 2
                ? "未婚"
                : "未知"
            }}</text>
            <text class="ml-20rpx mr-auto">{{ userInfo.phone }}</text>
            <button
              v-if="!buyInfo.group_id"
              @click="toRouter('/pages/user/choose/choose')"
              class="text-24rpx text-#fff bg-#31AEB6 rounded-full px-26rpx h55rpx !m-0 center !ml-auto"
            >
              更改预约人
            </button>
          </view>
          <view
            class="text-24rpx text-#040404 mb-30rpx"
            @click="toRouter('/pages/main/yytjsj/yytjsj', 1)"
          >
            <view
              class="mb-20rpx between"
              v-for="(val, index) in yytjInfo?.nmr_list"
              :key="index"
              v-if="yytjInfo?.nmr_list?.length"
            >
              <text>{{ val.name }}</text>
              <text class="ml-20rpx mr-auto text-#239EA3">{{ val.time }}</text>
              <text>{{ val.time ? "重新预约" : "预约时间" }}</text>
            </view>
            <view class="mb-20rpx between">
              <text>体检医生</text>
              <text>{{ yytjInfo?.doctor_name || "选择医生" }}</text>
            </view>
          </view>
        </view>
      </view>

      <view
        style="box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0, 0, 0, 0.04)"
        class="px-16rpx rounded-15rpx bg-#fff"
      >
        <view
          class="pt-42rpx pb-36rpx b-0 b-solid b-b-1 b-#E1ECEE"
          v-if="(comboInfo && comboInfo?.items?.length) || groupInfo?.items?.length"
        >
          <view class="text-#0E0E0E text-30rpx mb-35rpx">套餐名称</view>
          <view class="text-24rpx between">
            <text class="text-#0E0E0E">{{ comboInfo?.combo_name || groupInfo?.combo_name }}</text>
            <text>¥{{ comboInfo?.price || groupInfo?.sixi_zong_ji_jin_e }}</text>
          </view>
        </view>

        <view
          class="pt-42rpx pb-36rpx b-0 b-solid b-b-1 b-#E1ECEE"
          v-if="itemsInfo && itemsInfo?.items?.length"
        >
          <view class="text-#0E0E0E text-30rpx mb-35rpx">自选项目</view>
          <view
            class="text-24rpx"
            v-for="(val, index) in itemsInfo?.items"
            :key="index"
          >
            <view
              v-for="(v, i) in val.children"
              :key="i"
              class="between mb-30rpx"
              :class="{
                '!mb-0':
                  index == itemsInfo?.items?.length - 1 &&
                  i == val.children.length - 1,
              }"
            >
              <text class="text-#8B8B8B">{{ val.keshi_name }} -</text>
              <text class="text-#0E0E0E mr-auto ml-20rpx">{{ v.name }}</text>
              <text class="text-#6D6D6D">¥{{ v.price }}</text>
            </view>
          </view>
        </view>
      </view>
      <view
        class="shadow-[0px_1px_1px_0px_rgba(0,0,0,0.08)] fixed bottom-0 left-0 w-full bg-#fff py-30rpx pl-55rpx pr-45rpx box-border between"
      >
        <view class="text-#ED6907 text-48rpx center items-end line-height-[1]">
          <text class="text-24rpx">¥</text>
          <text>{{ truePrice }}</text>
        </view>
        <button
          @click="comfrimyy"
          class="w-276rpx h-76rpx center text-#fff text-30rpx rounded-full bg-#239EA3 !m-0"
        >
          提交预约
        </button>
      </view>
    </view>
  </view>
</template>
<style scoped lang="scss">
.activeTime {
  background-color: #239ea3;
  color: #fff;
}

.acitvetc {
  background-color: #e0f1f2;
}

.comboInfo {
  position: relative;
  z-index: 1;
  &::after {
    content: "";
    width: 100%;
    height: 50rpx;
    background-color: #239ea3;
    position: absolute;
    left: 0;
    top: 0;
    z-index: -1;
  }

  .active {
    color: #239ea3;
    font-weight: bold;
    position: relative;

    &::after {
      content: "";
      width: 26rpx;
      height: 3rpx;
      background-color: #239ea3;
      position: absolute;
      left: 50%;
      bottom: -10rpx;
      transform: translateX(-50%);
    }
  }
}
</style>
