// aislop-ignore-file: duplicate-block -- 宿舍/床位/柜子表单声明结构相似且字段不同，已共享 RoomItemFormFields
import React from 'react';
import { Form, Input, InputNumber, Modal, Select } from 'antd';
import { RoomDrawer } from './RoomDrawer';
import type { BedItem, LockerItem } from './RoomColumns';
import { parseRoomNumber } from './RoomColumns';

export const RoomItemFormFields: React.FC<{
  fieldName: 'bedNumber' | 'lockerNumber';
  label: string;
  placeholder: string;
}> = ({ fieldName, label, placeholder }) => (
  <>
    <Form.Item name={fieldName} label={label} rules={[{ required: true }]}>
      <Input placeholder={placeholder} />
    </Form.Item>
    <Form.Item name="status" label="状态">
      <Select
        options={[
          { value: 'available', label: '空闲' },
          { value: 'maintenance', label: '维修中' },
        ]}
        placeholder="默认为空闲"
      />
    </Form.Item>
    <Form.Item name="notes" label="备注">
      <Input.TextArea rows={2} />
    </Form.Item>
  </>
);

export const RoomEditModal: React.FC<{
  open: boolean;
  editing: boolean;
  saving: boolean;
  form: ReturnType<typeof Form.useForm>[0];
  onOk?: () => void;
  onCancel: () => void;
}> = ({ open, editing, saving, form, onOk, onCancel }) => {
  return (
    <Modal
      title={editing ? '编辑宿舍' : '添加宿舍'}
      open={open}
      onOk={onOk}
      onCancel={onCancel}
      okText="保存"
      confirmLoading={saving}
    >
      <Form form={form} layout="vertical">
        <Form.Item name="roomNumber" label="房间号" rules={[{ required: true }]}>
          <Input
            placeholder="如：4-102（自动解析楼栋楼层）"
            onChange={(e) => {
              const parsed = parseRoomNumber(e.target.value);
              if (parsed) form.setFieldsValue(parsed);
            }}
          />
        </Form.Item>
        <Form.Item name="building" label="楼栋">
          <Input placeholder="如：4号楼（留空自动解析）" />
        </Form.Item>
        <Form.Item name="floor" label="楼层">
          <InputNumber min={1} style={{ width: '100%' }} />
        </Form.Item>
        <Form.Item name="capacity" label="额定人数" rules={[{ required: true }]}>
          <InputNumber min={1} max={20} style={{ width: '100%' }} />
        </Form.Item>
        <Form.Item name="roomType" label="宿舍类型">
          <Select
            allowClear
            options={[
              { value: '四人间', label: '四人间' },
              { value: '单人间', label: '单人间' },
              { value: '家庭房', label: '家庭房' },
              { value: '爆改房', label: '爆改房' },
            ]}
            placeholder="留空自动解析"
          />
        </Form.Item>
        <Form.Item name="rentalCategory" label="租赁类别">
          <Select
            allowClear
            options={[
              { value: 'short', label: '短租' },
              { value: 'long', label: '长租' },
            ]}
            placeholder="默认为短租"
          />
        </Form.Item>
        <Form.Item name="monthlyRate" label="月租金">
          <InputNumber min={0} precision={2} style={{ width: '100%' }} placeholder="长租月租金" />
        </Form.Item>
        {editing && (
          <Form.Item name="status" label="状态">
            <Select
              options={[
                { value: 'available', label: '可入住' },
                { value: 'full', label: '已满' },
                { value: 'maintenance', label: '维修中' },
              ]}
            />
          </Form.Item>
        )}
      </Form>
    </Modal>
  );
};

export const BedModal: React.FC<{
  open: boolean;
  editing: boolean;
  saving: boolean;
  form: ReturnType<typeof Form.useForm>[0];
  onOk?: () => void;
  onCancel: () => void;
}> = ({ open, editing, saving, form, onOk, onCancel }) => {
  return (
    <Modal
      title={editing ? '编辑床位' : '添加床位'}
      open={open}
      onOk={onOk}
      onCancel={onCancel}
      confirmLoading={saving}
      okText="保存"
    >
      <Form form={form} layout="vertical">
        <RoomItemFormFields fieldName="bedNumber" label="床位编号" placeholder="如：1号床" />
      </Form>
    </Modal>
  );
};

