import {
  MyBetaModalFormProps,
  MyButtons,
  MyModalFormProps,
  rulesHelper,
} from '@/common';
import { Selects } from '@/components/Select';
import { Apis } from '@/gen/Apis';
import {
  BetaSchemaForm,
  ProCard,
  ProDescriptions,
} from '@ant-design/pro-components';
import { Form, message, Space } from 'antd';

export default function UseSeal(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractSealLogs.Store>
      {...MyModalFormProps.props}
      title={`${props.title}`}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields();
          form.setFieldsValue({
            seal_ids: props?.item?.seal_info || [],
          });
        }
      }}
      trigger={
        <MyButtons.Default
          title={`用印`}
          size={props?.item?.size || 'small'}
          disabled={props?.item?.status !== 'Approved' && props?.item?.disabled}
          type="primary"
        />
      }
      onFinish={async (values: any) => {
        return Apis.Contract.ContractSealLogs.Store({
          ...values,
          contracts_id: props?.item?.id,
          seal_ids: values?.can_seal_ids || [],
          is_use_seal: values?.is_use_seal ? 1 : 0,
          is_external_seal_usage: values?.is_external_seal_usage ? 1 : 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          dataIndex: '合同信息',
          valueType: 'text',
          renderFormItem: () => (
            <Space direction="vertical" style={{ width: '100%' }}>
              <ProCard size="small">
                <ProDescriptions size="small" column={1}>
                  <ProDescriptions.Item label="合同信息">
                    {props?.item?.code} ｜ {props?.item?.name || '-'}
                  </ProDescriptions.Item>
                </ProDescriptions>
              </ProCard>
            </Space>
          ),
          colProps: { span: 24 },
          formItemProps: {
            labelCol: { span: 6 },
            wrapperCol: { span: 18 },
          },
        },
        Selects.ContractSeals({
          key: 'seal_ids',
          required: true,
          title: '可用印章',
          colProps: { span: 24 },
          fieldProps: {
            showSearch: true,
            mode: 'multiple',
          },
        }),
        Selects?.Employees({
          title: '用印申请人',
          key: 'company_employees_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          required: true,
        }),
        {
          key: 'number_contract_copies',
          title: '合同份数',
          formItemProps: { ...rulesHelper.text },
          colProps: { span: 24 },
        },
        {
          title: '用印时间',
          key: 'use_seal_time',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            style: { width: '100%' },
            disabledDate: (current: any) => {
              const today = new Date();
              today.setHours(0, 0, 0, 0);
              return current && current > today;
            },
          },
        },
        {
          title: '交易方用印',
          tooltip: '该合同中，非我司主体的交易方，是否已经盖章',
          key: 'is_use_seal',
          valueType: 'switch',
          colProps: { span: 24 },
          fieldProps: {
            checkedChildren: '已用印',
            unCheckedChildren: '未用印',
          },
        },
        {
          title: '备注',
          key: 'remarks',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
