<template>
  <uni-popup ref="popup" type="center" :mask-click="false">
    <view class="bg-white rounded overflow-hidden" style="width: 690rpx;">
      <view class="flex items-center justify-between px-15 py-12 border-b">
        <view class="text-lg font-semibold text-dark">档案绑定验证</view>
        <uni-icons type="closeempty" :size="24" color="#999" @click="close"></uni-icons>
      </view>
      <view class="px-15 pt-15 text-base text-dark">
        您在"{{ corpName }}"医客通平台已存在档案，请选择档案绑定。
      </view>
      <!-- <scroll-view scroll-y="true" class="popup-content-scroll"> -->
      <view class="px-15 py-12">
        <view class="flex items-center p-10 mb-10 rounded-sm bg-gray">
          <view class="flex-grow w-0 mr-5 text-base leading-normal text-dark">
            <view class="flex items-center">
              <view class="flex-shrink-0 min-w-60">姓名：</view>
              <view>{{ hisArchive.name }}</view>
            </view>
            <view class="flex items-center">
              <view class="flex-shrink-0 min-w-60">手机号：</view>
              <view>{{ hisArchive.mobile }}</view>
            </view>
          </view>
        </view>
      </view>
      <view class="footer-buttons">
        <button-footer hideden-shadow confirmText="确定" :showCancel="false" @confirm="confirm()" />
      </view>
    </view>
  </uni-popup>
</template>

<script setup>
import { computed, ref, watch } from 'vue';
import api from '@/utils/api';
import { toast } from '@/utils/widget';

import ButtonFooter from '@/components/button-footer.vue';

const emits = defineEmits(['close', 'confirm'])
const props = defineProps({
  corpId: {
    type: String,
    default: ''
  },
  corpName: {
    type: String,
    default: ''
  },
  customer: {
    type: Object,
    default: () => ({})
  },
  hisArchive: {
    type: Object,
    default: () => ({})
  },
  openid: {
    type: String,
    default: ''
  },
  visible: {
    type: Boolean,
    default: false
  }
})
const popup = ref()
const loading = ref(false);

function close() {
  emits('close')
}

async function confirm() {
  if (loading.value) return;
  loading.value = true;
  const res = await api('bindHisArchive', { id: props.customer._id, corpId: props.corpId, miniAppId: props.openid });
  if (res && res.success) {
    await toast('绑定成功');
    emits('confirm');
  } else {
    toast(res?.message || '绑定失败')
  }
  loading.value = false;
}


watch(() => props.visible, n => {
  if (n) {
    popup.value && popup.value.open();
  } else {
    popup.value && popup.value.close()
  }
})
</script>

<style lang="scss" scoped>
.min-w-60 {
  min-width: 120rpx;
}

.check-icon {
  width: 48rpx;
  height: 48rpx;
}

.popup-content-scroll {
  max-height: 50vh;
}
</style>