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.Attendance.AttendanceSchedules.Update>
      {...MyModalFormProps.props}
      title={`调整班次`}
      trigger={
        <MyButtons.Default
          type="primary"
          title="调班"
          disabled={props.item?.status !== 'Active'}
        />
      }
      key={new Date().getTime()}
      layout="horizontal"
      labelCol={{ span: 5 }}
      wrapperCol={{ span: 19 }}
      labelAlign="left"
      width="500px"
      form={form}
      onOpenChange={(open: any) => {
        if (open && props.item) {
          form.setFieldsValue(props.item);
        }
      }}
      onFinish={async (values) =>
        Apis.Attendance.AttendanceSchedules.Update({
          ...values,
          id: props.item?.id ?? 0,
        })
          .then(() => {
            props.reload?.();
            message.success('编辑成功');
            return true;
          })
          .catch(() => false)
      }
      columns={[
        Selects?.Employees({
          title: '员工信息',
          key: 'company_employees_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
          required: true,
          fieldProps: {
            disabled: true,
          },
        }),

        Selects?.AssetProjects({
          title: '项目信息',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
          fieldProps: {
            disabled: true,
          },
        }),
        {
          title: '班次日期',
          key: 'schedule_date',
          valueType: 'date',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
          fieldProps: {
            style: {
              width: '100%',
            },
            disabled: true,
          },
        },
        Selects?.AttendanceShiftsSelect({
          key: 'attendance_shifts_id',
          title: '选择班次',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.number },
        }),
        {
          title: '备注',
          key: 'remark',
          valueType: 'textarea',
          colProps: { span: 24 },
        },
      ]}
    />
  );
}
