import React, { useState, useEffect, useCallback } from 'react';
import { Table, Button, Card, Space, Tag, Modal, Form, Input, InputNumber, Select, DatePicker, message, Descriptions, Image, Divider, Tabs } from 'antd';
import { PlusOutlined, EditOutlined, DeleteOutlined, EyeOutlined, UndoOutlined, PlusCircleOutlined, MinusCircleOutlined } from '@ant-design/icons';
import dayjs from 'dayjs';
import { useAuthStore } from '../../store/authStore';
import FileUpload from '../../components/FileUpload';
import useFormDraft from '../../hooks/useFormDraft';

const { Option } = Select;
const { TextArea } = Input;

interface DetailItem {
  id?: string;
  description: string;
  amount: number;
  category: string;
  attachments?: string[];
}

const ReimbursementsPage: React.FC = () => {
  const { user } = useAuthStore();
  const [reimbursements, setReimbursements] = useState<any[]>([]);
  const [completedReimbursements, setCompletedReimbursements] = useState<any[]>([]);
  const [activeTab, setActiveTab] = useState('active');
  const [projects, setProjects] = useState<any[]>([]);
  const [loading, setLoading] = useState(false);
  const [modalVisible, setModalVisible] = useState(false);
  const [detailModalVisible, setDetailModalVisible] = useState(false);
  const [editingId, setEditingId] = useState<number | null>(null);
  const [selectedRecord, setSelectedRecord] = useState<any>(null);
  const [form] = Form.useForm();
  const [deleteForm] = Form.useForm();
  const [currentEditingStatus, setCurrentEditingStatus] = useState<string>('');
  const [detailItems, setDetailItems] = useState<DetailItem[]>([]);
  const [exchangeRates, setExchangeRates] = useState<Record<string, number>>({});

  // 表单草稿保护
  const { save: saveDraft, restore: restoreDraft, clear: clearDraft, hasDraft } = useFormDraft({
    form,
    storageKey: 'reimbursement_create',
    onRestore: (data) => {
      if (data.detailItems) setDetailItems(data.detailItems);
    },
  });

  // 保存草稿（包含 detailItems 外部状态）
  const handleFormChange = useCallback(() => {
    saveDraft({ detailItems });
  }, [saveDraft, detailItems]);

  useEffect(() => {
    fetchReimbursements();
    fetchProjects();
    fetchExchangeRates();
  }, []);

  const fetchReimbursements = async () => {
    setLoading(true);
    try {
      const res = await fetch('/api/reimbursements');
      const data = await res.json();
      if (data.success) {
        // 分离活跃的和已完结的报销
        const active = data.data.filter((item: any) => ['pending', 'approved', 'rejected', 'withdrawn', 'pending_edit'].includes(item.status));
        const completed = data.data.filter((item: any) => ['executed', 'paid'].includes(item.status));
        setReimbursements(active);
        setCompletedReimbursements(completed);
      }
    } catch (error) {
      message.error('获取报销列表失败');
    } finally {
      setLoading(false);
    }
  };

  const fetchProjects = async () => {
    try {
      const res = await fetch('/api/projects');
      const data = await res.json();
      if (data.success) setProjects(data.data);
    } catch (error) {}
  };

  const fetchExchangeRates = async () => {
    try {
      const res = await fetch('/api/exchange-rates/latest');
      const data = await res.json();
      if (data.success) {
        const rates: Record<string, number> = {};
        Object.keys(data.data).forEach(key => {
          rates[key] = parseFloat(data.data[key]) || 1;
        });
        setExchangeRates(rates);
      }
    } catch (error) {}
  };

  const convertToCNY = (amount: number, currency: string): number => {
    if (currency === 'CNY') return amount;
    const rateKey = 'CNY_' + currency;
    const rate = exchangeRates[rateKey] || 1;
    return amount / rate;
  };

  const handleCreate = () => {
    setEditingId(null);
    setDetailItems([]);
    form.resetFields();
    form.setFieldsValue({
      reimbursement_date: dayjs(),
      currency: 'CNY',
      expense_type: 'company',
      applicant: user?.name || user?.username || '当前用户',
      attachments: []
    });
    setModalVisible(true);
    // 检查是否有草稿，提示用户是否恢复（在下一帧执行，确保 form 状态已更新）
    setTimeout(() => {
      if (hasDraft()) {
        Modal.confirm({
          title: '发现未完成的草稿',
          content: '检测到上次未提交的报销申请，是否恢复？',
          okText: '恢复草稿',
          cancelText: '重新填写',
          onOk: () => {
            restoreDraft();
          },
          onCancel: () => {
            clearDraft();
            form.resetFields();
            form.setFieldsValue({
              reimbursement_date: dayjs(),
              currency: 'CNY',
              expense_type: 'company',
              applicant: user?.name || user?.username || '当前用户',
              attachments: []
            });
            setDetailItems([]);
          },
        });
      }
    }, 0);
  };

  const handleEdit = (record: any) => {
    setEditingId(record.id);
    setCurrentEditingStatus(record.status);
    setDetailItems(record.detail_items || []);
    form.setFieldsValue({
      ...record,
      reimbursement_date: record.reimbursement_date ? dayjs(record.reimbursement_date) : null,
      attachments: record.attachments || []
    });
    setModalVisible(true);
  };

  const handleView = (record: any) => {
    setSelectedRecord(record);
    setDetailModalVisible(true);
  };

  const handleDelete = async (id: number) => {
    // 重置删除表单
    deleteForm.resetFields();
    
    Modal.confirm({
      title: '确认删除',
      content: (
        <Form form={deleteForm} layout="vertical">
          <Form.Item 
            name="password" 
            label="请输入密码确认删除"
            rules={[{ required: true, message: '请输入密码' }]}
          >
            <Input.Password placeholder="输入密码" />
          </Form.Item>
        </Form>
      ),
      onOk: async () => {
        try {
          const values = await deleteForm.validateFields();
          // 这里可以添加密码验证逻辑，暂时直接删除
          await fetch('/api/reimbursements/' + id, { method: 'DELETE' });
          message.success('删除成功');
          fetchReimbursements();
        } catch (error) {
          message.error('删除失败');
        }
      }
    });
  };

  const handleWithdraw = async (id: number) => {
    Modal.confirm({
      title: '确认撤回',
      content: '撤回后可重新编辑提交，确认撤回吗？',
      onOk: async () => {
        try {
          await fetch('/api/reimbursements/' + id + '/withdraw', { method: 'POST' });
          message.success('已撤回，可重新编辑');
          fetchReimbursements();
        } catch (error) {
          message.error('撤回失败');
        }
      }
    });
  };

  // 保存操作：只保存信息，不改变状态
  const handleSave = async () => {
    try {
      const values = await form.validateFields();
      // 保存时使用编辑时的状态
      const saveStatus = currentEditingStatus || 'pending_edit';
      const data = {
        ...values,
        reimbursement_date: values.reimbursement_date?.format('YYYY-MM-DD'),
        detail_items: detailItems,
        amount: detailItems.reduce((sum, item) => sum + (item.amount || 0), 0),
        applicant: user?.name || user?.username,
        status: saveStatus
      };
      const url = editingId ? '/api/reimbursements/' + editingId : '/api/reimbursements';
      const method = editingId ? 'PUT' : 'POST';
      const res = await fetch(url, {
        method,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      });
      const result = await res.json();
      if (result.success) {
        message.success(editingId ? '保存成功' : '创建成功');
        clearDraft();
        setModalVisible(false);
        fetchReimbursements();
      } else {
        message.error(result.error || '保存失败');
      }
    } catch (error) {
      message.error('保存失败');
    }
  };

  // 提交操作：提交到待审批状态
  const handleSubmit = async () => {
    try {
      const values = await form.validateFields();
      // 提交时使用pending状态
      const saveStatus = 'pending';
      const data = {
        ...values,
        reimbursement_date: values.reimbursement_date?.format('YYYY-MM-DD'),
        detail_items: detailItems,
        amount: detailItems.reduce((sum, item) => sum + (item.amount || 0), 0),
        applicant: user?.name || user?.username,
        status: saveStatus
      };
      const url = editingId ? '/api/reimbursements/' + editingId : '/api/reimbursements';
      const method = editingId ? 'PUT' : 'POST';
      const res = await fetch(url, {
        method,
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify(data)
      });
      const result = await res.json();
      if (result.success) {
        message.success(editingId ? '提交成功' : '创建成功');
        clearDraft();
        setModalVisible(false);
        fetchReimbursements();
      } else {
        message.error(result.error || '提交失败');
      }
    } catch (error) {
      message.error('提交失败');
    }
  };

  const handleSubmitAndSubmit = async () => {
    await handleSubmit();
  };

  const addDetailItem = () => {
    const newItems = [...detailItems, { description: '', amount: 0, category: '', attachments: [] }];
    setDetailItems(newItems);
    saveDraft({ detailItems: newItems });
  };

  const updateDetailItem = (index: number, field: keyof DetailItem, value: any) => {
    const newItems = [...detailItems];
    newItems[index] = { ...newItems[index], [field]: value };
    setDetailItems(newItems);
    saveDraft({ detailItems: newItems });
  };

  const removeDetailItem = (index: number) => {
    const newItems = detailItems.filter((_, i) => i !== index);
    setDetailItems(newItems);
    saveDraft({ detailItems: newItems });
  };

  const getStatusTag = (status: string) => {
    const statusMap: Record<string, { color: string; text: string }> = {
      pending: { color: 'processing', text: '待审批' },
      approved: { color: 'success', text: '已批准' },
      rejected: { color: 'error', text: '已退回' },
      withdrawn: { color: 'default', text: '已撤回' },
      paid: { color: 'blue', text: '已付款' },
      pending_edit: { color: 'warning', text: '待编辑' },
    };
    const config = statusMap[status] || { color: 'default', text: status };
    return <Tag color={config.color}>{config.text}</Tag>;
  };

  const formatAmount = (amount: number, currency: string = 'CNY') => {
    const symbols: Record<string, string> = { CNY: '¥', USD: '$', LAK: '₭', THB: '฿' };
    return (symbols[currency] || '¥') + (amount || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 });
  };

  const columns = [
    { title: '事由', dataIndex: 'reason', key: 'reason', ellipsis: true, render: (v: string, r: any) => <a onClick={() => handleView(r)}>{v}</a> },
    { title: '申请人', dataIndex: 'applicant', key: 'applicant', width: 80 },
    { title: '金额', dataIndex: 'amount', key: 'amount', width: 140, render: (v: number, r: any) => (
        <>
          <div>{formatAmount(v, r.currency)}</div>
          {r.currency !== 'CNY' && r.amount_cny && <div style={{ color: '#999', fontSize: 12 }}>≈ ¥{r.amount_cny.toLocaleString('zh-CN', {minimumFractionDigits: 2})}</div>}
        </>
      ) },
    { title: '报销日期', dataIndex: 'reimbursement_date', key: 'reimbursement_date', width: 100 },
    { title: '状态', dataIndex: 'status', key: 'status', width: 100, render: (status: string) => getStatusTag(status) },
    { title: '编号', dataIndex: 'reimbursement_code', key: 'reimbursement_code', width: 120 },
    {
      title: '操作', key: 'action', width: 250,
      render: (_: any, record: any) => (
        <Space wrap>
          <Button size="small" icon={<EyeOutlined />} onClick={() => handleView(record)}>详情</Button>
          {record.status === 'pending' && (
            <>
              <Button size="small" icon={<UndoOutlined />} onClick={() => handleWithdraw(record.id)}>撤回</Button>
            </>
          )}
          {(record.status === 'rejected' || record.status === 'withdrawn' || record.status === 'pending_edit') && (
            <Button size="small" type="primary" icon={<EditOutlined />} onClick={() => handleEdit(record)}>编辑重提</Button>
          )}
          <Button size="small" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record.id)}>删除</Button>
        </Space>
      )
    }
  ];

  const currency = Form.useWatch('currency', form);
  const expenseType = Form.useWatch('expense_type', form);
  const totalAmount = detailItems.reduce((sum, item) => sum + (item.amount || 0), 0);
  const amountCNY = React.useMemo(() => {
    return totalAmount && currency ? convertToCNY(totalAmount, currency) : 0;
  }, [totalAmount, currency, exchangeRates]);

  return (
    <div style={{ padding: 24 }}>
      <div style={{ marginBottom: 24 }}>
        <h2 style={{ marginBottom: 8 }}>报销申请</h2>
        <p style={{ color: '#888', marginBottom: 0 }}>管理费用报销申请</p>
      </div>

      <Card extra={<Button type="primary" icon={<PlusOutlined />} onClick={handleCreate}>新建报销</Button>}>
        <Tabs activeKey={activeTab} onChange={setActiveTab}>
          <Tabs.TabPane tab="活跃申请" key="active">
            <Table dataSource={reimbursements} columns={columns} rowKey="id" loading={loading} pagination={{ pageSize: 20 }} size="middle" scroll={{ x: 1100 }} />
          </Tabs.TabPane>
          <Tabs.TabPane tab="已完结" key="completed">
            <Table dataSource={completedReimbursements} columns={columns} rowKey="id" loading={loading} pagination={{ pageSize: 20 }} size="middle" scroll={{ x: 1100 }} />
          </Tabs.TabPane>
        </Tabs>
      </Card>

      <Modal
        title={editingId ? '编辑报销' : '新建报销'}
        open={modalVisible}
        onCancel={() => {
          if (form.isFieldsTouched()) {
            Modal.confirm({
              title: '确认关闭',
              content: '表单数据尚未保存，关闭后可通过草稿恢复。确定关闭吗？',
              okText: '关闭',
              cancelText: '继续编辑',
              onOk: () => {
                saveDraft({ detailItems });
                setModalVisible(false);
              },
            });
          } else {
            setModalVisible(false);
          }
        }}
        maskClosable={false}
        footer={[
          <Button key="cancel" onClick={() => {
            if (form.isFieldsTouched()) {
              Modal.confirm({
                title: '确认关闭',
                content: '表单数据尚未保存，关闭后可通过草稿恢复。确定关闭吗？',
                okText: '关闭',
                cancelText: '继续编辑',
                onOk: () => {
                  saveDraft({ detailItems });
                  setModalVisible(false);
                },
              });
            } else {
              setModalVisible(false);
            }
          }}>取消</Button>,
          <Button key="save" onClick={handleSave}>保存</Button>,
          <Button key="submit" type="primary" onClick={handleSubmitAndSubmit}>提交</Button>
        ]}
        width={900}
      >
        <Form form={form} layout="vertical" onValuesChange={handleFormChange}>
          <Form.Item name="applicant" label="申请人">
            <Input disabled style={{ color: 'rgba(0,0,0,0.85)', backgroundColor: '#f5f5f5' }} />
          </Form.Item>
          
          <Form.Item name="reimbursement_date" label="报销日期" rules={[{ required: true }]}>
            <DatePicker style={{ width: '100%' }} />
          </Form.Item>

          <Form.Item name="currency" label="币种" rules={[{ required: true }]}>
            <Select style={{ width: 200 }}>
              <Option value="CNY">人民币 (CNY)</Option>
              <Option value="USD">美元 (USD)</Option>
              <Option value="LAK">老挝基普 (LAK)</Option>
              <Option value="THB">泰铢 (THB)</Option>
            </Select>
          </Form.Item>

          <Form.Item name="expense_type" label="支出类型" rules={[{ required: true }]}>
            <Select placeholder="选择支出类型" onChange={() => form.setFieldsValue({ project_id: undefined })}>
              <Option value="company">公司支出</Option>
              <Option value="project">项目支出</Option>
            </Select>
          </Form.Item>

          {expenseType === 'project' && (
            <Form.Item name="project_id" label="选择项目" rules={[{ required: true, message: '请选择项目' }]}>
              <Select placeholder="选择项目" showSearch optionFilterProp="children">
                {projects.map((p: any) => <Option key={p.id} value={p.id}>{p.name}</Option>)}
              </Select>
            </Form.Item>
          )}

          <Form.Item name="reason" label="事由" rules={[{ required: true }]}>
            <TextArea rows={2} placeholder="请输入报销事由" />
          </Form.Item>

          <Divider>报销明细</Divider>
          
          <div style={{ marginBottom: 16 }}>
            <Button type="dashed" icon={<PlusCircleOutlined />} onClick={addDetailItem}>添加明细</Button>
            <span style={{ marginLeft: 16, color: '#888' }}>
              合计: {formatAmount(totalAmount, currency)}
              {currency !== 'CNY' && amountCNY > 0 && (
                <span style={{ color: '#999', marginLeft: 8, fontSize: 12 }}>≈ ¥{amountCNY.toLocaleString('zh-CN', {minimumFractionDigits: 2})}</span>
              )}
            </span>
          </div>

          {detailItems.map((item, index) => (
            <Card key={index} size="small" style={{ marginBottom: 16 }}>
              <div style={{ display: 'flex', gap: 16, flexWrap: 'wrap', alignItems: 'flex-start' }}>
                <div style={{ flex: 1, minWidth: 200 }}>
                  <label style={{ display: 'block', marginBottom: 4, color: '#666' }}>费用说明</label>
                  <Input 
                    value={item.description} 
                    onChange={(e) => updateDetailItem(index, 'description', e.target.value)}
                    placeholder="费用说明"
                  />
                </div>
                <div style={{ width: 180 }}>
                  <label style={{ display: 'block', marginBottom: 4, color: '#666' }}>支出分类</label>
                  <Select 
                    value={item.category} 
                    onChange={(v) => updateDetailItem(index, 'category', v)}
                    style={{ width: '100%' }}
                    placeholder="选择支出分类"
                  >
                    {expenseType === 'project' ? (
                      <>
                        <Option value="accommodation">住宿</Option>
                        <Option value="food">餐饮</Option>
                        <Option value="fuel">加油</Option>
                        <Option value="materials">零散材料</Option>
                        <Option value="customer_relations">客户关系</Option>
                        <Option value="subcontract_relations">分包关系</Option>
                        <Option value="edl_relations">EDL关系</Option>
                        <Option value="extra_construction">额外施工</Option>
                        <Option value="other">其他</Option>
                      </>
                    ) : (
                      <>
                        <Option value="general_operations">通用运营（房租/耗材）</Option>
                        <Option value="transportation">交通通勤</Option>
                        <Option value="business_expansion">业扩营销</Option>
                        <Option value="power_system_relations">电力系统关系</Option>
                        <Option value="employee_benefits">员工福利</Option>
                        <Option value="express_logistics">快递物流</Option>
                        <Option value="other">其他</Option>
                      </>
                    )}
                  </Select>
                </div>
                <div style={{ width: 150 }}>
                  <label style={{ display: 'block', marginBottom: 4, color: '#666' }}>金额</label>
                  <InputNumber 
                    value={item.amount} 
                    onChange={(v) => updateDetailItem(index, 'amount', v)}
                    min={0} 
                    precision={2}
                    style={{ width: '100%' }}
                    placeholder="金额"
                  />
                </div>
                <div style={{ flex: 2, minWidth: 300 }}>
                  <label style={{ display: 'block', marginBottom: 4, color: '#666' }}>凭证附件</label>
                  <FileUpload 
                    value={item.attachments || []}
                    onChange={(urls) => updateDetailItem(index, 'attachments', urls)}
                    maxCount={3}
                    accept="image/*"
                  />
                </div>
                <Button type="text" danger icon={<MinusCircleOutlined />} onClick={() => removeDetailItem(index)} style={{ marginTop: 24 }} />
              </div>
            </Card>
          ))}

          <Divider>主附件</Divider>
          <Form.Item name="attachments" label="整体凭证附件">
            <FileUpload 
              value={form.getFieldValue('attachments')} 
              onChange={(urls) => form.setFieldsValue({ attachments: urls })} 
              maxCount={9} 
              accept="image/*" 
            />
          </Form.Item>
        </Form>
      </Modal>

      <Modal title="报销详情" open={detailModalVisible} onCancel={() => setDetailModalVisible(false)} footer={null} width={900}>
        {selectedRecord && (
          <>
            <Descriptions bordered column={2} size="small">
              <Descriptions.Item label="报销编号">{selectedRecord.reimbursement_code}</Descriptions.Item>
              <Descriptions.Item label="状态">{getStatusTag(selectedRecord.status)}</Descriptions.Item>
              <Descriptions.Item label="申请人">{selectedRecord.applicant}</Descriptions.Item>
              <Descriptions.Item label="报销日期">{selectedRecord.reimbursement_date}</Descriptions.Item>
              <Descriptions.Item label="币种">{selectedRecord.currency}</Descriptions.Item>
              <Descriptions.Item label="支出类型">{selectedRecord.expense_type === 'project' ? '项目支出' : '公司支出'}</Descriptions.Item>
              {selectedRecord.project_id && (
                <Descriptions.Item label="关联项目" span={2}>
                  {projects.find(p => p.id === selectedRecord.project_id)?.name || '未知项目'}
                </Descriptions.Item>
              )}
              <Descriptions.Item label="金额">
                {formatAmount(selectedRecord.amount, selectedRecord.currency)}
                {selectedRecord.currency !== 'CNY' && selectedRecord.amount_cny && (
                  <span style={{ color: '#999', marginLeft: 8 }}>≈ ¥{selectedRecord.amount_cny.toLocaleString('zh-CN', {minimumFractionDigits: 2})}</span>
                )}
              </Descriptions.Item>
              <Descriptions.Item label="事由" span={2}>{selectedRecord.reason}</Descriptions.Item>
            </Descriptions>

            {Array.isArray(selectedRecord.detail_items) && selectedRecord.detail_items.length > 0 && (
              <>
                <Divider>报销明细</Divider>
                <Table 
                  dataSource={selectedRecord.detail_items} 
                  rowKey="id"
                  size="small"
                  pagination={false}
                  columns={[
                    { title: '费用说明', dataIndex: 'description', key: 'description' },
                    { 
                      title: '支出分类', 
                      dataIndex: 'category', 
                      key: 'category', 
                      render: (v: string) => {
                        const categoryMap: Record<string, string> = {
                          // Project expense categories
                          accommodation: '住宿',
                          food: '餐饮',
                          fuel: '加油',
                          materials: '零散材料',
                          customer_relations: '客户关系',
                          subcontract_relations: '分包关系',
                          edl_relations: 'EDL关系',
                          extra_construction: '额外施工',
                          // Company expense categories
                          general_operations: '通用运营（房租/耗材）',
                          transportation: '交通通勤',
                          business_expansion: '业扩营销',
                          power_system_relations: '电力系统关系',
                          employee_benefits: '员工福利',
                          express_logistics: '快递物流',
                          other: '其他'
                        };
                        return categoryMap[v] || v;
                      }
                    },
                    { title: '金额', dataIndex: 'amount', key: 'amount', render: (v: number) => formatAmount(v, selectedRecord.currency) },
                    { title: '附件', dataIndex: 'attachments', key: 'attachments', render: (v: string[]) => v?.length ? `${v.length}张` : '-' }
                  ]}
                />
              </>
            )}
            
            {Array.isArray(selectedRecord.attachments) && selectedRecord.attachments.length > 0 && (
              <>
                <Divider>整体凭证附件</Divider>
                <Image.PreviewGroup>
                  <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
                    {selectedRecord.attachments.map((url: string, index: number) => (
                      <Image key={index} src={url} width={100} height={100} style={{ objectFit: 'cover', borderRadius: 4 }} />
                    ))}
                  </div>
                </Image.PreviewGroup>
              </>
            )}
          </>
        )}
      </Modal>
    </div>
  );
};

export default ReimbursementsPage;
