/*
 * @Author: code4eat awesomedema@gmail.com
 * @Date: 2023-03-03 11:30:33
 * @LastEditors: code4eat awesomedema@gmail.com
 * @LastEditTime: 2024-09-25 14:59:59
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/pubDicTypeMana/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { createFromIconfontCN } from '@ant-design/icons';

import { ModalForm, ProFormText } from '@ant-design/pro-form';
import FormItem from 'antd/es/form/FormItem';
import { Input } from 'antd';
import { useEffect, useRef, useState } from 'react';

import {
  getData,
  downloadTemplateReq,
  importPatientInfoDataPost,
} from './service';

import './style.less';
import moment from 'moment';
import 'moment/locale/zh-cn';
import locale from 'antd/es/date-picker/locale/zh_CN';
import KCIMPagecontainer from '@/components/KCIMPageContainer';
import { KCIMTable } from '@/components/KCIMTable';
import KCIMUpload from '@/components/KCIMUpload';
import { ActionType, ProColumns } from '@ant-design/pro-components';

const IconFont = createFromIconfontCN({
  scriptUrl: '',
});

const currentData = `${new Date().getFullYear()}-${(new Date().getMonth() + 1)
  .toString()
  .padStart(2, '0')}`;

const columnsOne = [
  {
    title: '患者ID',
    dataIndex: 'patientNo',
    ellipsis: true,
    width: 120,
  },
  {
    title: '患者姓名',
    dataIndex: 'name',
    ellipsis: true,
    width: 120,
  },

  {
    title: '住院号/门诊号',
    dataIndex: 'visitNo',
    ellipsis: true,
    width: 120,
  },
  {
    title: '主诊断代码',
    dataIndex: 'primaryDiagCode',
    ellipsis: true,
  },
  {
    title: '主诊断名称',
    dataIndex: 'primaryDiagName',
    ellipsis: true,
  },
  {
    title: '次诊断代码',
    ellipsis: true,
    dataIndex: 'secondaryDiagCode',
  },
  {
    title: '次诊断名称',
    ellipsis: true,
    width: 220,
    dataIndex: 'secondaryDiagName',
  },
  {
    title: '主手术/操作代码',
    dataIndex: 'primaryOperationCode',
    ellipsis: true,
  },
  {
    title: '主手术/操作名称',
    dataIndex: 'primaryOperationName',
    width: 260,
    ellipsis: true,
  },
  {
    title: '次手术/操作代码',
    ellipsis: true,
    dataIndex: 'secondaryOperationCode',
  },
  {
    title: '次手术/操作名称',
    ellipsis: true,
    width: 300,
    dataIndex: 'secondaryOperationName',
  },
  {
    title: '入院日期',
    width: 180,
    dataIndex: 'inHospitalTime',
    ellipsis: true,
  },
];

const columnsTwo: ProColumns[] = [
  {
    title: '患者ID',
    dataIndex: 'patientNo',
    ellipsis: true,
    width: 120,
  },
  {
    title: '患者姓名',
    dataIndex: 'name',
    ellipsis: true,
    width: 120,
  },

  {
    title: '住院号/门诊号',
    dataIndex: 'visitNo',
    ellipsis: true,
    width: 120,
  },
  {
    title: '病区',
    ellipsis: true,
    renderText(text, record, index, action) {
      const { inpatientWardCode, inpatientWardName } = record;
      return `[${inpatientWardCode}]${inpatientWardName}`;
    },
  },
  {
    title: 'DRG/DIP分组名称',
    dataIndex: 'groupName',
    width: 400,
    ellipsis: true,
    renderText(text, record, index, action) {
      const { groupCode, groupName } = record;
      return `[${groupCode}]${groupName}`;
    },
  },
  {
    title: '主诊断代码',
    dataIndex: 'primaryDiagCode',
    ellipsis: true,
  },
  {
    title: '主诊断名称',
    dataIndex: 'primaryDiagName',
    ellipsis: true,
  },
  {
    title: '次诊断代码',
    ellipsis: true,
    dataIndex: 'secondaryDiagCode',
  },
  {
    title: '次诊断名称',
    ellipsis: true,
    width: 220,
    dataIndex: 'secondaryDiagName',
  },
  {
    title: '主手术/操作代码',
    dataIndex: 'primaryOperationCode',
    ellipsis: true,
  },
  {
    title: '主手术/操作名称',
    dataIndex: 'primaryOperationName',
    width: 260,
    ellipsis: true,
  },
  {
    title: '次手术/操作代码',
    ellipsis: true,
    dataIndex: 'secondaryOperationCode',
  },
  {
    title: '次手术/操作名称',
    ellipsis: true,
    width: 300,
    dataIndex: 'secondaryOperationName',
  },
  {
    title: '临床路径',
    dataIndex: 'clinicalPathwayName',
    width: 260,
    ellipsis: true,
    renderText(text, record, index, action) {
      const { clinicalPathwayCode, clinicalPathwayName } = record;
      return `[${clinicalPathwayCode}]${clinicalPathwayName}`;
    },
  },
  {
    title: '入院日期',
    width: 180,
    dataIndex: 'inHospitalTime',
    ellipsis: true,
  },
  {
    title: '出院日期',
    width: 180,
    dataIndex: 'outHospitalTime',
    ellipsis: true,
  },
  {
    title: '住院天数',
    width: 100,
    dataIndex: 'inHospitalDays',
    ellipsis: true,
  },
  {
    title: '床日数',
    width: 80,
    dataIndex: 'bedDays',
    ellipsis: true,
  },
  {
    title: '当月床日数',
    width: 100,
    dataIndex: 'currentBedDays',
    ellipsis: true,
  },
];

export default function PatientInfoImport({
  date,
  ifShowGetBtn,
  ifShowCancelBtn,
  currentSlectedDepCode,
  type,
  cancelHandle,
  getHandle,
}: {
  date: string;
  ifShowGetBtn: boolean;
  ifShowCancelBtn: boolean;
  currentSlectedDepCode: string;
  type: number;
  cancelHandle: () => Promise<boolean>;
  getHandle: () => Promise<boolean>;
}) {
  const [computeDate, set_computeDate] = useState(currentData);
  const [tableDataFilterParams, set_tableDataFilterParams] = useState<any>({
    computeDate: currentData,
  });
  const [tableDataSearchKeywords, set_tableDataSearchKeywords] = useState('');
  const tableRef = useRef<ActionType>();
  const [columns, set_columns] = useState<any[]>([]);

  const getTableData = async (params: any) => {
    if (date && currentSlectedDepCode && type) {
      const resp = await getData({ ...params, ...tableDataFilterParams });
      if (resp) {
        const { list, totalCount, pageSize, totalPage } = resp;
        return {
          data: list,
          success: true,
          total: totalCount,
          pageSize: pageSize,
          totalPage: totalPage,
        };
      }
    }

    return [];
  };

  const tableDataSearchHandle = (paramName: string) => {
    set_tableDataFilterParams({
      ...tableDataFilterParams,
      [`${paramName}`]: tableDataSearchKeywords,
    });
  };

  const downloadTemplate = async () => {
    await downloadTemplateReq();
  };

  useEffect(() => {
    if (date && currentSlectedDepCode && type) {
      set_computeDate(date);
      set_tableDataFilterParams({
        ...tableDataFilterParams,
        computeDate: date,
        departmentCode: currentSlectedDepCode,
        type: type,
      });
    }

    if (type == 3) {
      set_columns(columnsTwo);
    } else {
      set_columns(columnsOne);
    }
  }, [date, currentSlectedDepCode, type]);

  return (
    <KCIMPagecontainer className="patientInfoImport" title={false}>
      <div className="toolBar">
        <div className="filter">
          <div className="filterItem" style={{ width: 205 }}>
            <span className="label" style={{ whiteSpace: 'nowrap' }}>
              {' '}
              检索：
            </span>
            <Input
              placeholder={'患者ID/姓名'}
              allowClear
              autoComplete="off"
              suffix={
                <IconFont
                  type="iconsousuo"
                  style={{ color: '#99A6BF' }}
                  onClick={() => tableDataSearchHandle('filter')}
                />
              }
              onChange={(e) => {
                set_tableDataSearchKeywords(e.target.value);
                if (e.target.value.length == 0) {
                  set_tableDataFilterParams({
                    ...tableDataFilterParams,
                    filter: '',
                  });
                }
              }}
              onPressEnter={(e) => {
                set_tableDataFilterParams({
                  ...tableDataFilterParams,
                  filter: (e.target as HTMLInputElement).value,
                });
              }}
            />
          </div>
        </div>
        <div className="btnGroup">
          {ifShowCancelBtn && (
            <span
              onClick={() =>
                cancelHandle().then((isOk) => {
                  if (isOk) tableRef.current?.reload();
                })
              }
            >
              撤销
            </span>
          )}
          {ifShowGetBtn && (
            <span
              onClick={() =>
                getHandle().then((isOk) => {
                  if (isOk) tableRef.current?.reload();
                })
              }
            >
              获取
            </span>
          )}
          {/* {importData()} */}
        </div>
      </div>
      <div style={{ marginTop: 16 }}>
        <KCIMTable
          columns={columns}
          actionRef={tableRef}
          rowKey="id"
          scroll={{ x: type == 3 ? 4000 : 2000, y: `calc(100vh - 346px)` }}
          params={tableDataFilterParams}
          request={(params) => getTableData(params)}
        />
      </div>
    </KCIMPagecontainer>
  );
}
