import request from '@/utils/request'
import Cookie from 'js-cookie'
import { ElMessage } from 'element-plus'

export default {
    install(app) {
        app.config.globalProperties.CreateData = function (type, name, data, pageSize, pageList) {
            var param_Str = ''
            var param_array = []
            if (type === '7') {
                data.map(v => {
                    pageSize.map(h => {
                        const val = h.toString()
                        if (!v[val]) {
                            this.$set(v, val, '')
                        }
                    })
                })
                param_Str = data
            } else {
                if (data !== undefined) {
                    for (let i = 0; i < data.length; i++) {
                        param_array.push({
                            name: data[i][0],
                            value: data[i][1],
                            type: data[i][2],
                            output: data[i][3]
                        })
                    }
                    param_Str = JSON.stringify(param_array)
                }
            }
            var obj = []
            obj[0] = {}
            obj[0].type = type
            obj[0].name = name
            obj[0].param = param_Str
            obj[0].pageSize = pageSize
            obj[0].pageList = pageList
            obj[0].UserID = Cookie.get('username')
            obj[0].ModularID = ''//this.$router.currentRoute.path
            console.log(obj[0], 'obj[0]')
            var numn = JSON.stringify(obj[0])
            return numn
        }
        app.config.globalProperties.ExecDatabase = function (data) {
            return request({
                url: '',
                method: 'post',
                data: data
            })
        }

        app.config.globalProperties.ExecDatabaseByParam = function (type, name, data, pageSize, pageList) {
            var param_Str = ''
            var param_array = []
            if (type === '7') {
                data.map(v => {
                    pageSize.map(h => {
                        const val = h.toString()
                        if (!v[val]) {
                            this.$set(v, val, '')
                        }
                    })
                })
                param_Str = data
            } else {
                if (data !== undefined) {
                    for (let i = 0; i < data.length; i++) {
                        param_array.push({
                            name: data[i][0],
                            value: data[i][1],
                            type: data[i][2],
                            output: data[i][3]
                        })
                    }
                    param_Str = JSON.stringify(param_array)
                }
            }
            var obj = []
            obj[0] = {}
            obj[0].type = type
            obj[0].name = name
            obj[0].param = param_Str
            obj[0].pageSize = pageSize
            obj[0].pageList = pageList
            obj[0].UserID = Cookie.get('username')
            obj[0].ModularID = ''//this.$router.currentRoute.path
            console.log(obj[0], 'obj[0]')
            var numn = JSON.stringify(obj[0])
            return request({
                url: '',
                method: 'post',
                data: numn
            })
        }

        // app.config.globalProperties.ExecWritePLC = function (opName, tagTypeCodeID, value) {
        //     return request({
        //         url: window.g.API_MES_URL + '/api/imes/Web_WritePLC',
        //         method: 'post',
        //         data: {
        //             tagTypeCodeID: tagTypeCodeID,
        //             OPName: opName,
        //             tagValue: value,
        //         }
        //     }).catch(function (error) {
        //         // 如果接口调用失败，弹出错误弹窗
        //         ElMessage.error('写入PLC数据接口失败: ' + error.message)
        //     });
        // }
        // app.config.globalProperties.ExecReadPLC = function (opName, tagTypeCodeID) {
        //     return request({
        //         url: window.g.API_MES_URL + '/api/imes/Web_ReadPLC',
        //         method: 'post',
        //         data: {
        //             tagTypeCodeID: tagTypeCodeID,
        //             OPName: opName,
        //         }
        //     }).catch(function (error) {
        //         // 如果接口调用失败，弹出错误弹窗
        //         ElMessage.error('读取PLC数据接口失败: ' + error.message)
        //     });
        // }
        // app.config.globalProperties.ExecIMESAPI = function (apiName, param) {
        //     return request({
        //         url: window.g.API_MES_URL + '/api/imes/' + apiName,
        //         method: 'post',
        //         data: param
        //     }).catch(function (error) {
        //         // 如果接口调用失败，弹出错误弹窗
        //         ElMessage.error('调用MES' + apiName + '接口失败: ' + error.message)
        //     });
        // }
        app.config.globalProperties.ExecHttpRequest = function (url, method, data) {
            return request({
                url: url,
                method: method || 'post',
                data: data
            }).catch(function (error) {
                // 如果接口调用失败，弹出错误弹窗
                ElMessage.error('接口调用失败: ' + error.message)
                throw error;
            });
        }
    }
}
