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

//显示隐藏列
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_COMPANY_SORTFIELD,
            data: sortField,
        });
    }
}
export const changeSort = (sort) => {
    return (dispatch) => {
        dispatch({
            type: constants.CHANGE_COMPANY_SORT,
            data: sort,
        });
    }
}
//loading
export const changeLoading = () => {
    return (dispatch) => {
        dispatch({
            type: constants.CHANGE_COMPANY_LOADING,
            data: true,
        });
    }
}
//获得数据
export const getCompanyLIst = (pagination, props,searchValue) => {
    return (dispatch) => {
        
        // changeList(pagination,props,dispatch);
        let sortField = localStorage.getItem("contact_company_sortField");
        if(sortField===null){
            sortField = props.sortField;
        }
        let sort = localStorage.getItem("contact_company_sort");
        if(sort===null){
            sort = props.sort;
        }
        //检索
        if(typeof searchValue==='undefined'){
            searchValue=props.searchValue;
        }

        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) {
                //let d2=JSON.parse(data.list);
                //dispatch(changeList(JSON.parse(d2)));
                dispatch({
                    type: constants.CHANGE_COMPANY_DATA,
                    data: data,
                    pagination: pagination,
                    props: props,
                    searchValue:searchValue
                });
            } else {
                console.log(data);
            }
        }).catch((e) => {
            console.log(e);
            console.log('error');
        })

    }
};


// const changeList = (pagination,props,dispatch) => {
//     const { sortField, sort } = props;
//     axios.get(config.CLD2API + '/contact/company', {
//         params: {
//             current: pagination.current,
//             pageSize: pagination.pageSize,
//             sortField: sortField,
//             sort: sort,
//         },
//     }).then((res) => {
//         const data = res.data;
//         if (data.code === 200) {
//             dispatch({
//                 type: constants.CHANGE_COMPANY_DATA,
//                 data: data,
//                 pagination: pagination,
//                 props: props
//             });
//         } else {
//             console.log(data);
//         }
//     }).catch((e) => {
//         console.log(e);
//         console.log('error');
//     })
// }






