/**
 * 数据管理-税率数据管理首页JS
 *
 * Created by li.kui on 20203/17.
 */
'use strict';
var el = {
    searchForm: '#searchForm',
    pgCode: '#pgCode',
    pgName: '#pgName',
    poCode: '#poCode',
    orgCodeOrName: '#orgCodeOrName',
    tb: '#tb',
    addOrEditDialog: null,
    lookDialog: null,
    bodyHeight: document.documentElement.clientHeight
};


$(function () {
    loadGrid();
});


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

    $(el.tb).datagrid({
        loadMsg: '数据加载中......',
        url: g.ctx + 'pc/sys/tax/rest/list',
        singleSelect: true,
        height: el.bodyHeight - searchHeight,
        pagination: true,
        pageSize: pager.pageSize,
        pageList: pager.pageList,
        rownumbers: true,
        queryParams: {},
        columns: [
            [
                {
                    field: 'ck',
                    checkbox: true
                },
                {
                    field: 'taxCode',
                    title: '税率编码',
                    width: 120
                },
                {
                    field: 'taxName',
                    title: '税率名称',
                    width: 200
                },
                {
                    field: 'taxValue',
                    title: '税率值',
                    width: 200
                },
                {
                    field: 'createUserName',
                    title: '创建人',
                    width: 200
                },
                {
                    field: 'createTime',
                    title: '创建日期',
                    width: 140,
                    formatter: function (value) {
                        return moment(value).format('YYYY-MM-DD HH:mm');
                    }
                }
            ]
        ],
        toolbar: [
            {
                text: '新增',
                iconCls: 'icon-add',
                handler: function () {
                    addOrEdit(0);
                }
            },
            "-",
            {
                text: '编辑',
                iconCls: 'icon-edit',
                handler: function () {
                    addOrEdit(1);
                }
            },
            "-",
            {
                text: '删除',
                iconCls: 'icon-remove',
                handler: function () {
                    delFactory();
                }
            }
        ]
    });
}


/**
 * 新增或编辑采购组
 *
 * @param type 0:新增 1:编辑
 */
function addOrEdit(type) {
    var id= '';
    if(type===1){
        var cbox = $('#tb').datagrid('getSelected');
        if (cbox === null || cbox === undefined) {
            layer.msg("请选中一条记录");
            return;
        }
        id = cbox.id;
    }

    var taxDialog = openDialog('taxInfo', g.ctx + 'pc/sys/tax/detail?id=' + id, {
        title: type===0?'新增':'编辑',
        width: '750px',
        height: '200px',
        cache: false,
        modal: true,
        border: false,
        draggable: false,
        shadow: false,
        onClose: function () {
            taxDialog.dialog('destroy');
        },
        buttons: [
            {
                text: itablei8n.save,
                iconCls: 'icon-save',
                handler: function () {
                    topWin.frames['taxInfo'].saveOrUpdate(function () {
                        topWin.layer.msg(tipsi18n.czcg, {icon: 1});
                        $(el.tb).datagrid('reload');
                        taxDialog.dialog('close');
                    });
                }
            },
            {
                text: itablei8n.close,
                iconCls: 'icon-cancel',
                handler: function () {
                    taxDialog.dialog('close');
                }
            }
        ]
    });
}

/**
 * 删除
 */
function delFactory() {
    var cbox = $('#tb').datagrid('getSelected');
    if (cbox === null || cbox === undefined) {
        layer.msg("请选中一条记录");
        return;
    }

    var taxInfo = cbox.taxCode;

    parent.layer.confirm('是否删除？', function (index) {
        var loading = top.layer.msg('处理中', {icon: 16, shade: [0.1, '#000'], time: false});
        var url = g.ctx + 'pc/sys/tax/rest/remove';
        $.ajax({
            url: url,
            method: "post",
            data: {taxCode: taxInfo},
            success: function (data) {
                parent.layer.close(index);
                $(el.tb).datagrid('reload');
            },
            complete: function(data) {
                top.layer.close(loading);
            },
            error: function (data) {
                layer.close(index);
                layer.alert(JSON.parse(data.responseText).message);
            }
        });
    });
}


