<script setup lang="ts">
import Enabled from '@/components/Enabled.vue';
import { conditioningStatus } from '@/model/options';
import { getConditioningRecordMethod } from '@/request/api/care.api';
import type { ConditioningRecordListModel, ConditioningRecordListQuery } from '@/model/care.model';

import ServicePackageDetail from '@/service/ServicePackageDetail.vue';
import CareProcess from '@/service/CareProgress.vue';
import { usePagination } from 'alova/client';

import { type VxeFormListeners, type VxeFormProps, type VxeGridInstance, type VxeGridListeners, type VxeGridProps, VxeUI } from 'vxe-table';

const model = shallowRef<ConditioningRecordListQuery>();
const searchFormProps = reactive<VxeFormProps<ConditioningRecordListQuery>>({
  titleWidth: 100,
  titleAlign: 'right',
  titleColon: true,
  data: {},
  items: [
    {
      field: 'conditioningWrapName',
      title: '服务包名称',
      span: 6,
      itemRender: { name: 'VxeInput', props: { placeholder: '请输入' } },
    },
    {
      field: 'patientName',
      title: '姓名',
      span: 6,
      itemRender: { name: 'VxeInput', props: { placeholder: '请输入' } },
    },
    {
      field: 'progress',
      title: '调理状态',
      span: 6,
      itemRender: {
        name: 'VxeSelect',
        options: conditioningStatus,
        props: { placeholder: '请选择' ,clearable:true},
      },
    },
    {
      span: 6,
      itemRender: {
        name: 'VxeButtonGroup',
        options: [
          { name: 'submits', type: 'submit', content: '查询', status: 'primary' },
          { name: 'reset', type: 'reset', content: '清空' },
        ],
        events: {
          click(slotParams, { name }) {

          },
        },
      },
    },
  ],
});
const searchFormEmits: VxeFormListeners<ConditioningRecordListQuery> = {
  // 查询随访计划
  submit({ data }) {
    model.value = { ...data };
  },
  // 重置
  reset({ data }) {
    model.value = { ...data };
  },
};
const gridRef = ref<VxeGridInstance<ConditioningRecordListModel>>();
const gridOptions = reactive<VxeGridProps<ConditioningRecordListModel>>({
  id: 'tag-list',
  border: true,
  showOverflow: true,
  height: 'auto',
  autoResize: false,
  syncResize: true,
  scrollY: { enabled: true, gt: 0 },
  toolbarConfig: {
    custom: true,
    zoom: true,
    slots: {
      // buttons: 'handle',
      tools: 'toolbar-extra',
    },
  },
  columnConfig: {
    resizable: true,
  },
  customConfig: {
    storage: true,
  },
  columns: [
    { type: 'seq', width: 70, fixed: 'left' },
    { field: 'patientName', title: '姓名' },
    { field: 'conditioningWrapName', title: '服务包名称' },
    { field: 'diagnosis', title: '疾病名称' },
    { field: 'symptom', title: '证型' },
    { field: 'constitutionGroupName', title: '体质' },
    { field: 'createBy', title: '开具医生' },
    { field: 'estimatedStartDate', title: '开始调养日期' },
    { field: 'progress', title: '调理状态', slots: { default: 'patients' } },
    {
      field: 'action',
      title: '操作',
      align: 'center',
      width: 200,
      cellRender: {
        name: 'VxeButtonGroup',
        props: {
          mode: 'text',
        },
        options: [
          { content: '详情', status: 'primary', name: 'serviceDetail' },
          { content: '调养过程', status: 'primary', name: 'conditioningProcess' },
        ],
        events: {
          click({ row, rowIndex }: { row: ConditioningRecordListModel; rowIndex: number }, { name }: { name: string }) {
            let method;
            if (name === 'serviceDetail') {
              method = serviceDetail;
            } else if (name === 'conditioningProcess') {
              method = conditioningProcess;
            }
            method?.(row, rowIndex);
          },
        },
      },
    },
  ],
  data: [],
});
const gridEvents: VxeGridListeners = {};

const { loading, page, pageSize, total, onSuccess, replace, refresh, remove } = usePagination((page, size) => getConditioningRecordMethod(page, size, model.value), {
  initialData: { data: [], total: 0 },
  initialPage: 1,
  initialPageSize: 100,
  watchingStates: [model],
  immediate: true,
});
onSuccess(({ data: { data } }) => {
  gridRef.value?.loadData(data);
});

onMounted(() => {
  model.value = toRaw(searchFormProps.data);
});

function conditioningProcess(model?: ConditioningRecordListModel) {
  VxeUI.modal.open({
    title: model?.id ? `调养过程` : `新增调养过程`,
    height:window.innerHeight,
    width:window.innerWidth,
    fullscreen:true,
    escClosable: true,
    destroyOnClose: true,
    id: `conditioning-process-modal`,
    remember: true,
    storage: true,
    slots: {
      default() {
        return h(CareProcess, <any>{
          data: model,
          onSubmit(data: ConditioningRecordListModel) {
            refresh(page.value);
            VxeUI.modal.close(`conditioning-process-modal`);
          },
        });
      },
    },
  });
}

function serviceDetail(model?: ConditioningRecordListModel, index?: number) {
  const types='record';
    VxeUI.modal.open({
    id: 'servicePackageDetail-modal',
    title: '调养记录详情',
    height:window.innerHeight,
    width:window.innerWidth,  
    fullscreen:true,
    escClosable: true,
    destroyOnClose: true,
    slots: {
      default() {
        return h(ServicePackageDetail, <any>{
          data: {
            ...model,
            types,
          },
          onVoidSubmit(data: ConditioningRecordListModel) {
            refresh(page.value);
          },
        });
      },
    },
  });
}
const progressTextMap: Record<string, string> = {
  '0': '待付款',
  '1': '已作废',
  '2': '用户取消',
  '3': '未开始',
  '4': '调理中',
  '5': '已完结',
  '6': '待收货'
};


</script>
<template>
  <div class="page-container flex flex-col">
    <header class="flex-none mt-4">
      <vxe-form v-bind="searchFormProps" v-on="searchFormEmits"></vxe-form>
    </header>
    <main class="flex-auto overflow-hidden">
      <vxe-grid ref="gridRef" v-bind="gridOptions" v-on="gridEvents" :loading="loading">
        <template #cell="{ row }">{{ row.progress === 1 ? '已开始' : row.progress === 2 ? '已结束' : '未开始' }} </template>
        <template #patients="{ row }">
          <div>
            {{ progressTextMap[row.progress] || '' }}
          </div>
        </template>
        <template #toolbar-extra>
          <vxe-button style="margin-right: 12px" icon="vxe-icon-repeat" circle @click="refresh(page)"></vxe-button>
        </template>
      </vxe-grid>
    </main>
    <footer class="flex-none">
      <vxe-pager
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :total="total"
        :layouts="['Home', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump', 'End', 'Sizes', 'FullJump', 'Total']"
      />
    </footer>
  </div>
</template>
<style scoped lang="scss">
.page-container {
  padding: 0 24px;
  max-height: var(--page-main-container);
}
</style>
