import pageSetting from './pageSetting';
import React, { Component } from 'react';
import PageHeaderWrapper from '@/components/PageHeaderWrapper';
import ListTab from './ListTab';
import { getHandledPage, getWaitPage } from '../publicApiService';
import AuditModal from '@/webPublic/FormInsertDiy/AuditPage/BatchAudit/AuditModal';
import { ModalInfo } from '@/baseComponent/Modal';
import ButtonDiy from '@/baseComponent/ButtonDiy';

export default class AuditPage extends Component {
	constructor(props) {
		super(props);
		this.state = {};
	}

	componentDidMount() {}

	handleButtonSet = () => {
		const { hasBatchAudit, batchAuditButtonName, noNeedForm } = this.props;
		return {
			tab1: [
				hasBatchAudit
					? {
							type: 'audit',
							component: 'RenderComponent',
							render: ({ selectRows, getPage }) => {
								const disabled = !selectRows || !Array.isArray(selectRows) || !selectRows.length;
								function handleClick(onShow) {
									const taskName = selectRows[0].taskName;
									const differentTask = selectRows.slice(1).find((i) => i.taskName !== taskName);
									if (differentTask) {
										return ModalInfo(
											<div>
												<strong style={{ color: 'red' }}> {differentTask.taskName} </strong>和
												<strong> {taskName} </strong>
												不能一起审核。
												<br />
												请将分开操作。
											</div>,
										);
									}
									onShow();
								}
								return (
									<AuditModal selectRows={selectRows} getPage={getPage} noNeedForm={noNeedForm}>
										{({ onShow }) => (
											<ButtonDiy
												name={batchAuditButtonName || '批量审核'}
												handleClick={() => handleClick(onShow)}
												disabled={disabled}
											/>
										)}
									</AuditModal>
								);
							},
					  }
					: false,
			].filter((i) => i),
			tab2: [],
		};
	};

	handleSearchSet = () => {
		const { columns, searchCondition, allConfigSetInfo } = this.props;
		console.log(searchCondition);
		const tab1 = {
			search: {
				field: {},
        scroll: { x: 'max-content' },
        getPageService: getWaitPage,
				beforeSearchData: (data) => {
					let searcherKeyValue = {};
					for (let item of searchCondition) {
						if (typeof data[item.key] !== 'undefined' && item.key !== 'taskDefKey') {
							searcherKeyValue[item.base52] = data[item.key];
						}
					}
					if (searcherKeyValue && Object.keys(searcherKeyValue).length) {
						data.searcherKeyValue = JSON.stringify(searcherKeyValue);
					}
					return data;
				},
				responseCallBack: (response) => {
					return response;
				},
				condition: searchCondition.filter((g) => {
				  if(!allConfigSetInfo.isShowWaitTaskDef){
				    return g.key !== 'taskDefKey';
          }else{
				    return true;
          }
        }),
				nameSpan: { big: 8, small: 9 },
				fileSpan: { big: 4, small: 4 },
			},
			tableRowKey: 'id',
			columns,
		};
		const tab2 = {
			search: {
				field: {},
        scroll: { x: 'max-content' },
        getPageService: getHandledPage,
				responseCallBack: (response) => {
					return response;
				},
				beforeSearchData: (data) => {
					let searcherKeyValue = {};
					for (let item of searchCondition) {
						if (typeof data[item.key] !== 'undefined' && item.key !== 'taskDefKey') {
							searcherKeyValue[item.base52] = data[item.key];
						}
					}
					if (searcherKeyValue && Object.keys(searcherKeyValue).length) {
						data.searcherKeyValue = JSON.stringify(searcherKeyValue);
					}
					return data;
				},
        condition: searchCondition.filter((g) => {
          if(!allConfigSetInfo.isShowHandledTaskDef){
            return g.key !== 'taskDefKey';
          }else{
            return true;
          }
        }),
				nameSpan: { big: 8, small: 9 },
				fileSpan: { big: 4, small: 4 },
			},
			tableRowKey: 'id',
			columns,
		};
		return {
			tab1,
			tab2,
		};
	};

	render() {
		const { workId, dataBaseId, addFields, searchCondition } = this.props;

		return (
			<PageHeaderWrapper title="">
				<ListTab
					tabList={pageSetting.tabList}
					pageButton={this.handleButtonSet({})}
					workId={workId}
					dataBaseId={dataBaseId}
					searchCondition={searchCondition}
					addFields={addFields}
					pageSearch={this.handleSearchSet({})}
				/>
			</PageHeaderWrapper>
		);
	}
}
