/**
 * Created by Administrator on 2021/4/5.
 */
define(['jquery', "template", "Tools"], function ($, template, Tools) {
    //数据存储
    var module = {
        data: {
            //服务器地址
            serverApi:'',
            //焦点图数据
            focusList: [],
            communicateList: [],
            specialPublicityTypeOptions: [],
        },
    };
    var tools = new Tools();

    module.init = function (page) {
        //新闻列表
        tools.doGet(specialPublicityList, {
            pageNum:module.data.pageNum12,
            translate_dict:"1",
            pageSize:10,
            bookId: JSON.parse(tools.getCookie('user')).bookId,
            deptId: JSON.parse(tools.getCookie('user')).deptId,
        }, module.newsList , true);
        tools.doGet(webList, {}, module.webList , true);
        tools.doGet(webDeptType+"/special_publicity_type", {}, module.deptType,true);
    };

    //标的物类型
    module.deptType = function (data) {
        if (data.code == 200) {
            var content = data.data ;
            module.data.specialPublicityTypeOptions = content;
            //新闻详情
            tools.doGet(specialPublicityDetail, {
                id:getQueryVariable('id'),
                bookId: JSON.parse(tools.getCookie('user')).bookId,
                deptId: JSON.parse(tools.getCookie('user')).deptId
            }, module.newsDetail , true);
        }
    }

    //获取地址栏参数
    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    module.webList = function (data) {
        if (data.code == 200) {
            var content = data.data;
            module.data.webList = content;
            var webData = template('webData', module.data);
            $("#webContent").html(webData);
            $('#ewm').attr('src', 'static/images/'+content.top.bz);
        }
    }

    module.newsDetail = function (data) {
        if (data.code == 200) {
            var content = data.data;
            content.specialPublicityType = module.selectDictLabel(module.data.specialPublicityTypeOptions,content.specialPublicityType);
            $('#openName')[0].innerHTML = content.openName;
            $('#openNy')[0].innerHTML = '公示年月：'+content.openAt;
            $('#openPic')[0].innerHTML = '公示图片：<br/>';
            if (content.openPic){
                let arr = content.openPic.split(',');
                for (let i = 0;i < arr.length; i++){
                    $('#openPic')[0].innerHTML += '<img onclick="openDialog(\'/api'+arr[i]+'\')" src="/api'+arr[i]+'" style="width: 200px;height: 150px;margin-right: 10px;">';
                }
            }
            $('#qrcode')[0].innerHTML = '二维码图片：<br/>';
            if (content.qrcode){
                let arr2 = content.qrcode.split(',');
                for (let i = 0;i < arr2.length; i++){
                    $('#qrcode')[0].innerHTML += '<img onclick="openDialog(\'/api'+arr2[i]+'\')" src="/api'+arr2[i]+'" style="width: 200px;height: 150px;margin-right: 10px;">';
                }
            }
            $('#specialPublicityType')[0].innerHTML = '公示类型：'+(content.specialPublicityType || '');
            $('#differCount')[0].innerHTML = '有异议数：'+(content.differCount || '');
            $('#agreeCount')[0].innerHTML = '无异议数：'+(content.agreeCount || '');
            $('#remark')[0].innerHTML = '备注：'+(content.remark || '');

        }
    }

    openDialog = function (src){
        $("#dialogOp").css('display','block');
        $('#dialogImg').attr('src', src);
    }

    closeDia = function (){
        $("#dialogOp").css('display','none');
        $('#dialogImg').attr('src', '');
    }

    module.newsList = function (data) {
        if (data.code == 200) {
            var content = data.rows;
            module.data.newList = content;
            var newListData = template('newListData', module.data);
            $("#newListContent").html(newListData);
        }
    }

    module.selectDictLabel = function (datas, value) {
        var actions = [];
        Object.keys(datas).some((key) => {
            if (datas[key].dictValue == ('' + value)) {
                actions.push(datas[key].dictLabel);
                return true;
            }
        })
        return actions.join('');
    }

    return module;
});
