import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  MyProTableProps,
  renderTextHelper,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  GoodsReleasesAuditStatusEnum,
  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={2}>
                  <ProDescriptions.Item label="房屋信息" span={2}>
                    <Space>
                      <a
                        onClick={() =>
                          navigate(
                            `/customer/archive/show/${props?.item?.asset_houses_id}`,
                          )
                        }
                      >
                        {props?.item?.asset_house?.full_name || '-'}
                      </a>
                    </Space>
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="申请人">
                    {props?.item?.apply_name || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="联系电话">
                    {props?.item?.apply_phone || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="通行类型">
                    <renderTextHelper.Tag
                      Enums={GoodsReleasesPassedTypeEnum}
                      value={props?.item?.passed_type}
                    />
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="通行日期">
                    {props?.item?.passed_time || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="申请说明" span={2}>
                    {props?.item?.remark || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="申请时间" span={2}>
                    {props?.item?.created_at || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="审核时间">
                    {props?.item?.audit_time || '-'}
                  </ProDescriptions.Item>
                  <ProDescriptions.Item label="审核结果">
                    <renderTextHelper.Tag
                      Enums={GoodsReleasesAuditStatusEnum}
                      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 },
        },
      ]}
    />
  );
}
