import { useEffect, useState } from 'react';
import {
  Button, Drawer, Form, Input, Modal, Radio, Select, Space, Table, Tag, Typography, message,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { request, type HqProfile } from '../lib/api';
import { ACCOUNT_STATUS_LABELS, fmtTime } from '../lib/constants';
import { useAdminList } from '../lib/useAdminList';

type Row = {
  id: string;
  phone: string;
  loginName: string | null;
  hasPassword: boolean;
  name: string;
  adminRole: string;
  status: string;
  lastLoginAt: string | null;
  createdAt: string;
};

const ROLE_LABELS: Record<string, string> = {
  SUPER_ADMIN: '超级管理员',
  OPS: '运营',
  FINANCE: '财务',
  CUSTOMER_SERVICE: '客服',
};

export default function HqAccountsPage() {
  const [profile, setProfile] = useState<HqProfile | null>(null);
  const [form] = Form.useForm();
  const [editForm] = Form.useForm();
  const [createForm] = Form.useForm();
  const [filters, setFilters] = useState<Record<string, string>>({});
  const { data, loading, page, pageSize, setPage, setPageSize, reload } = useAdminList<Row>(
    '/admin/hq-accounts',
    () => {
      const qs = new URLSearchParams();
      if (filters.phone) qs.set('phone', filters.phone);
      if (filters.adminRole) qs.set('adminRole', filters.adminRole);
      if (filters.status) qs.set('status', filters.status);
      return qs;
    },
    [filters],
  );
  const [detail, setDetail] = useState<Row | null>(null);
  const [drawerOpen, setDrawerOpen] = useState(false);
  const [createOpen, setCreateOpen] = useState(false);
  const credentialType = Form.useWatch('credentialType', createForm) ?? 'phone';
  const isSuperAdmin = profile?.adminRole === 'SUPER_ADMIN';

  useEffect(() => {
    request<HqProfile>('/admin/auth/me').then(setProfile).catch(() => {});
  }, []);

  const columns: ColumnsType<Row> = [
    { title: '姓名', dataIndex: 'name' },
    { title: '用户名', dataIndex: 'loginName', width: 120, render: (v) => v || '—' },
    { title: '手机', dataIndex: 'phone', width: 130 },
    {
      title: '登录方式',
      width: 110,
      render: (_, r) => (
        <Space size={4}>
          {r.hasPassword ? <Tag color="blue">密码</Tag> : null}
          <Tag>短信</Tag>
        </Space>
      ),
    },
    { title: '角色', dataIndex: 'adminRole', width: 110, render: (r) => ROLE_LABELS[r] || r },
    { title: '状态', dataIndex: 'status', width: 90, render: (s) => <Tag>{ACCOUNT_STATUS_LABELS[s] || s}</Tag> },
    { title: '最后登录', dataIndex: 'lastLoginAt', width: 160, render: fmtTime },
    {
      title: '操作', width: 80,
      render: (_, row) => (
        <Button type="link" size="small" disabled={!isSuperAdmin} onClick={() => {
          setDetail(row);
          editForm.setFieldsValue({
            name: row.name,
            phone: row.phone,
            loginName: row.loginName,
            adminRole: row.adminRole,
            status: row.status,
          });
          setDrawerOpen(true);
        }}>编辑</Button>
      ),
    },
  ];

  return (
    <div>
      <Space style={{ marginBottom: 16, width: '100%', justifyContent: 'space-between' }}>
        <Typography.Title level={4} style={{ margin: 0 }}>HQ 账户</Typography.Title>
        {isSuperAdmin && (
          <Button
            type="primary"
            onClick={() => {
              createForm.resetFields();
              createForm.setFieldsValue({ credentialType: 'phone', adminRole: 'OPS' });
              setCreateOpen(true);
            }}
          >
            新建账户
          </Button>
        )}
      </Space>
      <Form form={form} layout="inline" style={{ marginBottom: 16 }} onFinish={(v) => { setFilters(v); setPage(1); }}>
        <Form.Item name="phone" label="手机"><Input allowClear /></Form.Item>
        <Form.Item name="adminRole" label="角色">
          <Select allowClear style={{ width: 120 }} options={Object.entries(ROLE_LABELS).map(([value, label]) => ({ value, label }))} />
        </Form.Item>
        <Form.Item><Button type="primary" htmlType="submit">查询</Button></Form.Item>
      </Form>
      <Table rowKey="id" loading={loading} columns={columns} dataSource={data?.items ?? []}
        pagination={{ current: page, pageSize, total: data?.total ?? 0, showSizeChanger: true, onChange: (p, ps) => { setPage(p); setPageSize(ps); } }} />
      <Drawer title="编辑 HQ 账户" width={420} open={drawerOpen} onClose={() => setDrawerOpen(false)}
        extra={
          <Button type="primary" onClick={async () => {
            if (!detail) return;
            const v = await editForm.validateFields();
            const payload = { ...v };
            if (!payload.password) delete payload.password;
            await request(`/admin/hq-accounts/${detail.id}`, { method: 'PUT', body: JSON.stringify(payload) });
            message.success('已保存');
            setDrawerOpen(false);
            void reload();
          }}>保存</Button>
        }>
        <Form form={editForm} layout="vertical">
          <Form.Item name="name" label="姓名" rules={[{ required: true }]}><Input /></Form.Item>
          <Form.Item
            name="phone"
            label="手机号"
            rules={[
              { required: true, message: '请输入手机号' },
              { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' },
            ]}
          >
            <Input maxLength={11} />
          </Form.Item>
          <Form.Item name="loginName" label="用户名"><Input placeholder="用于密码登录" /></Form.Item>
          <Form.Item name="password" label="新密码" extra="留空则不修改">
            <Input.Password placeholder="至少 6 位" />
          </Form.Item>
          <Form.Item name="adminRole" label="角色">
            <Select options={Object.entries(ROLE_LABELS).map(([value, label]) => ({ value, label }))} />
          </Form.Item>
          <Form.Item name="status" label="状态">
            <Select options={Object.entries(ACCOUNT_STATUS_LABELS).map(([value, label]) => ({ value, label }))} />
          </Form.Item>
        </Form>
      </Drawer>
      <Modal
        title="新建 HQ 账户"
        open={createOpen}
        onCancel={() => setCreateOpen(false)}
        onOk={async () => {
          const v = await createForm.validateFields();
          await request('/admin/hq-accounts', { method: 'POST', body: JSON.stringify(v) });
          message.success('已创建');
          setCreateOpen(false);
          createForm.resetFields();
          void reload();
        }}
      >
        <Form form={createForm} layout="vertical" initialValues={{ credentialType: 'phone', adminRole: 'OPS' }}>
          <Form.Item name="credentialType" label="创建方式">
            <Radio.Group>
              <Radio value="phone">手机号账户（短信登录）</Radio>
              <Radio value="password">用户名密码账户</Radio>
            </Radio.Group>
          </Form.Item>
          <Form.Item name="name" label="姓名" rules={[{ required: true }]}><Input /></Form.Item>
          <Form.Item name="adminRole" label="角色">
            <Select options={Object.entries(ROLE_LABELS).map(([value, label]) => ({ value, label }))} />
          </Form.Item>
          {credentialType === 'phone' ? (
            <Form.Item name="phone" label="手机号" rules={[{ required: true, message: '请输入手机号' }]}>
              <Input maxLength={11} />
            </Form.Item>
          ) : (
            <>
              <Form.Item name="loginName" label="用户名" rules={[{ required: true, message: '请输入用户名' }]}>
                <Input autoComplete="off" />
              </Form.Item>
              <Form.Item name="password" label="密码" rules={[{ required: true, message: '请输入密码' }, { min: 6, message: '至少 6 位' }]}>
                <Input.Password autoComplete="new-password" />
              </Form.Item>
              <Form.Item name="phone" label="手机号（可选）" extra="不填则自动生成占位手机号，用于满足账号唯一约束">
                <Input maxLength={11} />
              </Form.Item>
            </>
          )}
        </Form>
      </Modal>
    </div>
  );
}
