Index.js 4.83 KB
/*
 *@time: 2016-11-09 11:09:56
 *@author: LiQZ
 * App.template
 */
var $ = require('jquery'), common = require('../../../common/common');

var artTemplate = common.artTemplate;

// 下拉框
new common.dropDown({
    el: "#status"
});
new common.dropDown({
    el: "#time_status"
});

$.timepicker.datetimeRange($('#start_time'), $('#end_time'));

var tableGird = new common.grid({

    el: "#table-box",
    parms: function() {
      return {
            matchWordName: $("#matchWordName").val(),
            status: $("#status").val(),
            timeStatus: $("#time_status").val(),
            startTime: timeToSeconds($("#start_time").val()),
            endTime: timeToSeconds($("#end_time").val()),
        }  
    },
    columns: [
    { display: "ID", name: "id" },
    { display: "匹配词", name: "matchWordName"},
    { display: "PC链接", name: "matchPcUrl"},
    { display: "移动端链接", name:"matchAppUrl"},
    { display: "开启状态", name:"status", render: function(item) {
        if (item.status == 0) {return "开启"; } else { return "关闭"; }
    }},
    { display: "时间状态", name:"status", render: function(item) {
        var now = Date.parse(new Date())/1000;
        // 未开始
        if (item.startTime > now) {
            return "未开始";
        }
        // 进行中
        if (item.startTime <= now && item.endTime >= now) {
            return "进行中";
        }
        // 已结束
        if (item.endTime < now) {
            return "已结束";
        }
        return "时间设置错误";
    }},
    { display: "开始时间", name:"startTime", render: function(item) {
            return common.util.__secondsFormat(item.startTime);
    }},
    { display: "结束时间", name:"endTime", render: function(item) {
        return common.util.__secondsFormat(item.endTime);
    }},
    { display: "操作", render: function(items) {
            var HtmArr = [];
            HtmArr.push('<a href="javascript:void(0);" data-index="' + items.__index + '" class="btn btn-primary btn-xs btn_edit">编辑</a>');
            if (items.status == 0) {
                HtmArr.push('<a href="javascript:;" data-id="' + items.id + '" class="btn btn-danger btn-xs close-btn">关闭</a>');
            } else {
                HtmArr.push('<a href="javascript:;" data-id="' + items.id + '" class="btn btn-success btn-xs open-btn">开启</a>');
            }
            return HtmArr.join('');
        }
    }]
});

tableGird.init('/search/matchWords/list');

$(document).on("click", "#filter-btn", function () {
    tableGird.reload();
});

//开启
$('#table-box').on('click', '.open-btn', function() {
    var param = {
        id: $(this).data('id'),
        status: '0'
    }
    common.util.__ajax({
        url: '/search/matchWords/triggerStatus',
        data: param

    }, function(res) {
        tableGird.reload();
    });
});

//关闭
$('#table-box').on('click', '.close-btn', function() {
    var param = {
        id: $(this).data('id'),
        status: '1'
    }
    common.util.__ajax({
        url: '/search/matchWords/triggerStatus',
        data: param

    }, function(res) {
        tableGird.reload();
    });
});

function timeToSeconds(time) {
    if (time) {
        time = time.replace(/-/g,'/'); // 通用性好一点
        return new Date(time).getTime() / 1000;
    }
}

/**************************************
 * 添加编辑
 **************************************/

 var Bll = {

        toast:function(url, item, hint) {

            var e = new common.edit("#baseform");

            var dialog = common.dialog.confirm(hint, artTemplate("template", item),  function() {

                    e.submit(url, function (option) {

                        console.log(option.data);

                        option.data.startTime = new Date(option.data.startTime).getTime() / 1000;
                        option.data.endTime = new Date(option.data.endTime).getTime() / 1000;

                        option.success=function() {
                            dialog.close();
                            tableGird.reload();
                        };
                        option.error=function(){};
                    });

                    return false;
            });

            e.on("validate", function() {
                var matchWordName = $("#baseform").find("#matchWordName").val();
                if (matchWordName.indexOf(",") >= 0) {
                    return "请使用英文 , 分割";
                }
            });

            e.init();


        }
    };

    $('#btn_add').on('click', function() {
        var item = { "status": 0 }; // 默认开启
        Bll.toast('/search/matchWords/save', item, "创建匹配词");
    });

    $(document).on('click', '.btn_edit', function() {
        var item = tableGird.rows[$(this).data("index")];
        Bll.toast('/search/matchWords/save', item, "编辑匹配词");
    });