/**
 * 单位管理初始化
 */
var Company = {
    id: "CompanyTable", //表格id
    seItem: null, //选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Company.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle', visible: false},
        {title: '单位简称', field: 'shortName', align: 'center', valign: 'middle', sortable: false},
        {title: '单位全称', field: 'name', align: 'center', valign: 'middle', sortable: false},
        {title: '统一社会信用代码(不可修改)', field: 'code', align: 'center', valign: 'middle', sortable: false},
        {title: '排序', field: 'sn', align: 'center', valign: 'middle', sortable: true},
        {title: '备注', field: 'description', align: 'center', valign: 'middle', sortable: false}
    ]
};

/**
 * 检查是否选中
 */
Company.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Company.seItem = selected[0];
        return true;
    }
};

/**
 * 点击添加单位
 */
Company.openAddCompany = function () {
    var index = layer.open({
        type: 2,
        title: '添加单位',
        area: ['800px', '380px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: '/admin/company/add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看单位详情
 */
Company.openCompanyDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '单位详情',
            area: ['800px', '380px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/admin/company/edit/id/' + Company.seItem.id
        });
        this.layerIndex = index;
    }
};

Company.select = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '单位详情',
            area: ['800px', '380px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: '/admin/company/view/id/' + Company.seItem.id
        });
        this.layerIndex = index;
    }
}

/**
 * 删除单位
 */
Company.delete = function () {
    if (this.check()) {
        var operation = function () {
            var ajax = new $ax("/admin/company/delete", function () {
                Feng.success("删除成功!");
                Company.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", Company.seItem.id);
            ajax.start();
        };
        Feng.confirm("是否刪除该单位?", operation);
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Company.formParams = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['code'] = $("#code").val();
    return queryData;
}

/**
 * 查询单位列表
 */
Company.search = function () {
    Company.table.refresh({query: Company.formParams()});
};
/**
 * 重置
 */
Company.reset = function () {
    $("#name").val("");
    $("#code").val("");
};
$(function () {
    var defaultColunms = Company.initColumn();
    var table = new BSTable(Company.id, "/admin/company/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Company.formParams());
    Company.table = table.init();
});
