// 支持授权故事给其他用户，包含查看、新增、增删、管理员权限，查询当前好友列表，选择权限授权
import { queryFriendList } from '@/pages/account/center/service';
import { useRequest } from '@umijs/max';
import { Form, message, Modal, Select } from 'antd';
import { StoryType } from '../data';
import { values } from 'lodash';
import { authorizeStoryPermission } from '../service';

const AuthorizeStoryModal = ({
  open,
  current,
  handleOk,
}: {
  open: boolean;
  current: Partial<StoryType> | undefined;
  handleOk: (flag: boolean) => void;
}) => {
  const { data: friends, loading } = useRequest(() => queryFriendList());
  const [form] = Form.useForm();
  const onFinish = async (values: {userId: string, permissionType: number}) => {
    console.log(current, values);
    if (!values.userId || !values.permissionType || !current?.instanceId) return;
    let params = {userId: values.userId, permissionType: values.permissionType, storyInstanceId: current?.instanceId ?? '11'};
    const res = await authorizeStoryPermission(params);
    console.log(res);
    
    if (res.code === 200) {
      handleOk(true);
    }
  }
  return (
    <Modal title="授权故事" open={open} onCancel={() => handleOk(false)} onOk={() => onFinish(form.getFieldsValue())}>
      {/* 请选择需要授权的好友 */}
      <Form onFinish={onFinish} form={form}>
        <Form.Item name={'userId'} label="授权对象">
          <Select 
          // mode="multiple"
          >
            {friends?.map((item) => {
              return (
                <Select.Option title={item.username} key={item.userId}>
                  {item.username}
                </Select.Option>
              );
            })}
          </Select>
        </Form.Item>
        <Form.Item name="permissionType" label="权限类型">
          <Select>
            <Select.Option key="2" title="查看">
                可编辑
            </Select.Option>
            <Select.Option key="3" title="增加">
                可增加
            </Select.Option>
            <Select.Option key="4" title="增删">
                可增删
            </Select.Option>
            <Select.Option key="5" title="管理">
                可管理
            </Select.Option>
          </Select>
        </Form.Item>
      </Form>
    </Modal>
  );
};

export default AuthorizeStoryModal;
