import React from 'react';
import { Icon, Table, Modal, message, Spin, Input, Select, Button, Form } from 'antd';
import ajax from 'jquery/src/ajax/xhr.js';
import $ from 'jquery/src/ajax';
import './userMangagement.less';
import {} from '../../../dataDic.js';

const Newrole = Form.create()(React.createClass({
	addloadData() {
		this.setState({
			loading: true
		});
		$.ajax({
			method: "post",
			dataType: "json",
			crossDomain: false,
			url: globalConfig.context + '/api/admin/permissions',
			data: {
				pageNo: 1,
				pageSize: 10,
			},
			success: function(data) {
				let theArr = [];
				if(!data.data || !data.data.one) {
					if(data.error && data.error.length) {
						message.warning(data.error[0].message);
					};
				} else {
					this.setState({
						Interface: data.data.one,
					});
				};
			}.bind(this),
		}).always(function() {
			this.setState({
				loading: false
			});
		}.bind(this));
	},
	getInitialState() {
		return {
			orgCodeUrl: [],
			loading: false,
			visible: false,
			addColumns: [{
				title: '选择角色权限',
				dataIndex: 'name',
				key: 'name',
			}, {
				title: '',
				dataIndex: 'Interface',
				key: 'Interface',
			}],
		};
	},
	handleSubmit(e) {
		e.preventDefault();
		//let api=this.props.userDetaile?'编辑':'新建';
		this.props.form.validateFields((err, values) => {
	        var tmp = [];
	        let selectAllId=this.state.selectedRowKeyst;
	        for(var i in selectAllId){
	            //该元素在tmp内部不存在才允许追加
	            if(tmp.indexOf(selectAllId[i])==-1){
	                tmp.push(selectAllId[i]);
	            }
	        }
			if(!err) {
				this.setState({
					loading: true
				});
				$.ajax({
					method: "POST",
					dataType: "json",
					crossDomain: false,
					url: globalConfig.context + 'api',
					data: {
						roleName: this.state.roleName,
						id:JSON.stringify(tmp)
					}
				}).done(function(data) {
					this.setState({
						loading: false
					});
					if(!data.error.length) {
						message.success('保存成功！');
						this.handleCancel()
					} else {
						message.warning(data.error[0].message);
					}
				}.bind(this));
			}
		});
	},
	//查看基本详情基本信息
	loaduser(record) {
		if(record) {
			$.ajax({
				method: "get",
				dataType: "json",
				crossDomain: false,
				url: globalConfig.context + '/api/admin/customer/toUpdateBusiness',
				data: {
					businessId: record.id
				},
				success: function(data) {
					let thisData = data.data;
					if(!thisData) {
						if(data.error && data.error.length) {
							message.warning(data.error[0].message);
						};
						thisData = {};
					};
					this.setState({
						roleName: thisData.roleName,
						selectedRowKeyst:["f9ded9d3-7987-417b-90e8-1ddd9622ada5", "cf637978-ebe8-4be6-9356-f50bc1b81172", "470aad28-e980-4bc5-a8fb-60992c6ee345"],
					});
				}.bind(this),
			}).always(function() {
				this.setState({
					loading: false
				});
			}.bind(this));
		}
	},
	handleOk(e) {
		this.setState({
			visible: false,
		});
		this.props.closeDesc(false, true);
	},
	handleCancel(e) {
		this.setState({
			visible: false,
		});
		this.props.closeDesc(false);
	},
	componentWillMount() {
		this.addloadData()
	},
	componentWillReceiveProps(nextProps) { //props改变时触发
		this.state.visible = nextProps.showDesc;
		if(nextProps.userDetaile && nextProps.showDesc) {
			if(nextProps.datauser && nextProps.datauser.id) {
				this.loaduser(nextProps.datauser);
			}
		}
		this.setState({
			selectedRowKeyst: [],
			datauser: {},
		})

	},
	tableRowClick(record, index) {
		console.log(record)
	},
	render() {
		const FormItem = Form.Item
		const formItemLayout = {
			labelCol: { span: 8 },
			wrapperCol: { span: 14 },
		};
		const rowSelection = {
			selectedRowKeys: this.state.selectedRowKeyst,
			onChange: (selectedRowKeys, selectedRows) => {
				let allList = this.state.Interface
				let allId = [];
				let autAll = [];
				let selectId = [];
				let rowKey = selectedRowKeys;
				selectedRows.map(function(item, index) {
					if(item.autNo.length == 4) {
						let autoNo = item.autNo
						allList.map(function(item) {
							autAll.push(item.autNo);
							allId.push(item.id)
							if(item.children.length) {
								item.children.map(function(ttm) {
									autAll.push(ttm.autNo)
									allId.push(ttm.id)
									if(ttm.children.length) {
										ttm.children.map(function(itt) {
											autAll.push(itt.autNo)
											allId.push(itt.id)
										})
									}
								})
							}
						})
						for(var i = 0; i < autAll.length; i++) {
							if(autAll[i].indexOf(autoNo)) {} else {
								selectId.push(allId[i])
							}
						}
						rowKey = selectId
						if(rowKey.length==2){
							rowKey = selectedRowKeys
						}
						return rowKey;
					}
					if(item.autNo.length > 4) {
						rowKey = selectedRowKeys
					}
				})
				this.setState({
					selectedRowst: selectedRows,
					selectedRowKeyst: rowKey
				});
				console.log(rowKey)
			},
		};
		return(
			<div>
           <Modal maskClosable={false} visible={this.state.visible}
                        onOk={this.handleOk} onCancel={this.handleCancel}
                        width='1000px'
                        title={!this.props.userDetaile?'添加角色':'编辑角色'}                      
                        footer=''
                        className="admin-desc-content">
			            <Form layout="horizontal" onSubmit={this.handleSubmit} id="demand-form" style={{paddingBottom:'40px'}} required="required">
			                <Spin spinning={this.state.loading}>
			                        <div className="clearfix">
			                            <div className="clearfix">
					                    	<FormItem className="half-item"
						                            {...formItemLayout}
						                            label="角色名称" >                             
					                                    <Input placeholder="角色名称" value={this.state.roleName} onChange={(e)=>{this.setState({roleName:e.target.value})}} style={{width:'200px'}} required="required"/>
						                    		<span className="mandatory">*</span>
						                    </FormItem>
						                    <FormItem className="half-item"
						                            {...formItemLayout}
						                            label="" > 
							                    <Button className="set-submit" type="primary" htmlType="submit">保存</Button>  
				                                <Button className="set-submit" type="ghost" onClick={this.handleCancel}>返回</Button> 
			                                </FormItem>
						                </div>
						                <div className="clearfix">
						                	<Table columns={this.state.addColumns}
					                            dataSource={this.state.Interface}
					                            rowSelection={rowSelection}
					                            onRowClick={this.tableRowClick} 
					                            scroll={{ y: 500 }}
					                            pagination={false}
					                            />
						                </div>
				                    </div>
			                </Spin>
			            </Form >
			        </Modal>    
    	    </div>
		)
	}
}));

export default Newrole;