import React from 'react';
import * as constants from './constants';
import { fromJS } from 'immutable';

const defaultState = fromJS({
	companyList: [],
	pagination: {
		current: 1,
		pageSize: 10,
		showSizeChanger: false
	},
	loading: false,
	sortField: 'CREATETIME',
	sort: 'DESC',
	columns: [
		{
			title: '客户名称',
			dataIndex: 'companyname',
			key: 'companyname',
		},
		{
			title: '地区',
			dataIndex: 'local',
			key: 'local',
		},
		{
			title: '标签',
			dataIndex: 'nature',
			key: 'nature',
		},
		{
			title: '单位性质',
			dataIndex: 'nature',
			key: 'nature',
		},
		{
			title: '公司地址',
			dataIndex: 'address',
			key: 'address',
		},
		{
			title: '公司传真',
			dataIndex: 'fax',
			key: 'fax',
		},
		{
			title: '网址',
			dataIndex: 'webservice',
			key: 'webservice',
		},
		{
			title: '乘车路线',
			dataIndex: 'ride',
			key: 'ride',
		},
		{
			title: '地标建筑',
			dataIndex: 'landmarks',
			key: 'landmarks',
		},
		{
			title: '参考住宿',
			dataIndex: 'stay',
			key: 'stay',
		},
		{
			title: '备注',
			dataIndex: 'remarks',
			key: 'remarks',
		},
	],
	columnsVisible: [
		// { columnsName: '客户名称', columnsValue: true, dataKey: "companyname" },
		{ columnsName: '地区', columnsValue: true, dataKey: "local" },
		{ columnsName: '标签', columnsValue: true, dataKey: "nature" },
		{ columnsName: '单位性质', columnsValue: true, dataKey: "nature" },
		{ columnsName: '公司地址', columnsValue: true, dataKey: "address" },
		{ columnsName: '公司传真', columnsValue: true, dataKey: "fax" },
		{ columnsName: '网址', columnsValue: true, dataKey: "webservice" },
		{ columnsName: '乘车路线', columnsValue: true, dataKey: "ride" },
		{ columnsName: '地标建筑', columnsValue: true, dataKey: "landmarks" },
		{ columnsName: '参考住宿', columnsValue: true, dataKey: "stay" },
		{ columnsName: '备注', columnsValue: true, dataKey: "remarks" },
	],
	companyPopupsDetail: {
		companyDetail: {},

	},
});



export default (state = defaultState, action) => {
	let columns = state.get('columns').toJS();
	let columnsVisible = state.get('columnsVisible').toJS();

	switch (action.type) {
		//显示隐藏列
		case constants.TOGGLE_COLUMNS:
			columnsVisible[action.data].columnsValue = !columnsVisible[action.data].columnsValue;
			//存储
			localStorage.setItem('contact_company_Column_' + action.data, columnsVisible[action.data].columnsValue);
			var columnsBak = refreshColumns(columnsVisible, columns);

			return state.merge({
				columnsVisible: fromJS(columnsVisible),
				columns: fromJS(columnsBak)
			});
		// case constants.TOGGLE_COLUMNS_ALL:
		// 	for (var i = 0,ci=1; i <= columnsVisible.length; i++,ci++) {
		// 		var isAccept = localStorage.getItem("contact_company_Column_" + i);
		// 		if (isAccept === 'false') {
		// 			columnsVisible[i].columnsValue = false;
		// 		}
		// 	}
		// 	var columnsBak=refreshColumns(columnsVisible,columns);

		// 	return state.merge({
		// 		columnsVisible: fromJS(columnsVisible),
		// 		columns: fromJS(columnsBak)
		// 	});
		//设置列表排序
		case constants.CHANGE_COMPANY_SORTFIELD:
			return state.set('sortField', action.data);
		case constants.CHANGE_COMPANY_SORT:
			return state.set('sort', action.data);
		//读取锁定-获得客户data
		case constants.CHANGE_COMPANY_LOADING:
			return state.set('loading', action.data);
		case constants.CHANGE_COMPANY_DATA:
			let columnsDetail = {
				...columns[0],
				render: (text, record) => <a href="#detail" data-toggle="modal" data-target="#detail" onClick={() => action.props.handleCompanyPopups(record.key)} >{text}</a>,
			}
			columns[0] = columnsDetail;
			let pagination = {
				...action.pagination,
				total: action.data.recordsTotal,
			}

			for (var i = 0, ci = 1; i <= columnsVisible.length; i++, ci++) {
				var isAccept = localStorage.getItem("contact_company_Column_" + i);
				if (isAccept === 'false') {
					columnsVisible[i].columnsValue = false;
				}
			}
			var columnsBak = refreshColumns(columnsVisible, columns);
			return state.merge({
				pagination: fromJS(pagination),
				companyList: fromJS(action.data.data),
				loading: false,
				columns: fromJS(columnsBak)
			});
		default:
			return state;
	}
}


function refreshColumns(columnsVisible, columns) {
	let columnsBak = [];
	columnsBak.push(columns[0]);
	for (var i = 0; i < columnsVisible.length; i++) {
		if (columnsVisible[i].columnsValue) {
			columnsBak.push({
				title: columnsVisible[i].columnsName,
				dataIndex: columnsVisible[i].dataKey,
				key: columnsVisible[i].dataKey,
			});
		}
	}
	return columnsBak;
};