import { useState, useCallback, useMemo } from 'react';
import {
  Button, DatePicker, Form, Input, message, Modal, Popconfirm,
  Result, Select, Space, Table, Tag,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
import dayjs from 'dayjs';

import {
  consentApi,
  type Consent,
  type CreateConsentReq,
  CONSENT_TYPE_OPTIONS,
  CONSENT_SCOPE_OPTIONS,
  CONSENT_STATUS_COLOR,
  CONSENT_STATUS_LABEL,
} from '../../api/health/consents';
import { PageContainer } from '../../components/PageContainer';
import { usePermission } from '../../hooks/usePermission';

export default function ConsentList() {
  const { hasPermission } = usePermission('health.consent.list');

  const [data, setData] = useState<Consent[]>([]);
  const [total, setTotal] = useState(0);
  const [page, setPage] = useState(1);
  const [loading, setLoading] = useState(false);

  const [patientId, setPatientId] = useState('');
  const [searchInput, setSearchInput] = useState('');

  const [createModalOpen, setCreateModalOpen] = useState(false);
  const [submitting, setSubmitting] = useState(false);
  const [form] = Form.useForm();

  const pageSize = 20;

  const fetchData = useCallback(async (pid: string, p: number) => {
    if (!pid) return;
    setLoading(true);
    try {
      const resp = await consentApi.list(pid, { page: p, page_size: pageSize });
      setData(resp.data);
      setTotal(resp.total);
      setPage(p);
    } catch {
      message.error('加载知情同意记录失败');
    } finally {
      setLoading(false);
    }
  }, []);

  const handleSearch = () => {
    const pid = searchInput.trim();
    if (!pid) {
      message.warning('请输入患者 ID');
      return;
    }
    setPatientId(pid);
    fetchData(pid, 1);
  };

  const handleCreate = () => {
    form.resetFields();
    form.setFieldsValue({ patient_id: patientId });
    setCreateModalOpen(true);
  };

  const handleSubmit = async () => {
    try {
      const values = await form.validateFields();
      setSubmitting(true);
      const req: CreateConsentReq = {
        ...values,
        expiry_date: values.expiry_date?.format('YYYY-MM-DD'),
      };
      await consentApi.grant(req);
      message.success('知情同意已签署');
      setCreateModalOpen(false);
      fetchData(patientId, page);
    } catch {
      // validation
    } finally {
      setSubmitting(false);
    }
  };

  const handleRevoke = async (record: Consent) => {
    try {
      await consentApi.revoke(record.id, { version: record.version });
      message.success('知情同意已撤销');
      fetchData(patientId, page);
    } catch {
      message.error('撤销失败');
    }
  };

  const columns: ColumnsType<Consent> = useMemo(() => [
    {
      title: '同意类型',
      dataIndex: 'consent_type',
      width: 110,
      render: (v: string) => {
        const label = CONSENT_TYPE_OPTIONS.find((o) => o.value === v)?.label;
        return label ?? v;
      },
    },
    {
      title: '同意范围',
      dataIndex: 'consent_scope',
      width: 110,
      render: (v: string) => {
        const label = CONSENT_SCOPE_OPTIONS.find((o) => o.value === v)?.label;
        return label ?? v;
      },
    },
    {
      title: '状态',
      dataIndex: 'status',
      width: 90,
      render: (v: string) => (
        <Tag color={CONSENT_STATUS_COLOR[v] ?? 'default'}>{CONSENT_STATUS_LABEL[v] ?? v}</Tag>
      ),
    },
    {
      title: '签署方式',
      dataIndex: 'consent_method',
      width: 100,
      render: (v: string) => v ?? '-',
    },
    {
      title: '见证人',
      dataIndex: 'witness_name',
      width: 100,
      render: (v: string) => v ?? '-',
    },
    {
      title: '签署时间',
      dataIndex: 'granted_at',
      width: 170,
      render: (v: string) => v ? dayjs(v).format('YYYY-MM-DD HH:mm') : '-',
    },
    {
      title: '到期日',
      dataIndex: 'expiry_date',
      width: 110,
      render: (v: string) => v ? dayjs(v).format('YYYY-MM-DD') : '无限期',
    },
    {
      title: '操作',
      width: 100,
      render: (_, record) => (
        record.status === 'active' ? (
          <Popconfirm title="确定撤销此知情同意？" onConfirm={() => handleRevoke(record)}>
            <Button size="small" danger>撤销</Button>
          </Popconfirm>
        ) : null
      ),
    },
  ], [patientId, page]);

  if (!hasPermission) {
    return <Result status="403" title="权限不足" subTitle="您没有查看知情同意记录的权限" />;
  }

  return (
    <PageContainer
      title="知情同意管理"
      actions={patientId ? <Button type="primary" onClick={handleCreate}>签署同意</Button> : undefined}
    >
      <Space style={{ marginBottom: 16 }} wrap>
        <Input.Search
          placeholder="输入患者 ID 搜索"
          value={searchInput}
          onChange={(e) => setSearchInput(e.target.value)}
          onSearch={handleSearch}
          style={{ width: 360 }}
          enterButton="查询"
        />
        {patientId && (
          <Tag color="blue">当前患者: {patientId}</Tag>
        )}
      </Space>

      {!patientId ? (
        <Result title="请输入患者 ID 查询知情同意记录" />
      ) : (
        <Table<Consent>
          rowKey="id"
          columns={columns}
          dataSource={data}
          loading={loading}
          pagination={{
            current: page,
            pageSize,
            total,
            showTotal: (t) => `共 ${t} 条`,
            onChange: (p) => fetchData(patientId, p),
          }}
        />
      )}

      <Modal
        title="签署知情同意"
        open={createModalOpen}
        onOk={handleSubmit}
        onCancel={() => setCreateModalOpen(false)}
        confirmLoading={submitting}
        width={560}
      >
        <Form form={form} layout="vertical">
          <Form.Item name="patient_id" label="患者 ID" rules={[{ required: true }]}>
            <Input disabled />
          </Form.Item>
          <Space style={{ width: '100%' }} size="middle">
            <Form.Item name="consent_type" label="同意类型" rules={[{ required: true, message: '请选择类型' }]} style={{ width: 240 }}>
              <Select options={CONSENT_TYPE_OPTIONS} placeholder="选择类型" />
            </Form.Item>
            <Form.Item name="consent_scope" label="同意范围" rules={[{ required: true, message: '请选择范围' }]} style={{ width: 240 }}>
              <Select options={CONSENT_SCOPE_OPTIONS} placeholder="选择范围" />
            </Form.Item>
          </Space>
          <Space style={{ width: '100%' }} size="middle">
            <Form.Item name="consent_method" label="签署方式" style={{ width: 240 }}>
              <Select
                placeholder="选择方式"
                allowClear
                options={[
                  { label: '纸质签署', value: 'paper' },
                  { label: '电子签署', value: 'electronic' },
                  { label: '口头同意', value: 'verbal' },
                ]}
              />
            </Form.Item>
            <Form.Item name="expiry_date" label="到期日期" style={{ width: 240 }}>
              <DatePicker style={{ width: '100%' }} />
            </Form.Item>
          </Space>
          <Form.Item name="witness_name" label="见证人">
            <Input placeholder="见证人姓名（可选）" />
          </Form.Item>
          <Form.Item name="notes" label="备注">
            <Input.TextArea rows={2} />
          </Form.Item>
        </Form>
      </Modal>
    </PageContainer>
  );
}
