import { A1UtableType } from "@/types";
import { Button, Form, FormInstance, Modal, Popconfirm, Select } from "antd";
import React, { useCallback, useEffect, useRef } from "react";
import styles from "./index.module.scss";
import { useSelector } from "react-redux";
import { RootState } from "@/store";
import { A1_APIUsave } from "@/store/action/A1Project";
import { MessageFu } from "@/utils/message";

type Props = {
  info: A1UtableType;
  closeFu: () => void;
  editFu: () => void;
  addFu: () => void;
};

function A1UserAdd({ info, closeFu, editFu, addFu }: Props) {
  useEffect(() => {
    if (info.id > 0) {
      FormBoxRef.current?.setFieldsValue({
        dictJobId: info.dictJobId,
        userId: info.userId,
      });
    }
  }, [info]);

  // 表单的ref
  const FormBoxRef = useRef<FormInstance>(null);

  // 从仓库拿 项目职能 下拉框
  const arr1 = useSelector((state: RootState) => state.A2Dict.A2Tab1_1Obj.job);

  // 从仓库拿 用户姓名 下拉框
  const arr2 = useSelector((state: RootState) => state.A3User.tableInfo.list);

  // 没有通过校验
  const onFinishFailed = useCallback(() => {}, []);

  // 通过校验点击确定
  const onFinish = useCallback(
    async (value: any) => {
      const res = await A1_APIUsave({
        dictJobId: value.dictJobId,
        userId: value.userId,
        id: info.id > 0 ? info.id : null,
        projectId: info.projectId,
      });

      if (res.code === 0) {
        if (info.id > 0) {
          MessageFu.success("编辑成功!");
          editFu();
        } else {
          MessageFu.success("新增成功!");
          addFu();
        }
        closeFu();
      }
    },
    [addFu, closeFu, editFu, info.id, info.projectId]
  );

  return (
    <Modal
      wrapClassName={styles.A1UAdd}
      destroyOnClose
      open={true}
      title={info.id === -1 ? "新增成员" : "编辑成员"}
      footer={
        [] // 设置footer为空，去掉 取消 确定默认按钮
      }
    >
      <div className="A1UAmain">
        <Form
          scrollToFirstError={true}
          ref={FormBoxRef}
          labelCol={{ span: 5 }}
          name="basic"
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="项目职能"
            name="dictJobId"
            rules={[{ required: true, message: "请选择项目职能!" }]}
          >
            <Select
              placeholder="请选择"
              options={arr1.map((v) => ({ label: v.name, value: v.id }))}
            />
          </Form.Item>

          <Form.Item
            label="用户"
            name="userId"
            rules={[{ required: true, message: "请选择用户!" }]}
          >
            <Select
              placeholder="请选择"
              options={arr2
                .filter((v) => v.userName !== "admin")
                .map((v) => ({
                  label: v.userName + " - " + v.realName,
                  value: v.id,
                }))}
            />
          </Form.Item>

          <div className="A1UAbtn">
            <Popconfirm
              title="放弃编辑后，信息将不会保存!"
              okText="放弃"
              cancelText="取消"
              onConfirm={closeFu}
              okButtonProps={{ loading: false }}
            >
              <Button>取消</Button>
            </Popconfirm>
            &emsp;
            <Button type="primary" htmlType="submit">
              确定
            </Button>
          </div>
        </Form>
      </div>
    </Modal>
  );
}

const MemoA1UserAdd = React.memo(A1UserAdd);

export default MemoA1UserAdd;
