<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 v-for="customer in customers" :key="customer._id"
            class="flex items-center p-10 mb-10 rounded-sm bg-gray" @click="id = customer._id">
            <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>{{ customer.name }}</view>
              </view>
              <view class="flex items-center">
                <view class="flex-shrink-0 min-w-60">性别：</view>
                <view>{{ customer.sex || '--' }}</view>
              </view>
              <view class="flex items-center">
                <view class="flex-shrink-0 min-w-60">手机号：</view>
                <view>{{ customer.mobile }}</view>
              </view>
            </view>
            <image class="check-icon" :src="`/static/form/${id === customer._id ? 'checked' : 'uncheck'}.svg`"></image>
          </view>
        </view>
      </scroll-view>
      <view class="flex justify-end px-15 pb-10">
        <view class="mr-5 text-base text-dark">以上档案都不对，可以：</view>
        <view class="px-10 text-base leading-normal text-primary border-auto rounded-sm" @click="close()">新增档案</view>
      </view>
      <view class="footer-buttons">
        <button-footer hideden-shadow confirmText="确定" :showCancel="false" @confirm="confirm()" />
      </view>
    </view>
  </uni-popup>
</template>

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

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

const emits = defineEmits(['close', 'confirm'])
const props = defineProps({
  corpName: {
    type: String,
    default: ''
  },
  customers: {
    type: Array,
    default: () => []
  },
  visible: {
    type: Boolean,
    default: false
  }
})
const popup = ref()
const id = ref('')

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

function confirm() {
  if (props.customers.some(i => i._id === id.value && id.value)) {
    emits('confirm', id.value)
  } else {
    toast('请选择档案')
  }
}

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;
}
</style>