import React, { Component } from 'react';
import { Spin , Pagination } from 'antd';
import Tags from '../TagComponent/Index';
import '../exchange.css';
import './manage.css';
import Empty from '../Empty'
import ItemLeft from './ItemLeft';
import PassItem from './PassItem';
import axios from 'axios'

class SubSendItem extends Component {
  constructor(props){
    super(props);
    this.state={
      page:1,
      data:undefined,
      isSpin:true,
      limit:10
    }
  }

  componentDidMount=()=>{
    this.getList(1);
  }

  getList = (page) =>{
    this.setState({
      isSpin:true
    })
    const { plateId } = this.props.match.params;
    const url =  `/forum_sections/${plateId}/checked_memos.json`;
    axios.get(url,{params:{
      page
    }}).then((result)=>{
      if(result){
        this.setState({
          data:result.data,
          isSpin:false
        })
      }
    }).catch((error)=>{

    })
  }

  // 删除-已发布的帖子
  deleteEvent=(id)=>{
    const { page } = this.state;
    const url = `/memos/${id}/destroy.json`;
    axios.post(url).then(result=>{
      if(result){
        this.props.showNotification(result.data.message);
        this.getList(page);
      }
    }).catch(error=>{

    })
  }
  // 取消推荐/推荐-已发布的帖子
  isFineEvent=(id,flag)=>{
    const { page } = this.state;
    const url = `/memos/${id}/is_fine.json`;
    axios.post(url,{
      is_fine:flag ? 0 : 1
    }).then(result=>{
      if(result){
        this.props.showNotification(result.data.message);
        this.getList(page);
      }
    }).catch(error=>{

    })
  }
  // 取消置顶/置顶-已发布的帖子
  stickyEvent=(id,flag)=>{
    const { page } = this.state;
    const url = `/memos/${id}/set_top_or_down.json`;
    axios.get(url,{params:{
      sticky:flag ? 0 : 1
    }}).then(result=>{
      if(result){
        this.props.showNotification(result.data.message);
        this.getList(page);
      }
    }).catch(error=>{

    })
  }

  // 禁言
  stopEvent=(id,banned)=>{
    const { current_user } = this.props;
    const url =`/memos/${id}/banned_user.json`
    axios.post((url),{
      user_id:current_user && current_user.user_id,
      banned
    }).then((result)=>{
      if(result){
        const { page } = this.state;
        this.props.showNotification(result.data.message);
        this.getList(page);
      }
    }).catch((error)=>{

    })
  }

  // 分页
  changePageEvent=(page)=>{
    this.setState({
      page
    })
    this.getList(page);
  }

  render(){
    const { data , page , isSpin , limit } = this.state;

    const pageCom = (
      data && data.memos_count > limit &&
      <div className="edu-txt-center pt30 pb30">
        <Pagination showQuickJumper current={page} total={data.memos_count} pageSize={limit} onChange={this.changePageEvent}/>
      </div>
    )
    const dataList = (
      data && data.memos_lists &&  data.memos_lists.length > 0 ?
        <div className="pl30 pr30">
          {
            data.memos_lists.map((item,key)=>{
              const userInfo = {
                forum_id:item.forum_id,
                memo_id:item.memo_id,
                user_url:item.user_url,
                username:item.username,
                time:item.time,
                image_url:item.image_url,
                memo_title:item.memo_title,
                forum_title:item.forum_title
              }
              return(
                <div className="moderatorItems">
                  <div className="df">
                    <ItemLeft {...userInfo}/>
                    <div>
                      <p className="edu-txt-right color-red mb15 mt12" style={{height:"33px"}}>
                        <Tags bestClass={item.is_fine ? "mb5 ml15" : undefined} topClass={item.sticky ? "mb10 ml15" : undefined}></Tags>
                      </p>
                      <p className="ml50 color-grey-9">
                        <span className="c_point" onClick={()=>this.stickyEvent(item.memo_id,item.sticky)}>{item.sticky?"取消置顶":"置顶"}</span>
                        <span className="sendPoint c_point font-14" onClick={()=>this.isFineEvent(item.memo_id,item.is_fine)}>{item.is_fine?"取消推荐":"推荐"}</span>
                        <span className="sendPoint c_point font-14" onClick={()=>this.deleteEvent(item.memo_id)}>删除</span>
                      </p>
                    </div>
                  </div>
                </div>
              )
            })
          }
          { pageCom }
        </div>
        :
        <div className="edu-back-white pt50 pb50">
          <Empty></Empty>
        </div>
    )

    return(
      <Spin spinning={isSpin}>
      { dataList }
      </Spin>
    )
  }
}
export default SubSendItem;