<script setup lang="tsx">
import { ContentWrap } from '@common/src/components/ContentWrap'
import { Search } from '@common/src/components/Search'
import { Table, TableColumn } from '@common/src/components/Table'
import { BaseButton } from '@common/src/components/Button'
import { useTable } from '@common/src/hooks/web/useTable'
import { FormSchema } from '@common/src/components/Form'
import { ref, unref, reactive } from 'vue'
import { list, del } from '@/api/xxx'
import { useRouter } from 'vue-router'

const { push } = useRouter()

const props = defineProps({
  tabName: {
    type: String,
    default: '1'
  }
})

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    const res = await list({
      current: unref(currentPage),
      size: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.list,
      total: res.data.totalCount
    }
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

interface ISearch {
  caseId?: string
  userName?: string
  createTimeStart?: string
  createTimeEnd?: string
}
const searchParams = ref<ISearch>({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  if (params.time) {
    searchParams.value.createTimeStart = params.time[0]
    searchParams.value.createTimeEnd = params.time[1]
  }
  getList()
}
const searchSchema = reactive<FormSchema[]>([
  {
    field: 'caseId',
    label: '病例ID',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'userName',
    label: '对象名称',
    component: 'Input',
    componentProps: {
      placeholder: '请输入'
    }
  },
  {
    field: 'time',
    component: 'DatePicker',
    label: '创建时间段',
    componentProps: {
      type: 'datetimerange',
      ...window.dateRangeProps
    }
  }
])
const tableColumns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: '序号',
    type: 'index'
  },
  {
    field: 'caseId',
    label: '病例ID'
  },
  {
    field: 'userName',
    label: '对象名称'
  },
  {
    field: 'taskNo',
    label: '关联事件编号'
  },
  {
    field: 'createTime',
    label: '创建时间'
  },
  {
    field: 'action',
    width: '220px',
    label: '操作',
    slots: {
      default: (data: any) => {
        return (
          <>
            <BaseButton size="small" type="primary" onClick={() => action(data.row, 'detail')}>
              查看详情
            </BaseButton>
            <BaseButton size="small" type="primary" onClick={() => action(data.row, 'edit')}>
              编辑
            </BaseButton>
            <BaseButton size="small" type="danger" onClick={() => delData(data.row)}>
              删除
            </BaseButton>
          </>
        )
      }
    }
  }
])

const actionType = ref('')

const AddAction = () => {
  actionType.value = 'add'
  push({
    path: '/survey-case-manage/write',
    query: {
      actionType: 'add',
      tabName: props.tabName
    }
  })
}

const delData = async (row) => {
  ElMessageBox.confirm('确认删除？', '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    await del({
      caseId: row?.caseId
    })
    getList()
  })
}

const action = (row, type: string) => {
  actionType.value = type
  if (type === 'edit') {
    push({
      path: '/survey-case-manage/write',
      query: {
        actionType: 'edit',
        caseId: row?.caseId
      }
    })
  } else {
    push({
      path: '/survey-case-manage/detail',
      query: {
        caseId: row?.caseId
      }
    })
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <div class="mb-10px">
      <BaseButton type="primary" @click="AddAction">新增</BaseButton>
    </div>

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
