import {
  MyFormItems,
  MyModalFormProps,
  MyPageContainer,
  rulesHelper,
} from '@/common';
import MyModalsHouseSelectList from '@/components/ModalsHouseSelectList';
import { Apis } from '@/gen/Apis';
import { GoodsReleasesPassedTypeEnum } from '@/gen/Enums';
import { BetaSchemaForm, ProCard } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Form, message, Space } from 'antd';
import { useState } from 'react';

export default function Index({ title = '物品放行申请' }) {
  const [form] = Form.useForm();
  const navigate = useNavigate();
  const [getSelectHouse, setSelectHouse] = useState<any>({});

  return (
    <MyPageContainer
      title={
        <Space
          style={{ cursor: 'pointer' }}
          onClick={() => {
            navigate(-1);
          }}
        >
          {/* <LeftCircleOutlined size={34} /> */}
          {title}
        </Space>
      }
      enableTabs={false}
      tabKey="goods_releases_create"
      tabLabel={title}
    >
      <ProCard>
        <div style={{ width: 900, minHeight: '83vh', margin: '0 auto' }}>
          <BetaSchemaForm<ApiTypes.GoodsReleases.GoodsReleases.Store>
            {...MyModalFormProps.props}
            title={title}
            // 基础表单
            layoutType="Form"
            labelCol={{ span: 24 }}
            wrapperCol={{ span: 24 }}
            labelAlign="left"
            width="900px"
            form={form}
            onFinish={async (values: any) => {
              Apis.GoodsReleases.GoodsReleases.Store(values)
                .then(() => {
                  navigate(-1);
                  message.success('提交成功');
                })
                .catch(() => false);
            }}
            columns={[
              {
                colProps: { span: 24 },
                title: '选择房屋',
                key: 'asset_houses_id',
                formItemProps: { ...rulesHelper.text },
                renderFormItem: () => {
                  return (
                    <ProCard bordered>
                      <Space>
                        <MyModalsHouseSelectList
                          onChange={(e: any) => {
                            setSelectHouse(e);
                            form.setFieldsValue({
                              asset_houses_id: e?.id,
                            });
                          }}
                        />
                        {getSelectHouse?.full_name ? (
                          <div>已选择房屋：{getSelectHouse?.full_name}</div>
                        ) : (
                          ''
                        )}
                      </Space>
                    </ProCard>
                  );
                },
              },
              {
                title: '申请人',
                key: 'apply_name',
                colProps: { span: 6 },
                formItemProps: { ...rulesHelper.text },
              },
              {
                title: '申请人电话',
                key: 'apply_phone',
                colProps: { span: 6 },
                fieldProps: {
                  maxLength: 11,
                },
                formItemProps: { ...rulesHelper.phone },
              },
              MyFormItems.EnumRadio({
                key: 'passed_type',
                title: '通行类型',
                valueEnum: GoodsReleasesPassedTypeEnum,
                colProps: { span: 6 },
                formItemProps: { ...rulesHelper.text },
              }),
              {
                title: '通行日期',
                key: 'passed_time',
                valueType: 'date',
                colProps: { span: 6 },
                formItemProps: { ...rulesHelper.text },

                fieldProps: {
                  disabledDate: (current: any) => {
                    // 禁用过去的日期（只允许今天及之后的日期）
                    const today = new Date();
                    today.setHours(0, 0, 0, 0);
                    return current && current < today;
                  },
                },
              },
              {
                valueType: 'formList',
                dataIndex: 'goods',
                title: '添加放行物品',
                formItemProps: { ...rulesHelper.array },
                fieldProps: {
                  copyIconProps: false,
                  initialValue: [
                    {
                      number: 1,
                    },
                  ],
                  creatorRecord: {
                    number: 1,
                  },
                },
                columns: [
                  {
                    valueType: 'group',
                    colProps: { span: 24 },
                    columns: [
                      {
                        key: 'name',
                        title: '物品名称',
                        colProps: { span: 8 },
                        formItemProps: { ...rulesHelper.text },
                      },
                      {
                        key: 'number',
                        title: '数量',
                        valueType: 'digit',
                        colProps: { span: 3 },
                        formItemProps: { ...rulesHelper.number },
                      },
                      {
                        key: 'remark',
                        title: '物品备注',
                        colProps: { span: 13 },
                      },
                    ],
                  },
                ],
              },

              {
                title: '其他备注',
                key: 'remark',
                valueType: 'textarea',
                colProps: { span: 24 },
              },
            ]}
          />
        </div>
      </ProCard>
    </MyPageContainer>
  );
}
