<script lang="ts" setup>
import { ref } from 'vue';
import { VxeUI } from 'vxe-pc-ui';
import IntroduceProjectList from '@/service/IntroduceProjectList.vue';
import { getSystemCpListMethod, getCopyCwMethod } from '@/request/api/care.api';
import { usePagination } from 'alova/client';
import type { SystemCwModel } from '@/model/care.model';
type FollowModel = Partial<SystemCwModel>;
const props = defineProps<{ data: FollowModel; institutionId: string,institutionName: string }>();
const emit = defineEmits(['update:data']);
const tableData = ref<SystemCwModel[]>([]);
const { loading, page, pageSize, total, onSuccess, replace, refresh, remove } = usePagination((page, size) => getSystemCpListMethod(page, size), {
  initialData: { data: [], total: 0 },
  initialPage: 1,
  initialPageSize: 100,
  immediate: true,
});
onSuccess(({ data: { data } }) => {
  tableData.value = data;
  loading.value = false;
});
const id = ref<string>('');
async function handleSubmit() {
  
  const res: any = await getCopyCwMethod(parms.value);
  if (res && res.items && res.items.length > 0) {
    res.items.forEach((item: any) => {
      delete item.conditioningProgramDetail.id;
      item.frequencyTypeing = item.frequencyType ? [item.frequencyType] : [];
    });
    const newItems = res.items.filter((item: any) => {
      return item.isForCopyCw == 'Y';
    });
    props.data.items = [...newItems];
    props.data.price = res.price;
    props.data.name = res.name;
    if (res.conditioningWrapPatientMatchRule) {
      props.data.conditioningWrapPatientMatchRule.diagnoseDiseaseNames = res.conditioningWrapPatientMatchRule.diagnoseDiseaseNames || [];
      props.data.conditioningWrapPatientMatchRule.diagnoseSyndromeNames = res.conditioningWrapPatientMatchRule.diagnoseSyndromeNames || [];
      props.data.conditioningWrapPatientMatchRule.constitutionGroupNames = res.conditioningWrapPatientMatchRule.constitutionGroupNames || [];
      props.data.conditioningWrapPatientMatchRule.sex = res.conditioningWrapPatientMatchRule.sex || '';
      props.data.conditioningWrapPatientMatchRule.age = res.conditioningWrapPatientMatchRule.age || '';
      props.data.conditioningWrapPatientMatchRule.willillStateNames = res.conditioningWrapPatientMatchRule.willillStateNames || [];
    }
  }
}
const parms = ref<any>({});
async function handleSelect(model?: SystemCwModel) {
  if (model?.id) {
    id.value = model.id;
    parms.value = {
      types: 'institution',
      id: model.id,
      institutionId: props.institutionId,
    };
  }
  // 这里写你的选择逻辑
  VxeUI.modal.open({
    title: '引入项目',
    width: 920,
    height: 700,
    escClosable: true,
    destroyOnClose: true,
    id: `introduceProjectList-modal`,
    remember: true,
    storage: true,
    slots: {
      default() {
        return h(IntroduceProjectList, {
          data: { ...props.data },
          id: id.value,
          institutionId: props.institutionId,
          institutionName: props.institutionName,
          onSubmit() {
            // 提交之后刷新列表
            handleSubmit();
            VxeUI.modal.close(`introduceProjectList-modal`);
          },
        });
      },
    },
  });
}
</script>

<template>
  <div style="background: #fff; padding: 32px; border-radius: 6px; box-shadow: 0 2px 8px #0001">
    <vxe-table :data="tableData" border>
      <vxe-column field="name" title="服务包名称" />
      <vxe-column field="conditioningWrapPatientMatchRule.diagnoseDiseaseNames" title="专病" />
      <vxe-column field="conditioningWrapPatientMatchRule.diagnoseSyndromeNames" title="证型" />
      <vxe-column field="conditioningWrapPatientMatchRule.constitutionGroupNames" title="体质" />
      <vxe-column field="price" title="价格（元）" />
      <vxe-column title="操作" width="80" align="center">
        <template #default="{ row }">
          <a style="color: #1677ff; cursor: pointer" @click="handleSelect(row)">选择</a>
        </template>
      </vxe-column>
    </vxe-table>
  </div>
</template>
