<template>
  <Mask :width="696" :height="409" title="收费" :is-show="show" @close="close" :show-footer="true">
    <template #default>
      <div class="content" v-loading="loading">
        <div class="btns">
          <div
              class="btn"
              style="cursor: pointer"
              :style="{
              backgroundColor:
                selectedIndex === index
                  ? item.activeBackgroundColor
                  : hoverIndex === index
                  ? item.activeBackgroundColor
                  : item.backgrountColor
         }"
              v-for="(item, index) in btnsList"
              :key="index"
              @click="changePriceType(item.type,index)"
              @mouseover="hoverIndex = index"
              @mouseleave="hoverIndex = null"
          >
            <img
                class="img"
                :src="selectedIndex == index || hoverIndex == index ? item.imgActive : item.img"
                alt=""
                srcset=""
            />
            <span class="pay-left-text" :style="{color: selectedIndex==index?'#fff':''}">{{ item.name }}</span>
          </div>
        </div>
        <div class="pay">
          <div class="pay-left">
            <span class="pay-left-title">应收金额</span>
            <span class="pay-left-money">￥{{ retailOrder.totalPrice }}</span>
          </div>
          <div class="social">
            <CardPay v-model="socialCard"/>
          </div>
        </div>
      </div>
    </template>
    <template #footer>
      <div class="footer">
        <el-checkbox v-model="printReceipt">同时打印凭证</el-checkbox>
        <div class="default-btn" @click="completeSettlement()" type="primary">完成收费</div>
      </div>
    </template>
  </Mask>
  <PersonalPayment ref="psnPaymentRef" @orderCompleted="orderCompleted" @orderCancel="orderCanceled"></PersonalPayment>
</template>

<script setup lang="ts">

import {nextTick, onMounted, onUnmounted, ref} from "vue";
import CloseBtn from "@/components/CloseBtn.vue";
import {post} from "@/utils/request.ts";
import Mask from "@/components/common/Mask.vue";
import CardPay from "@/components/charge/CardPay.vue";
import {medTypeJson, priceBtnList} from "@/assets/config/constants.ts"
import {useWsStore} from "@/stores/wsStore.ts";
import PersonalPayment from "@/components/charge/PersonalPayment.vue";
import {ElMessage} from "element-plus";

const wsStore = useWsStore();
const socialCard = ref<any>({
  data: null,
  mdtrtCertType: null,
  mdtrtCertNo: null,
  payInfo: {}
});
const loading = ref(false);
const show = ref(false)
const printReceipt = ref(true);
const payType = ref(null);
const retailOrder = ref<any>(null);
const close = () => {
  show.value = false;
  payType.value = null;
  selectedIndex.value = null
  loading.value = false
}
const init = async (code: any, psnNo: any) => {
  retailOrder.value = await post("charge/getByCode", {code: code});
  if (psnNo != null) {
    let result: any = await post("social/person/getSocialInfoByPsnNo", {psnNo: psnNo});
    if (result) {
      socialCard.value.data = result;
      socialCard.value.mdtrtCertType = result.mdtrtCertType;
      socialCard.value.mdtrtCertNo = result.mdtrtCertNo;
      socialCard.value.payInfo.selfpay_prop_type = result.insuinfo[0].insutype;
    }
  }
  show.value = true;
}
defineExpose({init})
const changePriceType = (type: any, index: any) => {
  payType.value = type;
  selectedIndex.value = index
}
const emit = defineEmits(['orderCompleted', 'orderCanceled'])
const completeSettlement = () => {
  if (!retailOrder.value) {
    return;
  }
  if (payType.value == null) {
    ElMessage.warning("请选择支付方式")
    return
  }
  if (payType.value == 1) {
    //医保结算
    socialSettlement();
  } else {
    //其他结算
    loading.value = true;
    post('charge/completeOrder', {id: retailOrder.value.id, payType: payType.value}, {catch_error:true}).then((res: any) => {
      orderCompleted()
      loading.value = false
    }).catch((e)=>{
      loading.value = false
      ElMessage.error(e)
    })
  }

}


const orderCompleted = () => {
  loading.value = false;
  show.value = false;
  ElMessage.success("保存成功")
  emit('orderCompleted', printReceipt.value)
}
const orderCanceled = () => {
  show.value = false;
  emit('orderCanceled')
}

const getBalcByInsutype = (type: any) => {
  let balc = 0;
  for (const item of socialCard.value.data.insuinfo) {
    if (item.insutype == type) {
      balc = item.balc;
      break;
    }
  }
  return balc
}

