/**
 * 商城收获地址维护页面
 * @Author qmx
 */
var el = {
    searchForm: '#searchForm',
    receivingUser: '#receivingUser',
    tb: '#tb',
    addOrEditDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


/**
 * 文档就绪
 */
$(function () {
    loadGrid();
});

function loadGrid() {
    var searchHeight = $(el.searchForm).outerHeight();
    var pager = initGridPagerNew(el.bodyHeight - searchHeight, 37, 40);

    $(el.tb).datagrid({
        loadMsg: '数据远程载入中, 请等待...',
        height: el.bodyHeight - searchHeight,
        url: '/pc/mall/address/rest/list',
        singleSelect: true,
        rownumbers: true,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'receivingUser',
                    title: '收货人',
                    width: '150'
                },
                {
                    field: 'receivingTel',
                    title: '收货人电话',
                    width: '150'
                },
                {
                    field: 'provinceName',
                    title: '省',
                    width: '150'
                },
                {
                    field: 'cityName',
                    title: '市',
                    width: '150'
                },
                {
                    field: 'countyName',
                    title: '区',
                    width: '150'
                },
                {
                    field: 'address',
                    title: '地址',
                    width: '200'
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                id: 'btn_add',
                hidden: true,
                handler: function () {
                    addAddress(0);
                }
            },
            '-',
            {
                text: '编辑',
                iconCls: 'icon-edit',
                id: 'btn_edit',
                handler: function () {
                    addAddress(1);
                }
            },
            '-',
            {
                text: '删除',
                hide: true,
                id: 'btn_remove',
                iconCls: 'icon-remove',
                handler: function () {
                    delAddress();
                }
            }
        ]
    });
}


/**
 * 新增、编辑
 *
 * @param type 0.新增 1.编辑
 */
function addAddress(type) {
    var title = '新增收货地址', id = 0;
    if (Number(type) === 1) {
        title = '编辑收货地址';

        var row = $(el.tb).datagrid('getSelected');
        if (!row) {
            topWin.layer.alert('请选择一条记录进行操作', {icon: 0});
            return false;
        }

        id = row.id;
    }

    var url = '/pc/mall/address/page/detail?id=' + id;
    el.addOrEditDialog = openDialog('addOrEdit', url, {
        title: title,
        width: '800px',
        height: '420px',
        cache: false,
        modal: true,
        border: 'thin',
        onClose: function () {
            el.addOrEditDialog.dialog('destroy');
        },
        buttons: [
            {
                text: '保存',
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['addOrEdit'].dataSave(function () {
                        topWin.layer.msg('数据保存成功', {icon: 1});
                        $(el.tb).datagrid('reload');
                        el.addOrEditDialog.dialog('close');
                    });
                }
            },
            {
                text: '关闭',
                iconCls: 'icon-clear',
                handler: function () {
                    $(el.tb).datagrid('clearSelections');
                    el.addOrEditDialog.dialog('close');
                }
            }
        ]
    });
}


/**
 * 删除收货地址
 */
function delAddress() {
    var row = $('#tb').datagrid('getSelected');
    if (!row) {
        layer.msg("请选中一条记录");
        return;
    }
    var receivingAddressId = row.id;

    topWin.layer.confirm('您确定要删除选中的记录吗?', {icon: 3}, function (index) {
        topWin.layer.close(index);
        $.ajax({
            type: 'POST',
            url: '/pc/mall/address/rest/del',
            data: {receivingAddressId: receivingAddressId},
            success: function () {
                topWin.layer.msg('删除成功', {icon: 1});
                $(el.tb).datagrid('load');
            },
            error: function (data) {
                topWin.layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    });
}


/**
 * 搜索
 */
function dataSearch() {
    var queryParams = {};
    queryParams.receivingUser = $.trim($(el.receivingUser).val());
    $(el.tb).datagrid('options').queryParams = queryParams;
    $(el.tb).datagrid('load');
}


/**
 * 重置查询
 */
function resetSearch() {
    $(el.receivingUser).val('');
    dataSearch();
}
