import React,{ Component } from "react";
import { Modal,Checkbox,Select,Input,Spin,Icon} from "antd";
import axios from'axios';
import NoneData from "../coursesPublic/NoneData";
import Modals from '../../modals/Modals';

const Option = Select.Option;
const Search = Input.Search;

function formatDate(date) {
	var dateee = new Date(date).toJSON();
	return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
}

class Selectresource extends Component{
	constructor(props){
		super(props);
		this.state={
			Searchvalue:undefined,
			type:'all',
			category_id:0,
			page:1,
			Resourcelist:undefined,
			hometypepvisible:true,
			getallfiles:false,
			searchtype:'getallfiles'
		}
	}
	componentDidMount() {
		this.getallfiles(undefined,1)
	}

	componentDidUpdate = (prevProps) => {

    let {getallfiles}=this.state;

		if ( prevProps.visible != this.props.visible ) {

		}

	}

	//勾选实训
	shixunhomeworkedit=(list)=>{

		this.setState({
			patheditarry:list
		})

	}


	//搜索
	SenttotheValue=(e)=>{
		this.setState({
			Searchvalue:e.target.value,
			page:1,
		})
	}

	SenttotheSearch=(value)=>{
    let {searchtype}=this.state;
		if(searchtype==="getallfiles"){
			this.getallfiles(value,1)
		}else if(searchtype==="getmyfiles"){
			this.getmyfiles(value,1)
		}
	}


	setupdatalist=(url,newpage)=>{
		let {Resourcelist,Searchvalue}=this.state;
		if(Resourcelist!=undefined){
		let newResourcelist=Resourcelist.files;
		axios.get(url,{
			params:{
				page:newpage,
				page_size:15,
				search:Searchvalue,
			}
		}).then((result)=>{

	      var list=result.data.data.files;
				for(var i=0;i<list.length; i++ ){
				  newResourcelist.push(list[i])
				}
        let lists={
	        total_count:Resourcelist.total_count,
	        files:newResourcelist
        }
				this.setState({
					Resourcelist:lists,
					hometypepvisible:false,
					getallfiles:true,
					page:newpage
				})
		}).catch((error)=>{
			console.log(error)
		})
		}
	}
	contentViewScroll=(e)=>{
		let {page,searchtype}=this.state;

		//滑动到底判断
		if(e.currentTarget.scrollHeight-e.currentTarget.scrollTop===e.currentTarget.clientHeight){
			let newpage=page+1;
			let url
			if(searchtype==="getallfiles"){
				url="/files/public_with_course_and_project.json";
				this.setupdatalist(url,newpage)

			}else if(searchtype==="getmyfiles"){
				url="/files/mine_with_course_and_project.json";
				this.setupdatalist(url,newpage)
			}
		}

	}

	//tag
	changeTag=(types)=>{

		this.setState({
			type:types,
			page:1,
			patheditarry:[],
			Resourcelist:undefined
		})
		// this.props.funshixunmodallist(Searchvalue,types,true,1)
		// this.props.funpatheditarry([])
		if(types==="all"){
      this.getallfiles(undefined,1)
		}else if(types==="my"){
			this.getmyfiles(undefined,1)
		}

	}

	getallfiles=(value,page)=>{
		let url = "/files/public_with_course_and_project.json";
		axios.get(url,{
			params:{
				page:1,
				page_size:15,
				search:value,
			}
		}).then((result)=>{
	      this.setState({
		      Resourcelist:result.data.data,
		      hometypepvisible:false,
		      getallfiles:true,
		      searchtype:'getallfiles'
	      })
		}).catch((error)=>{
			console.log(error)
		})
	}

	getmyfiles=(value,page)=>{

		let url = "/files/mine_with_course_and_project.json";
		axios.get(url,{
			params:{
				page:1,
				page_size:15,
				search:value,
			}
		}).then((result)=>{
				this.setState({
					Resourcelist: result.data.data,
					hometypepvisible:false,
					searchtype:'getmyfiles'
				})
		}).catch((error)=>{
			console.log(error)
		})
	}


	hidecouseShixunModal=()=>{
		 this.props.hidecouseShixunModal()
	}

	ModalCancelModalCancel=()=>{
		this.setState({
			Modalstype:false,
			visible:false,
			Modalstopval:"",
			ModalSave:this.ModalCancelModalCancel,
			loadtype:false
		})
		this.props.hidecouseShixunModal()
	}

	savecouseShixunModal=()=>{

		let {patheditarry}=this.state;
		let {coursesId,attachmentId}=this.props;
	   let url="/files/import.json";

	  if(patheditarry===undefined||patheditarry.length===0){
	  	this.setState({
			  patheditarrytype:true
		  })
		  return
	  }else{
		  this.setState({
			  patheditarrytype:false
		  })
	  }

		axios.post(url, {
				course_id:coursesId,
				attachment_ids:patheditarry,
				course_second_category_id:this.props.coursesidtype===undefined||this.props.coursesidtype==="node"?0:attachmentId,
			}
		).then((response) => {
				if(response.data.status===0){
					// this.setState({
					// 	Modalstype:true,
					// 	Modalstopval:response.data.message,
					// 	ModalSave:this.ModalCancelModalCancel,
					// 	loadtype:true
					// })
					this.ModalCancelModalCancel();
					this.props.updataleftNavfun();
					this.props.showNotification("选用资源成功");
					this.props.setupdate(attachmentId)
				}
		}).catch((error) => {
			console.log(error)
		})

	}

