import * as constants from './constants';
import { fromJS } from 'immutable';
import * as config from '../../../common/config.js';
import axios from '../../../common/axios_auth.js';

export const toggleColumns = (index) => {
    return (dispatch) => {
        dispatch({
            type: constants.TOGGLE_COLUMNS,
            data: index,
        });
    }
};
export const toggleColumnsAll = () => {
    return (dispatch) => {
        dispatch({
            type: constants.TOGGLE_COLUMNS_ALL,
        });
    }
};

//排序
export const toggleSorter = () => {
    return (dispatch) => {
        dispatch({
            type: constants.TOGGLE_SORTER,
        });
    }
}
export const changeSortField = (sortField) => {
    return (dispatch) => {
        dispatch({
            type: constants.CHANGE_LOCKSTORE_SORTFIELD,
            data: sortField,
        });
    }
}
export const changeSort = (sort) => {
    return (dispatch) => {
        dispatch({
            type: constants.CHANGE_LOCKSTORE_SORT,
            data: sort,
        });
    }
}

export const getLockstoreList = (pagination,support,searchValue) =>{
    return(dispatch) =>{
        // console.log('searchValue:'+searchValue);
        // console.log('support:'+searchValue);
        // console.log('typeof:'+typeof searchValue);
        let sortField = localStorage.getItem("product_lock_store_sortField");
        if(sortField===null){
            sortField = support.sortField;
        }
        let sort = localStorage.getItem("product_lock_store_sort");
        if(sort===null){
            sort = support.sort;
        }
        if(typeof searchValue === 'undefined'){
            searchValue = support.searchValue;
        }
        // console.log();
        // console.log(config.CLD2API + '/contact/company')
        axios.get(config.CLD2API + '/contact/company', {
            params: {
                current: pagination.current,
                pageSize: pagination.pageSize,
                sortField:sortField,
                sort:sort,
                searchValue:searchValue,
            },
        }).then((res) => {
            const data = res.data;
            if (data.code === 200) {
                // console.log(searchValue);
                dispatch({
                    type: constants.CHANGE_LOCKSTORE_DATA,
                    data: fromJS(data.data),
                    pagination: pagination,
                    recordsTotal:data.recordsTotal,
                    searchValue:searchValue,
                });
            } else {
                console.log(data);
            }
        }).catch((e) => {
            console.log(e);
            console.log('error');
        })
    }
};


    // current: pagination.current,    //当前页
    // pageSize: pagination.pageSize,  //每页多少条
    // sortField: sortField,           //排序字段
    // sort: sort,                     //按降序 升级
    // searchValue:searchValue         //检索关键字