import {
  MyBetaModalFormProps,
  MyButtons,
  MyFormItems,
  MyModalFormProps,
  MyProTableProps,
  renderTextHelper,
  rulesHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { GoodsReleasesPassedTypeEnum } from '@/gen/Enums';
import {
  BetaSchemaForm,
  ProCard,
  ProDescriptions,
  ProTable,
} from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Form, message, Space } from 'antd';

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const navigate = useNavigate();

  return (
    <BetaSchemaForm<ApiTypes.GoodsReleases.GoodsReleases.ApproveByEmployee>
      {...MyModalFormProps.props}
      title={props.title}
      trigger={<MyButtons.Default title="审核" type="primary" />}
      wrapperCol={{ span: 24 }}
      width="800px"
      modalProps={{
        bodyStyle: { maxHeight: '70vh', overflowY: 'auto' },
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values: any) =>
        Apis.GoodsReleases.GoodsReleases.ApproveByEmployee({
          id: props.item?.id ?? 0,
          ...values,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        {
          dataIndex: 'info_display',
          valueType: 'text',
          renderFormItem: () => (
            <Space direction="vertical" style={{ width: '100%' }}>
              <ProCard size="small">
                <ProDescriptions bordered size="small" column={1}>
                  <ProDescriptions.Item label="房屋信息">
                    <a
                      onClick={() =>
                        navigate(
                          `/customer/archive/show/${props?.item?.house?.model_id}`,
                        )
                      }
                    >
                      {props?.item?.house?.full_name || '-'}
                    </a>
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="申请人">
                    {props?.item?.customer?.name || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="申请时间">
                    {props?.item?.created_at || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="当前状态">
                    <renderTextHelper.Tag
                      Enums={GoodsReleasesPassedTypeEnum}
                      value={props?.item?.status}
                    />
                  </ProDescriptions.Item>
                </ProDescriptions>
              </ProCard>

              {props?.item?.goods && props?.item?.goods.length > 0 && (
                <ProCard size="small" title="物品清单">
                  <ProTable
                    {...MyProTableProps.props}
                    search={false}
                    toolBarRender={false}
                    pagination={false}
                    dataSource={props?.item?.goods}
                    rowKey={(record, index) => record?.id || index}
                    size="small"
                    columns={[
                      {
                        title: '物品名称',
                        dataIndex: 'name',
                      },
                      {
                        title: '数量',
                        dataIndex: 'number',
                      },
                      {
                        title: '备注',
                        dataIndex: 'remark',
                      },
                    ]}
                  />
                </ProCard>
              )}
            </Space>
          ),
          colProps: { span: 24 },
        },
        {
          valueType: 'group',
          columns: [
            MyFormItems.EnumRadio({
              key: 'status',
              title: '审核操作',
              colProps: { span: 24 },
              valueEnum: () => {
                let obj: any = JSON.parse(
                  JSON.stringify(GoodsReleasesPassedTypeEnum),
                );
                delete obj.Pending;
                return obj;
              },
              required: true,
            }),
            {
              valueType: 'dependency',
              name: ['status'],
              columns: ({ status }) => {
                return status === 'Rejected'
                  ? [
                      {
                        title: '驳回原因',
                        dataIndex: 'reason',
                        valueType: 'textarea',
                        formItemProps: { ...rulesHelper.text },
                        colProps: { span: 24 },
                      },
                    ]
                  : [];
              },
            },
          ],
        },
      ]}
    />
  );
}
