import fetch from 'dva/fetch';
import React from 'react';
import ReactDOM from 'react-dom';
import {
	Select,
	Button,
	Modal,
	Input,
	Transfer,
	Row,
	Col,
	Form,
	message,
	notification,
} from 'antd';
import { connect } from 'dva';
import { getToken } from '../utils/token';
import config from '@/webPublic/one_stop_public/config';

import QueryItem from './QueryItem';
import OrderItem from './OrderItem';

import FormdataWrapper from '../utils/object-to-formdata-custom';
import ButtonDiy from './ButtonDiy/ButtonDiy';
const Option = Select.Option;
var keyX = 1;

function swapArray(arr, index1, index2) {
	arr[index1] = arr.splice(index2, 1, arr[index1])[0];
	return arr;
}

/**
 *
 * 2019/02/21  修改导出方式为fetch
 * 增加 props.fileName 设置导出文件名称
 * mustQuerys 设置搜索条件 demo :
 *  mustQuerys={[{
                hql: 'bean.adminStatus',
                x: '=',
                v: "pass",
                c: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
                notes: 'com.zysoft.app.zydxl.entity.base.BaseImportantWatch$Status',
              }]}
 */

const FormItem = Form.Item;
@connect(({ DataObj, loading }) => ({
	DataObj,
	loading: loading.models.DataObj,
}))
@Form.create()
export default class ExportCurrentInfo extends React.Component {
	constructor(props) {
		super(props);
		this.state = {
			confirmLoading: false,
		};
	}

	exportData = () => {
		let divElement = document.getElementById('downloadDiv');
		let downloadUrl = config.httpServer + '/DataObjApi/exportCurrent?';
		const token = getToken() != null && getToken() != 'null' ? getToken() : '0000';
		downloadUrl = `${downloadUrl}token=${token}`;
		let param = {
			dataObjId: this.props.objId,
			query: this.props.query,
			custom: this.props.custom,
			sql: this.props.sql,
			index: this.props.index,
		};
		this.downloadFile(downloadUrl, param);
	};

	downloadFile(url, params) {
		this.setState({ confirmLoading: true });

		fetch(url, {
			method: 'POST',
			body: FormdataWrapper(params),
		})
			.then((res) => {
				if (res.status != '200') {
					return res.json();
				} else {
					return res.blob();
				}
			})
			.then((data) => {
				if (data instanceof Blob) {
					let a = document.createElement('a');
					let url = window.URL.createObjectURL(data);
					let filename =
						(this.props.fileName ? this.props.fileName : '导出文件.') + (this.props.ext || 'xlsx');
					a.href = url;
					a.download = filename;
					a.click();
					window.URL.revokeObjectURL(url);
					a = null;
				} else {
					notification.error({
						message: `文件导出错误`,
						description: data.errMsg,
					});
				}
			})
			.catch((err) => {
				notification.error({
					message: `网络请求超时`,
				});
			})
			.finally(() => {
				this.setState({ confirmLoading: false });
			});
	}

	render() {
		const btn = {
			name: '导出',
			type: 'default',
			className: 'defaultBlue',
			...(this.props.btn ? this.props.btn : {}),
		};
		return (
			<span>
				<ButtonDiy {...btn} loading={this.state.confirmLoading} handleClick={this.exportData} />
				<div id="downloadDiv" style={{ display: 'none' }} />
			</span>
		);
	}
}