export const LockerModal: React.FC<{
  open: boolean;
  editing: boolean;
  saving: boolean;
  form: ReturnType<typeof Form.useForm>[0];
  onOk?: () => void;
  onCancel: () => void;
}> = ({ open, editing, saving, form, onOk, onCancel }) => {
  return (
    <Modal
      title={editing ? '编辑柜子' : '添加柜子'}
      open={open}
      onOk={onOk}
      onCancel={onCancel}
      confirmLoading={saving}
      okText="保存"
    >
      <Form form={form} layout="vertical">
        <RoomItemFormFields fieldName="lockerNumber" label="柜子编号" placeholder="如：1号柜" />
      </Form>
    </Modal>
  );
};

export const RoomDetailArea: React.FC<{
  modalOpen: boolean;
  canSaveRoom: boolean;
  editing: boolean;
  saving: boolean;
  form: ReturnType<typeof Form.useForm>[0];
  onSaveRoom: () => void;
  onCloseRoomModal: () => void;
  drawerOpen: boolean;
  drawerRoom: any;
  beds: BedItem[];
  lockers: LockerItem[];
  canEditRooms: boolean;
  remainingBedSlots: number;
  defaultBatchBedCount: number;
  onCloseDrawer: () => void;
  onAddBed: () => void;
  onBatchBeds: (count: number) => void;
  onEditBed: (record: BedItem) => void;
  onDeleteBed: (id: number) => void;
  onSaveBedCell: (record: BedItem, field: string, value: unknown) => void;
  onAddLocker: () => void;
  onBatchLockers: (count: number) => void;
  onEditLocker: (record: LockerItem) => void;
  onDeleteLocker: (id: number) => void;
  onSaveLockerCell: (record: LockerItem, field: string, value: unknown) => void;
  bedModalOpen: boolean;
  bedEditing: boolean;
  savingBed: boolean;
  bedForm: ReturnType<typeof Form.useForm>[0];
  onSaveBed: () => void;
  onCloseBedModal: () => void;
  lockerModalOpen: boolean;
  lockerEditing: boolean;
  savingLocker: boolean;
  lockerForm: ReturnType<typeof Form.useForm>[0];
  onSaveLocker: () => void;
  onCloseLockerModal: () => void;
}> = (props) => {
  return (
    <>
      <RoomEditModal
        open={props.modalOpen && props.canSaveRoom}
        editing={props.editing}
        saving={props.saving}
        form={props.form}
        onOk={props.canSaveRoom ? props.onSaveRoom : undefined}
        onCancel={props.onCloseRoomModal}
      />
      <RoomDrawer
        open={props.drawerOpen}
        room={props.drawerRoom}
        beds={props.beds}
        lockers={props.lockers}
        canEditRooms={props.canEditRooms}
        remainingBedSlots={props.remainingBedSlots}
        defaultBatchBedCount={props.defaultBatchBedCount}
        onClose={props.onCloseDrawer}
        onAddBed={props.onAddBed}
        onBatchBeds={props.onBatchBeds}
        onEditBed={props.onEditBed}
        onDeleteBed={props.onDeleteBed}
        onSaveBedCell={props.onSaveBedCell}
        onAddLocker={props.onAddLocker}
        onBatchLockers={props.onBatchLockers}
        onEditLocker={props.onEditLocker}
        onDeleteLocker={props.onDeleteLocker}
        onSaveLockerCell={props.onSaveLockerCell}
      />
      <BedModal
        open={props.bedModalOpen && props.canEditRooms}
        editing={props.bedEditing}
        saving={props.savingBed}
        form={props.bedForm}
        onOk={props.canEditRooms ? props.onSaveBed : undefined}
        onCancel={props.onCloseBedModal}
      />
      <LockerModal
        open={props.lockerModalOpen && props.canEditRooms}
        editing={props.lockerEditing}
        saving={props.savingLocker}
        form={props.lockerForm}
        onOk={props.canEditRooms ? props.onSaveLocker : undefined}
        onCancel={props.onCloseLockerModal}
      />
    </>
  );
};
