import netapi from './netapi';
import Axios from 'axios';
import JSONbig from 'json-bigint';
import { projectconfig } from "../static/projectconfig"
import store from '../store/index'
import { Store } from 'le5le-store';

//const URL = '/api/';
const URL = process.env.VUE_APP_BASE_URL || '/wisdom-water/';
//******************************用户接口************************************//
export const loginRequest = objData =>
{
    return Axios.post(URL + "sys/login", objData);
};
export const registerUserRequest = objData =>
{
    return Axios.post(URL + "user/register", objData);
};
export const editUserRequest = objData =>
{
    return Axios.post(URL + "user/edit", objData);
};

export const getAllUserRequest = objData =>
{
    return Axios.post(URL + "user/get", objData, {
        headers: {
            'content-type': 'application/json',
            'token': objData.token
        }
    });

};
export const resetUserRequest = objData =>
{

    return Axios.post(URL + "/user/reset", objData, {
        headers: {
            'content-type': 'application/json',
            'token': objData.token
        }
    });
};


//******************************end****************************************//


//******************************测点查询接口************************************//

export const fetcPointList = query =>
{
    return request({
        url: URL + 'point/all',
        method: 'get',
        params: query
    });
};
//******************************end****************************************//


//******************************自定义报警API************************************//
export const alert_rule_fetchTableData = query =>
{
    let data =
        netapi({
            url: URL + 'rule/list',
            method: 'get',
            params: query,
            headers: {
                'content-type': 'application/json',
                'token': localStorage.getItem('token')
            },
            timeout: 3000,
        });
    return data;
};

export const alert_rule_postSave = form =>
{
    return Axios.post(URL + "alertrule2/save", form, {
        headers: {
            'content-type': 'application/json',
            'token': sessionStorage.getItem('token')
        }
    })
};

//******************************end*******************************************//


//#region  监视页面相关
export const monitor = {

    /*** 加载头文件导航中的监视页面的结构Json文件 */
    async getJsonFile()
    {
        await Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.watcher,
                {
                    params:
                    {
                        keys: '/'
                    },
                    headers: {
                        'content-type': 'application/json',
                        'token': localStorage.getItem('token')
                    }
                }
            )
            .then(res =>
            {
                if (res.data && res.data.success && res.data.success == true && res.data.data && res.data.data.pagejson) {

                    var x = JSON.parse(res.data.data.pagejson);
                    store.dispatch('setPathSummary', x)
                }
            })
            .catch(error =>
            {
                console.log("getJsonFile 异常:" + error);
            });

    },
    async getJsonFileBy(path, callBack)
    {
        await Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.watcher,
                {
                    params:
                    {
                        keys: path
                    },
                    headers: {
                        'content-type': 'application/json',
                        'token': localStorage.getItem('token')
                    }
                }
            )
            .then(res => callBack(res))
            .catch(error =>
            {
                console.log("getJsonFileBy 异常:" + error);
            });

    },
    async setJsonFile(monitorJson, backCall)
    {
        await Axios.post
            (
                projectconfig.restful.host + projectconfig.restful.postjson,
                monitorJson,
                {
                    headers: {
                        'content-type': 'application/json',
                        'token': localStorage.getItem('token')
                    }
                }
            ).then(res =>
            {
                if (backCall) {
                    backCall(res);
                }
                else {
                    //console.log(res.data)
                }
            }).catch(error =>
            {
                console.log("setJsonFile 异常:" + error);
                databack = error;
            });
    },
	/**
	 * 根据标签点名查询对应的数据
	 * @param {*} tagNameArray []
	 */
    getTagValues(tagNameArray)
    {
        Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.searchTagValues,
                {
                    params:
                    {
                        keys: tagNameArray.join(',')
                    }
                }
            ).then(res =>
            {
                Store.set('wxwData', res.data);
            }).catch(error =>
            {
                //console.log("getTagValues 异常:" + error);
            });
    }

}
//#endregion


//#region  报警相关
export const alert = {
    async searchAlert(searchCondition,callback)
    {
        await Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.alert.searchActiveAlert,
                {
                    params:
                    {
                        pagenum: searchCondition.pagenum,
                        pagesize: searchCondition.pagesize,
                        name:searchCondition.name,
                        startTime: searchCondition.startTime,
                        endTime: searchCondition.endTime
                    },
                    headers: {
                        'content-type': 'application/json',
                        'token': localStorage.getItem('token')
                    }
                }
            )
            .then(res =>callback(res))
            // {
            //     console.log(res.data);
            //     if (res.data && res.data.success && res.data.success == true && res.data.data) {

            //         if (res.data.data.length > 0) {
            //             callback(res.data.data);
            //         }
            //     }
            // })
            .catch(error =>
            {
                console.log("getAllActiveAlert 异常:" + error);
            });
    },
    async getAllActiveAlert(searchCondition,callback)
    {
        await Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.alert.allActiveAlert,
                {
                    params:
                    {
                        name: searchCondition.name,
                        rank: searchCondition.rank,
                        startTime: searchCondition.startTime,
                        endTime: searchCondition.endTime
                    },
                    headers: {
                        'content-type': 'application/json',
                        'token': localStorage.getItem('token')
                    }
                }
            )
            .then(res =>callback(res))
            // {
            //     console.log(res.data);
            //     if (res.data && res.data.success && res.data.success == true && res.data.data) {

            //         if (res.data.data.length > 0) {
            //             callback(res.data.data);
            //         }
            //     }
            // })
            .catch(error =>
            {
                console.log("getAllActiveAlert 异常:" + error);
            });
    },
}
//#endregion
export const nodes = {
    pull(pagination, callBack)
    {
        Axios.get
            (
                projectconfig.restful.host + projectconfig.restful.nodelist,
                {
                    params:
                    {
                        pagenum: pagination.pagenum,
                        pagesize: pagination.pagesize,
                        name: pagination.name,
                        pointtype: pagination.pointtype,
                        part: pagination.part
                    }
                }
            )
            .
            then(res =>
            {
                callBack(res);
            })
            .
            catch(error =>
            {
                console.log("pullNodes 异常:");
                console.log(error);
            });
    }
}