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

export default function Create(props: MyBetaModalFormProps) {
  const [form] = Form.useForm();
  return (
    <BetaSchemaForm<ApiTypes.Asset.AssetItemMaintenanceRules.Store>
      {...MyModalFormProps.props}
      title={`添加维保规则`}
      wrapperCol={{ span: 24 }}
      width="800px"
      trigger={
        <MyButtons.Default title={props.title} size="middle" type="primary" />
      }
      onOpenChange={(open: any) => {
        if (open) {
          form.resetFields();
        }
      }}
      key={new Date().getTime()}
      form={form}
      onFinish={async (values: any) => {
        return Apis.Asset.AssetItemMaintenanceRules.Store({
          ...values,
          asset_item_ids: values.asset_items.map((item: any) => item.id),
        })
          .then(() => {
            props.reload?.();
            message.success('添加成功');
            return true;
          })
          .catch(() => false);
      }}
      columns={[
        {
          title: '维保规则名称',
          key: 'name',
          colProps: { span: 12 },
          formItemProps: { ...rulesHelper.text },
        },
        {
          title: '维保时间间隔（天）',
          key: 'cycle_days',
          colProps: { span: 12 },
          fieldProps: { min: 1, style: { width: '100%' }, suffix: '天' },
          valueType: 'digit',
          formItemProps: { ...rulesHelper.number },
        },
        {
          title: '提前生成天数',
          key: 'advance_days',
          colProps: { span: 12 },
          fieldProps: { min: 1, style: { width: '100%' }, suffix: '天' },
          valueType: 'digit',
          formItemProps: { ...rulesHelper.number },
        },
        {
          title: '完成期限天数',
          key: 'deadline_days',
          colProps: { span: 12 },
          fieldProps: { min: 1, style: { width: '100%' }, suffix: '天' },
          valueType: 'digit',
          formItemProps: { ...rulesHelper.number },
        },
        Selects?.AssetProjects({
          title: '选择项目',
          key: 'asset_projects_id',
          colProps: { span: 24 },
          formItemProps: { ...rulesHelper.text },
        }),
        {
          title: '选择资产',
          key: 'asset_items',
          formItemProps: { ...rulesHelper.array },
          renderFormItem: (schema, config) => {
            console.log(config, '2222');
            return (
              <ProCard size="small" bordered>
                <ProTable
                  {...MyProTableProps.props}
                  options={false}
                  size="small"
                  search={false}
                  pagination={false}
                  dataSource={
                    typeof config?.value === 'object' ? config?.value || [] : []
                  }
                  // headerTitle={
                  //   <ModalsAssetItemsSelectList
                  //     onChange={(e) => {
                  //       form.setFieldValue('asset_items', e);
                  //     }}
                  //   />
                  // }
                  columns={[
                    MyColumns.ID({
                      search: false,
                    }),
                    {
                      title: '名称',
                      dataIndex: 'name',
                    },
                    MyColumns.Option({
                      render: (_, item: any) => (
                        <MyButtons.Delete
                          onConfirm={() => {
                            let data = JSON.parse(
                              JSON.stringify(form.getFieldValue('asset_items')),
                            );
                            form.setFieldValue(
                              'asset_items',
                              data.filter((res: any) => res?.id !== item?.id),
                            );
                          }}
                        />
                      ),
                    }),
                  ]}
                />
              </ProCard>
            );
          },
        },
      ]}
    />
  );
}
