//病区对照
import React from 'react';
import { Input, Button, Select, Row, Col,Popconfirm,message} from 'antd';
import { $http } from '../../../containers/config/https';
import store from '../../../store';
import PubilcTablePagination from '../../common/PubilcTablePagination.jsx'; // 公共table组件

const Option = Select.Option;
let user;
export default class WardContrst extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			id: '',
			locDr: "",
			locid:'',
			// 表格数据
			tableData: [],
			DepartmentInformation: [], // 科室名称下拉框
			// 科室信息下拉框数据
			selectData: {},
			// 表格数据总条数
			total: 0,
			showLoading: true,
			page: 1,
			pageSize: 10
		};
		user= window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
		this.columns = [
			{
				key: 1,
				title: '科室名称',
				dataIndex: 'locDesc',
				align: "center",
				width: 130
			},
			{
				key: 2,
				title: '关联科室代码',
				dataIndex: 'llocCode',
				align: "center",
				width: 130
			},
			{
				key: 3,
				title: '关联科室名称',
				dataIndex: 'llocDesc',
				align: "center",
				width: 130
			},
			{
				title: '操作',
				key: 9,
				align: "center",
				width: 130,
				render: (text, record) => (
					<span>
						<Popconfirm title="删除不可恢复，你确定要删除吗?" onConfirm={() => this.handleDelete(record)} >
							<span className='span' style={{ color: 'rgba(240,65,52,1)' }}>删除</span>
						</Popconfirm>
					</span>
				),
			}
		]
	}

	componentDidMount() {
		this.setState({showLoading: false});
		const locDr = this.props.locID;
		//console.log(this.props.locid)
		if(locDr !== ''){
				// 初始化列表数据
				this.handleQuery();
				this.getDepartmentInformation();
				this.setState({
					locDr:locDr
				})		

		}
	}

	componentWillReceiveProps(nextProps) {
		// 当修改选择的数据后刷新数据
		if (nextProps.locID !== '') {
			// this.handleQuery(this.props.locid);
		} else {
		//		this.setState({ locid: "" })
		}
}
   // 3.13.23.获取科室信息
	getDepartmentInformation() {
	
		let data = {
			params: [{
				locID:this.props.locID
			}]
		}
		$http.post('urlS', this, {
			code: "01040035",
			data: data,
			success: function (res) {
				//console.log(res.result.result.Data)
				if (res.errorCode == '0') {
					this.setState({DepartmentInformation: res.result})
				} else {
					this.setState({DepartmentInformation: []})
				}
			}
		})
	}

	// 查询tab数据 
	handleQuery = (locDr) => {
		
		let { page, pageSize } = this.state;
		let data = {
			params: [{
				locID:locDr?locDr:this.props.locID
			}],
			pagination: [{
				pageSize: pageSize,
				currentPage: page
			}]
		}
		$http.post('urlS', this, {
			code: "01050011",
			data: data,
			success: function (res) {
				if (+res.errorCode === 0  && res.result) {
					for(var i = 0; i < res.result.Data.length; i++) {
						res.result.Data[i].key = i
					}
					this.setState({ tableData: res.result.Data,total: res.result.TotalCount })
					// 数据加载成功关闭loading
					this.child.handleLoading();
				} else {
					this.setState({tableData: [], total: 0})
						// 数据加载成功关闭loading
						this.child.handleLoading();
				}
			},
			error: function (error) {
				this.setState({tableData: [], total: 0}, () => {
					// 数据加载成功关闭loading
						this.child.handleLoading();
				})
			}   
		})
	}
	// 查询条件 --- 科室名称
	handleChange = (e) => {
	   this.setState({ locDr: e });
	}
  	// 提供修改page和pageSize的回调函数
	compilePage = (page, pageSize) => {
		// 修改page，或者pageSize
		this.setState({page: page, pageSize: pageSize}, () => {
			// 重新调用查询方法
			let {locDr} = this.state;
			this.handleQuery(locDr);
		})
	}
	// 新增
	handelSave = () => {
		let user = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
		if( this.state.locDr==="")
		{
			message.error("请先选择对应科室！");
			return;
		}
		let data = {
			params: [{
				locID:this.props.locID,//科室ID
				llocID: this.state.locDr,//病区ID
				updateUserID:user.userID//创建人
			}]
		}
		$http.post('urlS', this, {
			code: "01050012",
			data: data,
			success: (res) => {
				if (+res.errorCode === 0) {
					message.success('新增成功');
					this.handleQuery(this.props.locID);
					// this.state({page:1},()=>{
					// 	this.handleQuery(this.props.locid);
					// })	
				}
			}
		})
	}
			// 删除
			handleDelete = (record) => {
				// debugger;
				// console.log(record);
				let data = {
						params: [{
							llocID: record.locLinkLocID,
							updateUserID:user.userID
						}]
				}
				$http.post('urlADS', this, {
						code: '01050013',
						data: data,
						success: function (res) { 
								if (+res.errorCode === 0) {
										message.success('删除成功');
										// 刷新列表数据	 
										 this.handleQuery(this.props.locID);
								} 
						}
				})
		}
		

  
	// 调用公共组件的连接事件
	onRef = (ref) => {
		this.child = ref
	}

	render() {
		// 组件所需参数
		const param = {
			// 表头配置
			columns: this.columns,
			// 表格数据
			data: this.state.tableData,
			// 数据总条数
			total: this.state.total,
			page: this.state.page,
			// 表格的宽度
			x: 900,
			y: store.getState().tableHeight.y -70,
			height: store.getState().tableHeight.y -25 + 'px',
		}
		return (
			<div>
				<div className="table-operations">
					<Row style={{ height: '80px', lineHeight: '80px', padding: '0 24px' }}>
						<Col span={24}>
							对应科室：<Select
								showSearch
								allowClear
								style={{ width: 200 }}
								placeholder="请输入"
								optionFilterProp="title"
								onChange={this.handleChange}
								filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0 ||
									option.props.code.toLowerCase().indexOf(input.toLowerCase()) >= 0 }
							>
								{this.state.DepartmentInformation && this.state.DepartmentInformation.map(item => {
												return <Option key={item.id} code={item.descriptsSPCode} title={item.descripts + item.descriptsSPCode.toLowerCase()}>{item.descripts}</Option>
								})}
							</Select>  
							<Button type="primary" onClick={this.handelSave} style={{ marginBottom: '12px', marginLeft: '24px', display: 'inline-block' }}>增加</Button>
						</Col>
					</Row>
				</div>
				<div style={{ widht: '100%', height: '10px', background: '#f0f2f5' }}></div>

				<div style={{ padding: 24 }}>
					{/* handelThis将夫组件的属性与方法给子组件访问 */}
					<PubilcTablePagination param={param} handelThis={this} onRef={this.onRef} />
				</div>

			</div>
		);
	}
}