var jiGouId = 271;
function getUrlParam(a) {
    var t = new RegExp("(^|&)" + a + "=([^&]*)(&|$)");
    var e = decodeURIComponent(window.location.search).substr(1).match(t);
    if (e != null) return unescape(e[2]);
    return null;
}
var section = getUrlParam("section");
var apiActivityList = "/v1/osps/app/activity/getWebActivityList";
var keywordApiActivityList = "/v1/osps/app/activity";
var queryCity = getUrlParam("city");
var queryDist = getUrlParam("dist");
var code = getUrlParam("code");
var ajaxType = "GET";
var ajaxDataP = {
    page: 1,
    pagesize: 12,
    service_object: 0,
    service_type: 0,
    state: 0,
    area_code: code || 0,
    keyword: "",
    start: getTimeByDay(-60) + " 00:00:00",
    end: getTimeByDay(30) + " 00:00:00",
};

if (section) ajaxDataP.section = section;
var changePageCount = false;
function goAjax(a, t, e, i) {
    $("#loading").show();
    $.ajax({
        type: a,
        url: t,
        data: e,
        success: function success(a) {
            var _$t = a.data;
            i && i(_$t);
            $("#loading").hide();
        },
        error: function error(a) {
            console.log(a);
        },
    });
}
$(".menu-filter").on("click", ".menu-filter-list-item", function () {
    changePageCount = true;
    $(this).addClass("cur").siblings().removeClass("cur");
    if ($(this).data("type") == "service_object") ajaxDataP.service_object = $(this).data("value");
    if ($(this).data("type") == "state") ajaxDataP.state = $(this).data("value");
    ajaxDataP.page = 1;
    getMenuPractice();
    return false;
});
function getMenuPractice() {
    $("#loading").show();
    // 有关键词的调这个接口
    var func;
    var newobj = {
        page: ajaxDataP.page,
        page_size: ajaxDataP.pagesize,
        service_object: ajaxDataP.service_object,
        service_type: ajaxDataP.service_type,
        attend_state: ajaxDataP.state,
        orgid: ajaxDataP.area_code,
        start_at: ajaxDataP.start,
        end_at: ajaxDataP.end,
        keyword: ajaxDataP.keyword,
    };
    if (ajaxDataP.keyword.trim()) {
        func = getSearchActivity;
    } else {
        func = getActivityList;
    }
    func(newobj).then(function (res) {
        $("#loading").hide();
        renderMenuPractice(res.data);
    });
}
function renderMenuPractice(a) {
    $(".menu-practice").html(renderMenuPracticeDOM(a));
}
function renderMenuPracticeDOM(a) {
    var i = "";
    if (a.list.length) {
        i = getActivityListDom(a.list);
        if (changePageCount) {
            // 显示共多少条
            $(".pagination-total")
                .text("共" + a.total + "条")
                .show();
            pageCount = Math.ceil(Number(a.total) / 12);
            initPagination(pageCount);
        }
    } else {
        $(".pagination-total").hide();
        initPagination(0);
        i = '<div class="empty-box"><img src="/public/city/default/img/kong.png" alt=""></div>';
    }
    return i;
}
function initPagination(a) {
    var maxCount = Math.floor(10000 / 12);
    $(".pagination").pagination({
        pageCount: a > maxCount ? maxCount : a,
        jump: false,
        coping: true,
        homePage: "首页",
        endPage: "末页",
        prevContent: "上页",
        nextContent: "下页",
        callback: function callback(a) {
            ajaxDataP.page = a.getCurrent();
            changePageCount = false;
            getMenuPractice();
            // 如需查询更多数据，请输入更精确的搜索条件
            if (ajaxDataP.page == maxCount) {
                cocoMessage.info(2500, "如需查询更多数据，请输入更精确的搜索条件");
            }
        },
    });
}
function searchPractice() {
    var a = $.trim($("#practice-text").val());
    if (a) {
        ajaxDataP.page = 1;
        ajaxDataP.keyword = a;
        getMenuPractice();
    } else {
        cocoMessage.info(2500, "请输入搜索关键词！");
    }
}
$("#btn-search-practice").on("click", function () {
    changePageCount = true;
    searchPractice();
    return false;
});
$(".btn-clear").on("click", function () {
    changePageCount = true;
    var a = $("#practice-text").val();
    if (a) {
        $("#practice-text").val("");
        ajaxDataP.page = 1;
        ajaxDataP.keyword = "";
        getMenuPractice();
    }
    return false;
});
function n(e, t) {
    return e < t;
}
function initDateLimit() {
    var throttling = true;
    $.datetimepicker.setLocale("zh");
    $("#datetimepickerStart").datetimepicker({
        step: 1,
        validateOnBlur: false,
        value: getTimeByDay(-60),
        onChangeDateTime: function onChangeDateTime(dateText, inst) {
            ajaxDataP.start = inst.val() + ":00";
        },
    });
    $("#datetimepickerEnd").datetimepicker({
        step: 1,
        validateOnBlur: false,
        value: getTimeByDay(30),
        onChangeDateTime: function onChangeDateTime(dateText, inst) {
            ajaxDataP.end = inst.val() + ":00";
        },
    });
    $(".date-search-btn").click(function () {
        if (throttling) {
            throttling = false;
            var s = new Date(ajaxDataP.start).getTime() || 0;
            var e = new Date(ajaxDataP.end).getTime() || 0;
            if (s == 0 && e == 0) {
                cocoMessage.info(2500, "请选择活动时间");
                return;
            }
            if ((s == 0 && e != 0) || (s != 0 && e == 0)) {
                changePageCount = true;
                ajaxDataP.page = 1;
                getMenuPractice();
                return;
            }
            if (n(s, e)) {
                changePageCount = true;
                ajaxDataP.page = 1;
                getMenuPractice();
            } else {
                cocoMessage.info(2500, "开始时间不能晚于结束时间");
            }
        }
        setTimeout(function () {
            throttling = true;
        }, 2000);
    });
}
$(function () {
    var c = function c(param) {
        var a = param.res,
            t = param.templateId,
            e = param.nodeId,
            i = param.index;
        var r;
        if (t == "fuwu") {
            a.data.service_object.unshift({
                value: "全部",
                key: 0,
            });
            a.data.service_type.unshift({
                value: "全部",
                key: 0,
            });
            r = {
                list: a.data,
                i: i,
            };
        } else {
            if (t == "area") {
                a.data.unshift({
                    id: a.data[0].parent_id,
                    name: "全部",
                });
                ajaxDataP.area_code = a.data[0].id;
                getMenuPractice();
            }
            if (t == "city") {
                a.data.unshift({
                    id: 0,
                    name: "全部",
                });
                getMenuPractice();
            }
            r = {
                list: a.data,
                i: i,
                total: a.data.total ? a.data.total : 0,
            };
        }
        var o = template(t, r);
        document.getElementById(e).innerHTML = o;
        if (t == "fuwu") {
            initDateLimit();
        }
    };
    var n = function n() {
        changePageCount = true;
        // 获取服务区域
        getProvinceCity({
            parent_id: area_code,
        }).then(function (res) {
            c({
                res: res,
                templateId: "area",
                nodeId: "area-contain",
                index: 0,
            });
        });
        // 获取服务对象
        getOptionList({
            type: "service_object,service_type",
        }).then(function (res) {
            c({
                res: res,
                templateId: "fuwu",
                nodeId: "fuwu-contain",
                index: 0,
            });
        });
    };
    var t = $(".screen-label-tabs");
    t.on("click", ".tab-item", function () {
        changePageCount = true;
        var a = $(this).attr("data-value");
        $(this).addClass("tab-item-active");
        $(this).siblings().removeClass("tab-item-active");
        ajaxDataP.page = 1;
        queryCity = null;
        queryDist = null;
        if (a == 0 || a == provincialStraight) {
            ajaxDataP.area_code = a;
            $(".area-wrap-p").hide();
            getMenuPractice();
        } else {
            $(".area-wrap-p").show();
            getProvinceCity({
                parent_id: a,
            }).then(function (res) {
                c({
                    res: res,
                    templateId: "area",
                    nodeId: "area-contain",
                    index: 0,
                });
            });
        }
    });
    var e = $(".area-wrap");
    e.on("click", ".tab-item-area", function () {
        $(this).addClass("tab-item-active");
        $(this).siblings().removeClass("tab-item-active");
        changePageCount = true;
        queryCity = null;
        queryDist = null;
        ajaxDataP.area_code = $(this).data("value");
        ajaxDataP.page = 1;
        getMenuPractice();
        return false;
    });
    var i = $("#fuwu-contain");
    i.on("click", ".tab-item", function () {
        $(this).addClass("tab-item-active");
        $(this).siblings().removeClass("tab-item-active");
        changePageCount = true;
        if ($(this).data("type") == "service_object") ajaxDataP.service_object = $(this).data("value");
        if ($(this).data("type") == "service_type") ajaxDataP.service_type = $(this).data("value");
        if ($(this).data("type") == "state") ajaxDataP.state = $(this).data("value");
        ajaxDataP.page = 1;
        getMenuPractice();
        return false;
    });
    n();
});
