import React, { Component } from 'react';
import { getControl } from 'zorod/components/Zform/controls';
import { ZsearchListHOC } from 'zorod';
import api from '../Api';
import { Row, Col, Card, Form, Input, Button, message, Icon, Checkbox, Select, Cascader, Table, Popconfirm, Divider } from 'antd';
import TagRuleSetPop from '../popComponents/TagRuleSetPop'

const FormItem=Form.Item;
const TextArea=Input.TextArea;


class ecTagRuleList 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.getListTagRule(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.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
				data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_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: '新增标签生成规则',
			setMode: 'add',
			popData: {},
			detailMode: true
		})
	}
	// 打开更新弹窗
	sourceSet = (key) => {
		api.system.getSingleTagRule(key).then(res => {
			console.log(res)
			this.setState({
				popShow: false,
				popMode: '标签生成规则配置',
				setMode: 'update',
				popData: res.data,
				detailMode: true
			})
		})
	}
	// 打开详情窗口
	sourceDetailSet = (key) => {
		api.system.getSingleTagRule(key).then(res => {
			console.log(res)
			this.setState({
				popShow: false,
				popMode: '标签生成规则详情',
				setMode: '',
				popData: res.data,
				detailMode: false
			})
		})
	}
	// 新增一条数据
	addDataSource = (data) => {
		api.system.addTagRule(data).then(res => {
			this.setState({
				popShow: true
			})
			api.system.getListTagRule(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.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
					data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_TYPE]
				}
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// 修改一条数据
	updateDataSource = (data) => {
		api.system.updateTagRule(data).then(res => {
			console.log(res)
			this.setState({
				popShow: true
			})
			api.system.getListTagRule(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.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
					data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_TYPE]
				}
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// 删除一条数据
	deleteDataSource = (key) => {
		api.system.deleteTagRule(key).then(res => {
			console.log(res)
			api.system.getListTagRule(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.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
					data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_TYPE]
				}
				this.setState({
					dataSource: res.data.list,
					pagination
				})
			})
		})
	}
	// 搜索
	search = () => {
		this.props.form.validateFields((err, values)=>{
			// values = JSON.stringify(values)
			console.log(values)
			this.setState({
				searchValues: values
			})
			api.system.getListTagRule(1, 10, values).then(res => {
				const pagination = { ...this.state.pagination }
				pagination.current = 1
				pagination.total = res.data.total
				for (let data of res.data.list) {
					data.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
					data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_TYPE]
				}
				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.getListTagRule( pagination.current, pagination.pageSize, this.state.searchValues).then(res => {
			const pagination = { ...this.state.pagination }
			for (let data of res.data.list) {
				data.TAG_TYPE = ['基础标签', '模型标签', '趋势标签'][data.TAG_TYPE]
				data.RULE_TASH_TYPE = ['JAVA任务方式', '调用shell方式', '调用sql方式'][data.RULE_TASH_TYPE]
			}
			this.setState({
				dataSource: res.data.list,
        pagination
			})
		})
	}

	render() { 
		const { getFieldDecorator, getFieldsError, getFieldError, isFieldTouched } = this.props.form
		const formItemLayout = {
		    labelCol: { span: 8 },
		    wrapperCol: { span: 16 },
		  }
		const buttonItemLayout = {
						wrapperCol: { span: 14, offset: 4 },
					}
		const formTailLayout = {
			labelCol: { span: 12 },
			wrapperCol: { span: 12 },
		}
		const formButtom={
			wrapperCol: { span: 12, offset: 6 }
		}
		const columns = [
			{
				title: '标签规则名称',
				dataIndex: 'RULE_NAME',
				width: '10%',
				key: 'RULE_NAME'
			},
			{
				title: '规则描述',
				dataIndex: 'DESCRIBE',
				width: '15%',
				key: 'DESCRIBE'
			},
			{
				title: '任务类型',
				dataIndex: 'RULE_TASH_TYPE',
				width: '10%',
				key: 'RULE_TASH_TYPE'
			},
			{
				title: 'API_URL',
				dataIndex: 'API_URL',
				width: '15%',
				key: 'API_URL'
			},
			{
				title: 'API传参',
				dataIndex: 'TRANS_PARAM_FORMAT',
				key: 'TRANS_PARAM_FORMAT'
			},
			{
				title: 'SQL',
				dataIndex: 'SQL',
				key: 'SQL'
			},
			{
				title: '标签类型',
				dataIndex: 'TAG_TYPE',
				width: '10%',
				key: 'TAG_TYPE'
			},
			{
				title: '操作',
				key: 'actions',
				width: '350px',
				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>}>
						<FormItem label="生成规则配置" {...formItemLayout}
							style={{ display: 'inline-block', width: 'calc(25% - 12px)' }}
						>
							{
								getFieldDecorator('RULE_NAME', {
									rules: [
										{
											required: false,
											message: '请输入标签规则名称',
										},
									],
								})
								(<Input placeholder="请输入标签规则名称" />)
							}
						</FormItem>
						<FormItem label="描述" {...formItemLayout}
							style={{ display: 'inline-block', width: 'calc(25% - 12px)' }}
						>
							{
								getFieldDecorator('DESCRIBE', {
									rules: [
										{
											required: false,
											message: '请输入描述',
										},
									],
								})
								(<Input placeholder="请输入描述" />)
							}
						</FormItem>
						<FormItem label="任务类型" {...formItemLayout}
							style={{ display: 'inline-block', width: 'calc(20% - 12px)' }}
						>
							{getFieldDecorator('RULE_TASH_TYPE', {
								initialValue: '0',
								rules: [
									{
										required: false,
										message: '请输入任务类型',
									},
								],
							})(<Select style={{ width: 100 }}>
								<option value="0">0全部</option>
								<option value="1">1SQL</option>
								<option value="2">2SHELL脚本</option>
								<option value="3">3JAVA</option>
							</Select>)}
						</FormItem>
						<FormItem label="标签类型" {...formItemLayout}
							style={{ display: 'inline-block', width: 'calc(20% - 12px)' }}
						>
							{getFieldDecorator('TAG_TYPE', {
								initialValue: '0',
								rules: [
									{
										required: false,
										message: '请输入运行结果',
									},
								],
							})(<Select style={{ width: 100 }}>
								<option value="0">0全部</option>
								<option value="1">1基础标签</option>
								<option value="2">2模型标签</option>
								<option value="3">3趋势标签</option>
							</Select>)}
						</FormItem>
						<FormItem
							style={{ display: 'inline-block', width: 'calc(10% - 12px)' }}
						>
							<Button icon='search' onClick={this.search} type='primary'>搜索</Button>
						</FormItem>
						<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 &&
						<TagRuleSetPop
							title={this.state.popMode}
							onPopClose={this.closePop}
							onAdd={this.addDataSource}
							onUpdate={this.updateDataSource}
							data={this.state.popData}
							setMode={this.state.setMode}
							detailMode={this.state.detailMode}
						></TagRuleSetPop>
					}
				</div>
			</div>
		)
	}
}
export default Form.create({ name: 'ecTagRuleList' })(ecTagRuleList);
//export default EcTashAdd;