import * as constants from './constants';
// import React from 'react';
//锁定state不可修改，导致错误y
import { fromJS } from 'immutable';

const defaultState = fromJS({
	pagination: {
		current: 1,
		pageSize: 10,
		position: ['bottomCenter'],
		showSizeChanger: false
	},
	columnsNum: 0,
	searchValue: '',
	dataSource: [],
	sortField: 'KEYNUM',
	sort: 'DESC',
	columns: [
		{
			title: '锁号',
			dataIndex: 'companyname',
			key: 'companyname',
		},
		{
			title: '产品',
			dataIndex: 'nature',
			key: 'nature',
		},
		{
			title: '办事处',
			dataIndex: 'local',
			key: 'local',
		},
		{
			title: '出库时间',
			dataIndex: 'address',
			key: 'address',
		},
		{
			title: '销售/借出/赠送时间',
			dataIndex: 'ride',
			key: 'ride',
		},
		{
			title: '责任人',
			dataIndex: 'stay',
			key: 'stay',
		},
		{
			title: '状态',
			dataIndex: 'remarks',
			key: 'remarks',
		},
		{
			title: '联系人',
			dataIndex: 'webservice',
			key: 'webservice',
		},
	],
	columnsData: [
		{ columnsName: '锁号', columnsValue: true, dataKey: "companyname" },
		{ columnsName: '产品', columnsValue: true, dataKey: "nature" },
		{ columnsName: '办事处', columnsValue: true, dataKey: "local" },
		{ columnsName: '出库时间', columnsValue: true, dataKey: "address" },
		{ columnsName: '销售/借出/赠送时间', columnsValue: true, dataKey: "ride" },
		{ columnsName: '责任人', columnsValue: true, dataKey: "stay" },
		{ columnsName: '状态', columnsValue: true, dataKey: "remarks" },
		{ columnsName: '联系人', columnsValue: true, dataKey: "webservice" },

	],
});

export default (state = defaultState, action) => {
	let columns = state.get('columns').toJS();
	let columnsData = state.get('columnsData').toJS();
	let countFalse = 0;
	let columnNumData = 0;

	switch (action.type) {
		//显示列
		case constants.TOGGLE_COLUMNS:
			columnsData[action.data].columnsValue = !columnsData[action.data].columnsValue;
			localStorage.setItem('product_lock_store_Column_' + action.data, columnsData[action.data].columnsValue);
			var columnsBak = refreshColumns(columnsData, columns);
			countFalse = 0;
			for (var i = 0; i < columnsData.length; i++) {
				var isAccept = localStorage.getItem("product_lock_store_Column_" + i);
				if (isAccept === "true") {
					countFalse++;
				}
			}
			// console.log(countFalse);
			columnNumData = countFalse;
			return state.merge({
				columnsData: fromJS(columnsData),
				columns: fromJS(columnsBak),
				columnsNum: columnNumData,
			});
		case constants.TOGGLE_COLUMNS_ALL:
			countFalse = 0;
			//console.log(columns.length)
			for (var i = 0; i <= columnsData.length; i++) {
				var isAccept = localStorage.getItem("product_lock_store_Column_" + i);
				if (isAccept === "false") {
					columnsData[i].columnsValue = false;
					countFalse++;
				}
			}
			columnNumData = columnsData.length - countFalse;
			var columnsBak = refreshColumns(columnsData, columns);
			return state.merge({
				columnsNum: columnNumData,
				columnsData: fromJS(columnsData),
			});
		//排序
		case constants.TOGGLE_SORTER:
			var sortField = localStorage.getItem("product_lock_store_sortField");
			var sort = localStorage.getItem("product_lock_store_sort");
			if(sortField!==null&&sort!==null){
				return state.merge({
					sortField: sortField,
					sort: sort
				});
			}else if(sortField!==null){
				return state.set('sortField', sortField);
			}else if(sort!==null){
				return state.set('sort', sort);
			}
		case constants.CHANGE_LOCKSTORE_SORTFIELD:
			// console.log(action.data);
			localStorage.setItem('product_lock_store_sortField', action.data);
			return state.set('sortField', action.data);
		case constants.CHANGE_LOCKSTORE_SORT:
			localStorage.setItem('product_lock_store_sort', action.data);
			return state.set('sort', action.data);
		//表格
		case constants.CHANGE_LOCKSTORE_DATA:
			let pagination = {
				...action.pagination,
				total: action.recordsTotal,
			}
			for (var i = 0; i <= columnsData.length; i++) {
				var isAccept = localStorage.getItem("product_lock_store_Column_" + i);
				if (isAccept === 'false') {
					columnsData[i].columnsValue = false;
				}
			}
			var columnsBak = refreshColumns(columnsData, columns);
			// console.log(pagination);
			// return state.set('dataSource', action.data);
			return state.merge({
				pagination: fromJS(pagination),
				dataSource: fromJS(action.data),
				searchValue: action.searchValue,
				columns: fromJS(columnsBak),
			});
		default:
			return state;
	}
}
function refreshColumns(columnsData) {
	let columnsBak = [];
	//columnsBak.push(columns[0]);
	for (var i = 0; i < columnsData.length; i++) {
		if (columnsData[i].columnsValue) {
			columnsBak.push({
				title: columnsData[i].columnsName,
				dataIndex: columnsData[i].dataKey,
				key: columnsData[i].dataKey,
			});
		}
	}
	return columnsBak;
};