const socialSettlement = () => {
  if (socialCard.value.data == null) {
    ElMessage.warning("请刷卡")
    return;
  }
  loading.value = true;
  const params = {
    mdtrtCertType: socialCard.value.mdtrtCertType,
    mdtrtCertNo: socialCard.value.mdtrtCertNo,
    insutype: socialCard.value.payInfo.selfpay_prop_type,
    changeOrderCode: retailOrder.value.code,
  }
  post("charge/uploadCostDetails", {changeOrderCode: retailOrder.value.code}).then((res: any) => {
    //预结算
    post("charge/socialPrePay", {...params}).then((res: any) => {
      openPsnPayment(res, params)
    })
  })


}
const psnPaymentRef = ref();
const openPsnPayment = (payInfo: any, orderInfo: any) => {
  loading.value = false;
  nextTick(() => {
    psnPaymentRef.value?.open(payInfo, orderInfo);
  })

}
const selectedIndex = ref<number | null>(null)
const hoverIndex = ref<number | null>(null)
const isShow = ref<boolean>(false)
const btnsList = [
  {
    type: 1,
    name: '医保支付',
    backgrountColor: '#F0F4FD ',
    activeBackgroundColor: '#4d6de4',
    img: '/static/images/slices/1.png',
    imgActive: '/static/images/slices/1-active.png',
  },
  {
    type: 2,
    name: '微信支付',
    backgrountColor: '#ecf8fe',
    activeBackgroundColor: '#80cfdb',
    img: '/static/images/slices/2.png',
    imgActive: '/static/images/slices/2-active.png',
  },
  {
    type: 3,
    name: '支付宝',
    backgrountColor: '#fff5ec',
    activeBackgroundColor: '#fdb376',
    img: '/static/images/slices/3.png',
    imgActive: '/static/images/slices/3-active.png',
  },
  {
    type: 4,
    name: '现金支付',
    backgrountColor: '#ffeeee',
    activeBackgroundColor: '#ff8686',
    img: '/static/images/slices/4.png',
    imgActive: '/static/images/slices/4-active.png',
  },
  {
    type: 5,
    name: '其他支付',
    backgrountColor: '#e5f9ff',
    activeBackgroundColor: '#30e3ca',
    img: '/static/images/slices/5.png',
    imgActive: '/static/images/slices/5-active.png',
  },
]
</script>


<style scoped lang="scss">
.panel {
  .price {
    height: 50px;
    text-align: center;
    font-size: 25px;
    font-weight: 600;
    color: rgba(237, 120, 23, 0.8);
  }

  .price-type {
    display: flex;
    margin-top: 20px;
    justify-content: space-between;
    flex-wrap: wrap;
    align-items: center;

    .btn {
      height: 45px;
      width: 95px;
      font-size: 14px;
      border-radius: 10px;
      display: flex;
      background: #efecec;
      margin-bottom: 10px;
      justify-content: center;
      align-items: center;
      padding: 10px;
      cursor: pointer;

      .image {
        width: 16px;
        height: 16px;
        margin-right: 3px;
        border-radius: 23px;
        background-color: #ffffff;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      &:hover {
        //color: #fff;
        //background-color: #409EFF;
        border: 1px solid #409EFF;
        cursor: pointer;
      }
    }

    .active {
      color: #fff !important;
      background-color: #409EFF;
      cursor: pointer;
    }

  }
}

.content {
  flex: 1;
  padding: 0 24px;
  display: flex;
  flex-direction: column;

  .btns {
    height: 86px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .btn {
      width: 120px;
      height: 46px;
      border-radius: 8px;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 500;
      font-size: 16px;
      color: #333333;
      font-style: normal;

      .img {
        width: 24px;
        height: 24px;
        margin-right: 4px;
      }

      &:hover {
        color: #fff;
      }
    }
  }

  .pay {
    flex: 1;
    display: flex;
    justify-content: space-between;

    .pay-left {
      padding-top: 24px;

      &-title {
        display: block;
        font-weight: 800;
        font-size: 18px;
        color: #333333;
        font-style: normal;
        margin-bottom: 22px;
      }

      &-money {
        font-weight: bold;
        font-size: 42px;
        color: #ff0000;
        font-style: normal;
      }
    }

    .pay-right {
      width: 436px;
      background: #f9fafc;
      border-radius: 8px;
      padding: 24px;
      display: flex;
      flex-direction: column;

      &-title {
        font-weight: 800;
        font-size: 18px;
        color: #333333;
        font-style: normal;
      }

      &-content {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        font-weight: 500;
        font-size: 16px;
        color: #999999;
        font-style: normal;
      }

      &-btns {
        height: 38px;
        display: flex;
        justify-content: space-between;

        &-btn {
          width: 91px;
          height: 38px;
          background: #ffffff;
          border-radius: 8px;
          border: 1px solid #4d6de4;
          font-weight: bold;
          font-size: 16px;
          color: #4d6de4;
          font-style: normal;
          text-align: center;
          line-height: 38px;

          &:hover {
            background: #4d6de4;
            color: #ffffff;
          }
        }
      }
    }
  }
}

.footer {
  height: 100%;
  display: flex;
  justify-content: space-between;
  padding: 0 24px;
  align-items: center;
}
</style>