import React from "react";
import { Menu , Dropdown , notification } from "antd";
import {Link} from 'react-router-dom';
import axios from 'axios';

// permission = {
//   admin://管理员
//   banned_permission：//管理员、且有置顶、推荐权限
//   is_currentUser: true,  #是否为当前用户，判断是否显示编辑/删除，并隐藏其他的
//   is_fine: true,  #是否精华帖子
//   sticky: true,  #是否置顶
//   memo_watched: true,  #是否收藏
//   is_deleted:true#是否已经申请删除
// }
export default ({ id , permission , calbackFunc , confirm }) => {  
  // 置顶、取消置顶
  function changeSticky(s){
    let sticky = s ? 0 : 1;//1为置顶，0为取消置顶
    const url = `/memos/${id}/set_top_or_down.json`;
    axios.get(url,{
      params:{
        sticky
      }
    }).then(result=>{
      if(result){
        notification.open({message:"提示",description:result.data.message});
        calbackFunc && calbackFunc();
      }
    })
  }
  // 推荐、取消推荐
  function changeFine(f){
    let is_fine = f ? 0 : 1;//1表示加精，0表示取消加精
    const url = `/memos/${id}/is_fine.json`;
    axios.post(url,{
      is_fine
    }).then(result=>{
      if(result){
        notification.open({message:"提示",description:result.data.message});
        calbackFunc && calbackFunc();
      }
    })
  }

  // 关注、取消关注
  function changeMemoWatched(m){
    let is_watch = m ? 0 : 1;//1为添加关注，0为取消关注
    const url = `/memos/${id}/watch_memo.json`;
    axios.post(url,{
      is_watch
    }).then(result=>{
      if(result){
        notification.open({message:"提示",description:result.data.message});
        calbackFunc && calbackFunc();
      }
    })
  }
  // 管理员直接删除帖子
  function deleteForum(){
    confirm && confirm({
      content: '确认删除帖子?',
      onOk:()=>{
        const url = `/memos/${id}.json`;
        axios.delete(url).then(result=>{
          if(result){
            notification.open({message:"提示",description:result.data.message});
            calbackFunc && calbackFunc();
            window.location.href="/forums"
          }
        })
      }
    })
  }

  // 发布者申请删除、撤销申请删除
  function sendDeleteForum(d){
    let is_apply = d ? 0 : 1;//1为申请删除，0为撤销申请删除
    confirm && confirm({
      content: '确认申请删帖?',
      onOk:()=>{
        const url = `/memos/${id}/confirm_delete.json`;
        axios.post(url,{
          is_apply
        }).then(result=>{
          if(result){
            notification.open({message:"提示",description:result.data.message});
            calbackFunc && calbackFunc();
          }
        })
      }
    })
  }

  const menu=(
    permission && 
    <Menu style={{minWidth:"100px",textAlign:'center'}}>
      { permission.banned_permission && <Menu.Item onClick={()=>changeSticky(permission.sticky)}>{permission.sticky ? "取消置顶":"置顶"}</Menu.Item>}
      { permission.banned_permission && <Menu.Item onClick={()=>changeFine(permission.is_fine)}>{permission.is_fine ? "取消推荐":"推荐"}</Menu.Item>}
      { permission.login && <Menu.Item onClick={()=>changeMemoWatched(permission.memo_watched)}>{permission.memo_watched ? "取消收藏":"收藏"}</Menu.Item> }
      { (permission.admin || permission.is_currentUser) && <Menu.Item><Link to={`/forums/${id}/edit`}>编辑</Link></Menu.Item>}
      { permission.admin ? 
        <Menu.Item onClick={()=>deleteForum()}>删除</Menu.Item>
        : 
        permission.is_currentUser ? 
        <Menu.Item onClick={()=>sendDeleteForum(permission.is_deleted)}>{permission.is_deleted?"撤销申请":"申请删帖"}</Menu.Item>:""
      }
    </Menu>
  )
  return (
    <Dropdown overlay={menu} align={"center"} placement={"bottomCenter"}>
      <i className="iconfont icon-gengduo1"></i>
    </Dropdown>
  );
};
