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

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

  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractSealLogs.Update>
      {...MyModalFormProps.props}
      title={`编辑${props.title}`}
      trigger={<MyButtons.Edit />}
      layout="horizontal"
      labelCol={{ span: 6 }}
      wrapperCol={{ span: 18 }}
      labelAlign="left"
      width="600px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            ...props.item,
            seal_ids: props.item?.seal_info?.map((item: any) => ({
              seal_id: item?.id || item,
            })),
          });
        }
      }}
      onFinish={async (values: any) =>
        Apis.Contract.ContractSealLogs.Update({
          ...values,
          contracts_id: props?.item?.id,
          is_use_seal: values?.is_use_seal ? 1 : 0,
          is_external_seal_usage: values?.is_external_seal_usage ? 1 : 0,
          id: props.item?.id ?? 0,
          seal_ids: values?.seal_ids?.map((item: any) => item.seal_id),
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        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); // 设置为今天的00:00:00
              return current && current > today;
            },
          },
        },
        {
          title: '交易方用印',
          tooltip: '该合同中，非我司主体的交易方，是否已经盖章',
          key: 'is_use_seal',
          valueType: 'switch',
          colProps: { span: 24 },
          fieldProps: {
            checkedChildren: '已用印',
            unCheckedChildren: '未用印',
          },
        },
        // {
        //   title: '是否外出用印',
        //   tooltip: '本次用印，是否在非公司办公范围内',
        //   key: 'is_external_seal_usage',
        //   valueType: 'switch',
        //   colProps: { span: 24 },
        //   fieldProps: {
        //     checkedChildren: '是',
        //     unCheckedChildren: '否',
        //   },
        // },
        {
          valueType: 'formList',
          dataIndex: 'seal_ids',
          title: '实际用章',
          formItemProps: { ...rulesHelper.array },
          fieldProps: {
            copyIconProps: false,
            // deleteIconProps: false,
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                Selects?.CompanySeals({
                  title: '',
                  key: 'seal_id',
                  colProps: { span: 24 },
                  required: true,
                  fieldProps: {
                    placeholder: '请选择要使用的印章（可搜索）',
                  },
                }),
              ],
            },
          ],
        },
        {
          title: '备注',
          key: 'remarks',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
