/*
 * @Author: your name
 * @Date: 2022-01-12 17:11:11
 * @LastEditTime: 2024-10-12 10:09:05
 * @LastEditors: code4eat awesomedema@gmail.com
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /KC-MiddlePlatform/src/pages/platform/setting/userManage/modal.tsx
 */

import React from 'react';
import KCModal from '@/components/KCModal';
import KCProSelect from '@/components/KCProSelect';
import { menuManageModelState, Dispatch } from 'umi';

import { Form } from 'antd';

import { ProFormText, ProFormDependency, ProFormDigit, ProFormRadio, ProFormTextArea } from '@ant-design/pro-form';
import type { AddUsersDataType } from '@/service/user';
import { randomString } from '@/utils';
import { MenuTypes } from '@/constant';
import TreeEditer from '@/pages/platform/components/TreeEditer';

// import { TableActType } from "./typings";

export enum TableActType { //表格操作类型枚举
  NOACT,
  ADD,
  DEL,
  EDIT,
  SET,
  MOVEANDCOPY,
}

interface ActModalProps extends menuManageModelState {
  dispatch: Dispatch | undefined;
}

const ActModal: React.FC<ActModalProps> = ({ dispatch, isShowModal, tableAct, currentEdit, parentId, record }) => {
  const onVisibleChangeHandle = (bool: boolean) => {
    // console.log({bool});
    if (!bool) {
      dispatch &&
        dispatch({
          type: 'menuManageModel/cancelTableAct',
        });
    }
  };

  const onFinishHandle = (data: any & AddUsersDataType) => {
    console.log({ currentEdit });
    if (tableAct == TableActType.ADD) {
      dispatch &&
        dispatch({
          type: 'menuManageModel/postAddData',
          payload: data,
        });
    }

    if (tableAct == TableActType.EDIT) {
      dispatch &&
        dispatch({
          type: 'menuManageModel/postEditData',
          payload: data,
        });
    }
    if (tableAct == TableActType.SET) {
      dispatch &&
        dispatch({
          type: 'menuManageModel/setHomePage',
          payload: data,
        });
    }

    if (tableAct == TableActType.MOVEANDCOPY) {
      dispatch &&
        dispatch({
          type: 'menuManageModel/transformMenu',
          payload: data,
        });
    }

    return true;
  };

  const menuTypeOptionFiler = () => {
    if (parentId == '0') {
      //顶级目录
      return MenuTypes.filter((t) => t.label == '中心层');
    }

    if (record?.type == 6) {
      //中心层
      return MenuTypes.filter((t) => t.label == '体系');
    }

    if (record?.type == 5) {
      //体系
      return MenuTypes.filter((t) => t.label == '系统' || t.label == '菜单');
    }

    if (record?.type == 3) {
      //子系统
      return MenuTypes.filter((t) => t.label == '目录' || t.label == '菜单');
    }
    if (record?.type == 1) {
      //子系统菜单目录
      return MenuTypes.filter((t) => t.label == '菜单');
    }
    if (record?.type == 0) {
      //子系统菜单目录
      return MenuTypes.filter((t) => t.label == '菜单' || t.label == '目录');
    }
    return MenuTypes;
  };

  const setModalFormTitle = (key: TableActType) => {
    if (key == TableActType.ADD) return '新增';
    if (key == TableActType.EDIT) return '编辑菜单';
    if (key == TableActType.SET) return '设置首页';
    if (key == TableActType.MOVEANDCOPY) return '复制/移动';
  };

  return (
    <KCModal
      visible={isShowModal}
      onVisibleChange={onVisibleChangeHandle}
      layout="horizontal"
      width={600}
      initialValues={
        tableAct == TableActType.EDIT
          ? { ...currentEdit }
          : {
              hospSign: randomString(16),
              isDataShare: 0,
              isHomepage: 0,
            }
      }
      // title={tableAct == TableActType.EDIT ? '编辑菜单' : tableAct == TableActType.SET ? '设为首页' : '新增菜单'}
      title={setModalFormTitle(tableAct)}
      labelCol={{
        span: 6,
      }}
      wrapperCol={{ span: 12 }}
      onFinish={async (data) => onFinishHandle(data)}
    >
      {tableAct == TableActType.SET && (
        <>
          <ProFormText
            width="md"
            name="menuName"
            label="首页名称"
            placeholder="请输入"
            rules={[
              {
                required: false,
                message: '请输入名称(可选)',
              },
            ]}
          />
        </>
      )}
      {tableAct == TableActType.MOVEANDCOPY && (
        <Form.Item name="treeEditer">
          <TreeEditer />
        </Form.Item>
      )}
      {(tableAct == TableActType.ADD || tableAct == TableActType.EDIT) && (
        <>
          <ProFormText
            width="md"
            name="name"
            label="名称"
            placeholder="请输入"
            rules={[
              {
                required: true,
                message: '请输入名称!',
              },
            ]}
          />
          <KCProSelect
            label="类型"
            width="md"
            name="type"
            fieldProps={{size:'small'}}
            options={menuTypeOptionFiler()}
            rules={[
              {
                required: true,
                message: '请选择!',
              },
            ]}
          />
          <ProFormText
            width="md"
            name="url"
            label="URL"
            placeholder="请输入"
            rules={[
              {
                required: false,
                message: '请输入!',
              },
            ]}
          />

          <ProFormDependency name={['type', 'contentType']}>
            {({ type, contentType }) => {
              if (type == 1 && contentType != 4) {
                return (
                  <ProFormText
                    width="md"
                    name="path"
                    label="PATH"
                    placeholder="请输入"
                    rules={[
                      {
                        required: true,
                        message: '请输入!',
                      },
                    ]}
                  />
                );
              } else {
                return (
                  <ProFormText
                    width="md"
                    name="path"
                    label="PATH"
                    initialValue={tableAct == TableActType.ADD ? '/platform' : ''}
                    placeholder="请输入"
                    rules={[
                      {
                        required: false,
                        message: '请输入!',
                      },
                    ]}
                  />
                );
              }
            }}
          </ProFormDependency>
          <ProFormDigit label="排序号" name="orderNum" min={0} width="md" max={1000} fieldProps={{ precision: 0 }} />
          <ProFormText width="md" name="icon" label="icon" placeholder="请输入" />
          <ProFormDependency name={['type']}>
            {({ type }) => {
              return (
                type == 1 && (
                  //类型时菜单时
                  <>
                    <KCProSelect
                      label="内容类型"
                      width="md"
                      name="contentType"
                      initialValue={0}
                      options={[
                        {
                          label: '一般',
                          value: 0, //普通页面，默认值
                        },
                        {
                          label: '报告',
                          value: 1,
                        },
                        {
                          label: '大屏',
                          value: 2,
                        },
                        {
                          label: '填报',
                          value: 3,
                        },
                        {
                          label: '空白',
                          value: 4,
                        },
                        {
                          label: '静态',
                          value: 5,
                        },
                        {
                          label: '外部系统嵌入',
                          value: 6,
                        },
                        {
                          label: '外部系统链接',
                          value: 7,
                        },
                      ]}
                      rules={[
                        {
                          required: true,
                          message: '请选择!',
                        },
                      ]}
                    />
                    <ProFormRadio.Group
                      name="isHomepage"
                      label="是否为首页"
                      options={[
                        {
                          label: '是',
                          value: 1,
                        },
                        {
                          label: '否',
                          value: 0,
                        },
                      ]}
                    />

                    <ProFormDependency name={['contentType']}>
                      {({ contentType }) => {
                        return (
                          contentType &&
                          contentType != 0 &&
                          contentType != 4 &&
                          (contentType != 6 && contentType != 7) && (
                            //页面内容为非一般时，填写对应有数Id
                            <ProFormDigit
                              label="报告Id"
                              name="reportId"
                              min={1}
                              fieldProps={{ precision: 0 }}
                              rules={[
                                {
                                  required: true,
                                  message: '请填写id!',
                                },
                              ]}
                            />
                          )
                        );
                      }}
                    </ProFormDependency>
                  </>
                )
              );
            }}
          </ProFormDependency>
          <ProFormDependency name={['contentType']}>
            {({ contentType }) => {
              if (contentType == 4) {
                return <ProFormTextArea name="description" label="空白页描述" placeholder="请输入" fieldProps={{}} />;
              }
            }}
          </ProFormDependency>
        </>
      )}
    </KCModal>
  );
};

export default ActModal;
