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

export default function Update(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Contract.ContractArchiveBorrows.Return>
      {...MyModalFormProps.props}
      title={`归还${props.title}`}
      trigger={
        <MyButtons.Default
          title="归还"
          disabled={
            props?.item?.borrow_status === 'Pending' ||
            props?.item?.borrow_status === 'Approved' ||
            props?.item?.borrow_status === 'Rejected' ||
            props?.item?.borrow_status === 'Returned' ||
            props?.item?.is_need_return === 0
          }
          type="primary"
        />
      }
      key={new Date().getTime()}
      wrapperCol={{ span: 24 }}
      width="850px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue({
            files: props.item?.contract_archive_borrow_files?.map(
              (res: any) => ({
                id: res?.id,
                name: res?.contract_archive_file?.name,
                borrow_number: res?.borrow_number,
              }),
            ),
          });
        }
      }}
      onFinish={async (values) =>
        Apis.Contract.ContractArchiveBorrows.Return({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success(props.title + '编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Employees({
          title: '归还人',
          dataIndex: 'returner_id',
          key: 'returner_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            showSearch: true,
          },
          required: true,
        }),
        {
          valueType: 'divider',
          fieldProps: {
            orientation: 'left',
            children: '归还文件',
          },
        },
        {
          valueType: 'formList',
          dataIndex: 'files',
          title: '',
          formItemProps: {
            ...rulesHelper.array,
            labelCol: { span: 0 },
            wrapperCol: { span: 24 },
          },
          colProps: { span: 24 },
          fieldProps: {
            copyIconProps: false,
            deleteIconProps: false,
            creatorButtonProps: false,
            itemRender: ({ listDom, action }: any, { index }: any) => {
              return (
                <ProCard
                  bordered
                  extra={action}
                  title={`归还文件${index + 1}`}
                  style={{
                    marginBlockEnd: 8,
                  }}
                >
                  {listDom}
                </ProCard>
              );
            },
          },
          columns: [
            {
              valueType: 'group',
              colProps: { span: 24 },
              columns: [
                {
                  title: 'ID',
                  key: 'id',
                  colProps: { span: 2 },
                  fieldProps: {
                    disabled: true,
                  },
                },
                {
                  title: '名称',
                  key: ['name'],
                  colProps: { span: 7 },
                  fieldProps: {
                    disabled: true,
                  },
                },
                {
                  title: '归还份数',
                  key: 'borrow_number',
                  valueType: 'digit',
                  colProps: { span: 3 },
                  fieldProps: {
                    disabled: true,
                    style: {
                      width: '100%',
                    },
                  },
                },
                MyFormItems.EnumSelect({
                  key: 'return_status',
                  title: '归还状态',
                  colProps: { span: 4 },
                  valueEnum: () => {
                    let enumObj = JSON.parse(
                      JSON.stringify(ContractArchiveFileReturnStatusEnum),
                    );
                    delete enumObj.NotReturned;
                    return enumObj;
                  },
                  required: true,
                }),
                {
                  name: ['return_status', 'borrow_number'],
                  valueType: 'dependency',
                  columns: ({ return_status, borrow_number }: any) => {
                    return [
                      {
                        title: '遗失数量',
                        key: 'lost_number',
                        valueType: 'digit',
                        colProps: { span: 4 },
                        formItemProps:
                          return_status === 'AllLost' ||
                          return_status === 'PartialLost'
                            ? { ...rulesHelper.number }
                            : {},
                        fieldProps: {
                          max: borrow_number,
                          min: 0,
                          style: {
                            width: '100%',
                          },
                        },
                      },
                      {
                        title: '损坏数量',
                        key: 'damaged_number',
                        valueType: 'digit',
                        colProps: { span: 4 },
                        formItemProps:
                          return_status === 'PartialDamaged' ||
                          return_status === 'AllDamaged'
                            ? { ...rulesHelper.number }
                            : {},
                        fieldProps: {
                          max: borrow_number,
                          min: 0,
                          style: {
                            width: '100%',
                          },
                        },
                      },
                    ];
                  },
                },
                {
                  title: '遗失描述',
                  key: 'lost_description',
                  colProps: { span: 12 },
                },
                {
                  title: '损坏描述',
                  key: 'damaged_description',
                  colProps: { span: 12 },
                },
              ],
            },
          ],
        },
      ]}
    />
  );
}
