import * as constants from './constants';
import { fromJS } from 'immutable';
import * as config from '../../common/config.js';
import axios from '../../common/axios_auth.js';
import md5 from 'js-md5';
import qs from 'qs';
import cookie from 'react-cookies'

const salt = 'cld2cp89_+!';

//loading
export const changeLoading = () => {
    return (dispatch) => {
        dispatch({
            type: constants.CHANGE_LOADING,
            data: true,
        });
    }
}

export const login = (parameter) => {
    return (dispatch) => {
        let mdpw = md5(md5(parameter.password) + salt);
        let data = {
            'username': parameter.username,
            'password': mdpw,
        };
        
        //let csrftoken = cookie.load('csrfToken');
        axios.post(config.CLD2API + '/login', qs.stringify(data), {
            headers: {
                //'Content-Type': 'application/x-www-form-urlencoded',
                //'x-csrf-token': csrftoken,
            },
        }).then((res) => {
            const data = res.data;
            if (data.code === 200) {
                window.location.href = '/contact/company';
            } else{
                alert('登录不正确');
            }
        }).catch((e) => {
            console.log(e);
        })
    }
}
