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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  const [dataSource, setDataSource] = useState([]);

  return (
    <BetaSchemaForm<ApiTypes.Attendance.AttendanceSchedules.BatchStore>
      {...MyModalFormProps.props}
      title={`批量${props.title}`}
      wrapperCol={{ span: 24 }}
      width="800px"
      key={new Date().getTime()}
      form={form}
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields(); // 清空表单数据
        }
      }}
      trigger={<MyButtons.Create title={`批量${props.title}`} />}
      onFinish={async (values) =>
        Apis.Attendance.AttendanceSchedules.BatchStore(values)
          .then(() => {
            props.reload?.();
            message.success('新增成功');
            return true;
          })
          .catch(() => false)
      }
      submitter={false}
      columns={[
        Selects?.OrganizationsTree({
          title: '选择组织',
          key: 'organizations_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          title: '排班日期',
          key: 'schedule_date',
          valueType: 'date',
          fieldProps: {
            format: 'YYYY-MM-DD',
          },
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          valueType: 'dependency',
          name: ['organizations_id', 'schedule_date'],
          colProps: { span: 24 },
          columns: ({ schedule_date, organizations_id }) => {
            return [
              {
                renderFormItem: () => {
                  console.log(schedule_date, organizations_id);
                  return (
                    <MyButtons.Default
                      type="primary"
                      size="middle"
                      title="获取员工列表"
                      style={{ width: '100%' }}
                      onClick={() => {
                        // getEmployeesExternal(organizations_id || []); //获取外部员工
                        Apis.Attendance.AttendanceSchedules.ShiftList({
                          schedule_date: schedule_date,
                          organizations_id:
                            organizations_id?.[organizations_id?.length - 1],
                        }).then((res) => {
                          setDataSource(res?.data);
                          console.log(res);
                        });
                      }}
                    />
                  );
                },
              },
              {
                renderFormItem: () => {
                  return (
                    <EditableProTable<any>
                      headerTitle="可编辑表格"
                      bordered
                      recordCreatorProps={false}
                      columns={[
                        {
                          title: '员工ID',
                          key: 'id',
                        },
                        {
                          title: '员工',
                          key: 'name',
                        },
                      ]}
                      rowKey="id"
                      value={dataSource}
                    />
                  );
                },
              },
            ];
          },
        },
      ]}
    />
  );
}
