import React, { Component } from 'react';
import { getControl } from 'zorod/components/Zform/controls';
import { ZsearchListHOC } from 'zorod';
import api from '../Api';
import { Card, Form, Input, Button, message, Icon, Checkbox, Select, Cascader, Table, Popconfirm, Divider } from 'antd';
import MapperSetPop from '../popComponents/MapperSetPop'

const FormItem=Form.Item;
const TextArea=Input.TextArea;

const formItemLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 12 },
  };
  const formTailLayout = {
    labelCol: { span: 4 },
    wrapperCol: { span: 12, offset: 4 },
  };
  const formButtom={
    wrapperCol: { span: 12, offset: 6 }
  }
const residences=["baijing","hunian","guangxi"];

class ecMapperList extends Component {
	constructor(props) {
			super(props)
			this.state = {
				dataSource: [],
				selectedRowKeys: [], // Check here to configure the default column
				loading: false,
				popShow: true,
				popMode: '',
				popData: {},
				setMode: '',
				pagination: {},
				searchValues:  {"DATABASE_TYPE": "0"}
			}
	}

	// 页面加载函数
	componentDidMount() {
		api.system.getListMapper(1, 10, this.state.searchValues).then(res => {
			const pagination = { ...this.state.pagination }
			pagination.current = 1
			pagination.total = res.data.total
			// for (let data of res.data.list) {
			// 	data.DATASOURCE_TYPE = ['0.全部', '1.数据库', '2.API', '3.文件'][data.DATASOURCE_TYPE]
			// }
			this.setState({
				dataSource: res.data.list,
				pagination
			})
		})
	}
	//表单提交
	handelsubmit=()=>{
			let userInfo=this.props.form.getFieldsValue
			this.props.form.validateFields((err,values)=>{
						if(!err){
							console.log(values)
							message.success(`添加成功!!!`)
						}
			})
	}
	//取消
	handelConcel=()=>{
		this.setState({
			popShow: false
		})
	}
	// 关闭弹窗
	closePop = () => {
		this.setState({
			popShow: true
		})
	}
	// 打开新增弹窗
	sourceAdd = () => {
		this.setState({
			popShow: false,
			popMode: '新增编码映射',
			popData: {},
			setMode: 'add'
		})
	}
	// 打开更新弹窗
	sourceSet = (key) => {
		api.system.getSingleMapper(key).then(res => {
			console.log(res)
			this.setState({
				popShow: false,
				popMode: '编码映射配置',
				setMode: 'update',
				popData: res.data
			})
		})
	}
	// 打开详情窗口
	sourceDetailSet = (key) => {
		api.system.getSingleMapper(key).then(res => {
			this.setState({
				popShow: false,
				popMode: '编码映射详情',
				setMode: '',
				popData: res.data,
				detailMode: false
			})
		})
	}
	// 新增一条数据
	addDataSource = (data) => {
		console.log(data)
		api.system.addMapper(data).then(res => {
			this.setState({
				popShow: true
			})
			api.system.getListMapper(1, 10, this.state.searchValues).then(res => {
				const pagination = { ...this.state.pagination }
				pagination.current = 1
				pagination.total = res.data.total
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// 修改一条数据
	updateDataSource = (data) => {
		api.system.updateMapper(data).then(res => {
			this.setState({
				popShow: true
			})
			api.system.getListMapper(1, 10, this.state.searchValues).then(res => {
				// for (let data of res.data.list) {
				// 	data.TYPE_ID = ['0.全部', '1.姓名', '2.职业'][data.TYPE_ID]
				// }
				const pagination = { ...this.state.pagination }
				pagination.current = 1
				pagination.total = res.data.total
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// 删除一条数据
	deleteDataSource = (key) => {
		api.system.deleteMapper(key).then(res => {
			console.log(res)
			api.system.getListMapper(1, 10, this.state.searchValues).then(res => {
				// for (let data of res.data.list) {
				// 	data.TYPE_ID = ['0.全部', '1.姓名', '2.职业'][data.TYPE_ID]
				// }
				const pagination = { ...this.state.pagination }
				pagination.current = 1
				pagination.total = res.data.total
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// reload选项
	start = () => {
		this.setState({ loading: true })
		// ajax request after empty completing
		setTimeout(() => {
			this.setState({
				selectedRowKeys: [],
				loading: false,
			})
		}, 1000)
	}
	// 点击选择一行
  onSelectChange = selectedRowKeys => {
    console.log('selectedRowKeys changed: ', selectedRowKeys)
    this.setState({ selectedRowKeys })
  }
	// 点击翻页
	handleTableChange = pagination => {
		const pager = { ...this.state.pagination }
		pager.current = pagination.current
		this.setState({
			pagination: pager,
		})
		api.system.getListMapper( pagination.current, pagination.pageSize, this.state.searchValues).then(res => {
			const pagination = { ...this.state.pagination }
			this.setState({
				dataSource: res.data.list,
        pagination
			})
		})
	}

	render() { 
		const columns = [
			{
				title: '源码所属表',
				dataIndex: 'SOURCE_TABLE',
				key: 'SOURCE_TABLE'
			},
			{
				title: '源码所属字段',
				dataIndex: 'SOURCE_COLUMN',
				key: 'SOURCE_COLUMN'
			},
			{
				title: '源码',
				dataIndex: 'SOURCE_CODE',
				key: 'SOURCE_CODE'
			},
			{
				title: '对应标准码类别',
				dataIndex: 'sysStandardCode.codeType.TYPE_NAME',
				key: 'sysStandardCode.codeType.TYPE_NAME'
			},
			{
				title: '对应标准码',
				dataIndex: 'sysStandardCode.STANDARD_CODE',
				key: 'sysStandardCode.STANDARD_CODE'
			},
			{
				title: '操作',
				key: 'actions',
				render: (text, record) => (
					<div>
						<Button icon='eye' type='default' size='small' onClick={() => this.sourceDetailSet(record.id)}>详情</Button><Divider type="vertical" />
						<Button icon='edit' type='primary' size='small' onClick={() => this.sourceSet(record.id)}>修改</Button><Divider type="vertical" />
						<Popconfirm placement="topLeft" title='是否确认删除？' onConfirm={() => this.deleteDataSource(record.id)} okText="确认" cancelText="取消">
							<Button icon='delete' type='danger' size='small'>删除</Button>
						</Popconfirm>
					</div>
				)
			}
		]
    const { loading, selectedRowKeys } = this.state
    const rowSelection = {
      selectedRowKeys,
      onChange: this.onSelectChange,
    }
    const hasSelected = selectedRowKeys.length > 0
		return ( 
			<div>
				<div>
					<Card title="标准编码映射配置" extra={<div>
						<Button icon='plus' type='primary' onClick={this.sourceAdd}>新增</Button>
						</div>}
					>
						<Button type="primary" onClick={this.start} disabled={!hasSelected} loading={loading}>
							重置选择
						</Button>
						<span style={{ marginLeft: 8 }}>
							{hasSelected ? `Selected ${selectedRowKeys.length} items` : ''}
						</span>
						<Table
							rowSelection={rowSelection}
							pagination={this.state.pagination}
							loading={this.state.loading}
							onChange={this.handleTableChange}
							columns={columns}
							dataSource={this.state.dataSource}
						></Table>
					</Card>
					{!this.state.popShow &&
						<MapperSetPop
							title={this.state.popMode}
							onPopClose={this.closePop}
							onAdd={this.addDataSource}
							onUpdate={this.updateDataSource}
							data={this.state.popData}
							setMode={this.state.setMode}
						></MapperSetPop>
					}
				</div>
			</div>
		)
	}
}
export default Form.create({ name: 'ecMapperList' })(ecMapperList);
//export default EcTashAdd;