	selectCloseList=(value)=>{
		this.setState({
			category_id:value
		})
	}
	render(){
		let {Searchvalue,type,category_id,Resourcelist,hometypepvisible,patheditarry}=this.state;
		let {visible,shixunmodallist}=this.props;
		const antIcon = <Icon type="loading" style={{ fontSize: 24 }} spin />;
		return(
			<div>
				{/*提示*/}
				<Modals
					modalsType={this.state.Modalstype}
					modalsTopval={this.state.Modalstopval}
					modalCancel={this.state.ModalCancel}
					modalSave={this.state.ModalSave}
					loadtype= {this.state.loadtype}

				/>
				{visible===true?
				<Modal
					keyboard={false}
					title="选用资源"
					visible={visible}
					closable={false}
					footer={null}
					width="600px"
					destroyOnClose={true}
				>
					<style>
						{
							`
							.ant-modal-body{
                  padding: 30px 0px;
							}
							`
						}
					</style>

						<Spin indicator={antIcon} spinning={hometypepvisible}  size="large" style={{marginTop:'15%'}}>
						<style>{`
                .newupload_conboxtop{
                   margin-top: -30px;
                }
                #shixun_tab_div{
                   padding: 0 30px;
                   padding-top:30px;
                }
                .search-news{
									width: 237px!important;
									height: 30px;
									margin-bottom: 30px;
                }
              `}</style>

						<div className="newupload_conbox newupload_conboxtop">
							<div className="clearfix shixun_work_div newshixun_tab_div cdefault" style={{"marginRight":"4px"}} id="shixun_tab_div">

								<li className="fl mr5 mt5"> <a onClick={()=>this.changeTag("all")} className={ type==="all" ? "active edu-filter-cir-grey font-12":"edu-filter-cir-grey font-12"}>全部</a></li>
								<li className="fl mr5 mt5 ml15"> <a onClick={()=>this.changeTag("my")} className={ type==="my" ? " active edu-filter-cir-grey font-12":"edu-filter-cir-grey font-12"}>我的资源</a></li>
								<div className="fr search-news" >
									<Search
										placeholder="请输入发布人或名称进行搜索"
										value={Searchvalue}
										onInput={this.SenttotheValue}
										onSearch={(value) => this.SenttotheSearch(value)}
										style={{width: '100%'}}
									/>
								</div>
							</div>


							<style>{`
                .greybackHead{
                  padding:0px 30px;
                }
                .fontlefts{text-align: left;}
              `}</style>
							<ul className="clearfix greybackHead edu-txt-center">
								<li className="fl paddingleft22 fontlefts" style={{width:'220px'}}>资源名称</li>
								<li className="fl edu-txt-left" style={{width:'80px'}}>大小</li>
								<li className="fl" style={{width:'100px'}}>发布人</li>
								<li className="fl" style={{width:'100px'}}>时间</li>
							</ul>


							<style>{`
                   .color-grey-9a{color: #9A9A9A !important;}
                   .datastyle{
                      width: 120px;
									    overflow: hidden;
									    height: 37px;
                   }
                   .scrollbox{
                     height:250px !important;
                   }
                   .selectfilsbox{
								     height: 50px;
								     line-height: 50px;
                   }
                `}</style>
						  	<div className="over210 pl20 pr20 scrollbox"
							     onScroll={this.contentViewScroll}
							     >

								{
									Resourcelist === undefined ?"":Resourcelist.files.length===0?<NoneData/>:

										<Checkbox.Group style={{ width: '100%' }} value={patheditarry}  onChange={this.shixunhomeworkedit}>
											{
											  Resourcelist.files.map((item,key)=>{
													return(
														<div className="clearfix edu-txt-center lineh-40 bor-bottom-greyE selectfilsbox" key={key}>
															<li className="fl" style={{width:'220px'}}>
																<Checkbox
																	value={item.id}
																	key={item.id}
																	className="fl task-hide edu-txt-left"
																	style={{"width":"220px"}}
																	name="shixun_homework[]"
																>
																	<label style={{"textAlign":"left","color":"#4C4C4C"}} className="task-hide" title={item.title}>{item.title}</label>
																</Checkbox>
															</li>
															<li className="fl edu-txt-left task-hide paddingl5 color-grey-9a" style={{width:'90px'}}>{item.filesize}</li>
															<li className="fl paddingl10 color-grey-9a" style={{width:'100px'}}>{item.author.name}</li>
															<li className="fl color-grey-9a paddingl10 datastyle">{formatDate(item.created_on)}</li>
														</div>
													)
												})
											}
										</Checkbox.Group>

								}
								</div>



							{this.state.patheditarrytype===true?<p className={"color-red ml20"}>请选择资源</p>:""}

							<div className="mt20 marginauto clearfix edu-txt-center">
								<a className="pop_close task-btn mr30 margin-tp26" onClick={this.hidecouseShixunModal}>取消</a>
								<a className="task-btn task-btn-orange margin-tp26" id="submit_send_shixun" onClick={this.savecouseShixunModal}>确定</a>
							</div>
						</div>
					</Spin>
				</Modal>:""}
			</div>
		)
	}
}
export default Selectresource;



