/**
 * 通用附件类别管理初始化
 */
var CurrencyFiletype = {
    id: "CurrencyFiletypeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
CurrencyFiletype.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '唯一标识', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '申报类别', field: 'typeName', visible: true, align: 'center', valign: 'middle'},
            {title: '附件类型名称', field: 'name', visible: true, align: 'center', valign: 'middle'},
            {title: '附件类型api', field: 'api', visible: true, align: 'center', valign: 'middle'},
            {title: '是否必传', field: 'must', visible: true, align: 'center', valign: 'middle',
                formatter :function (value,row,index) {
                    if(value==1){
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-primary btn-xs\">是</button>";
                    }if(value==2){
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-warning btn-xs\">否</button>";
                    }
                }
            },
            {title: '是否生效', field: 'active', visible: true, align: 'center', valign: 'middle',
                formatter :function (value,row,index) {
                    if(value==1){
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-primary btn-xs\">是</button>";
                    }if(value==2){
                        return "<button type=\"button\" style=\"line-height: 1.3\" class=\"btn btn-warning btn-xs\">否</button>";
                    }
                }
            },
            {title: '排序', field: 'sn', visible: true, align: 'center', valign: 'middle'},
            {
                title: '模板预览', field: 'templateUrl', visible: true, align: 'center', valign: 'middle', width: 80,
                formatter: function (value, row, index) {
                    if(value==null||value==''){
                        return "无";
                    }
                    var sn = value.lastIndexOf(".");
                    var suffix = value.substring(sn+1,value.length);
                    if(suffix=='gif'||suffix=='jpg'||suffix=='jpeg'||suffix=='bmp'||suffix=='png'){
                        return '<img class=\"imgUrl\"  src=\"'+value+'\" style=\"width:25px;height:25px;\">';
                    }else{
                        return "<button type='button'  onclick=\"Feng.showPdf('"+value+"')\" class=\"btn btn-xs btn-danger\"><i class=\"fa fa-file-pdf-o\" aria-hidden=\"true\"></i></button>";
                    }
                }
            },
            {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle'},
    ];
};

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

/**
 * 点击添加通用附件类别
 */
CurrencyFiletype.openAddCurrencyFiletype = function () {
    var index = layer.open({
        type: 2,
        title: '添加通用附件类别',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/admin/currencyFiletype/currencyFiletype_add'
    });
    CurrencyFiletype.layerIndex = index;
};

/**
 * 打开查看通用附件类别详情
 */
CurrencyFiletype.openCurrencyFiletypeDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '通用附件类别详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/admin/currencyFiletype/currencyFiletype_update?id=' + CurrencyFiletype.seItem.id
        });
        CurrencyFiletype.layerIndex = index;
    }
};

/**
 * 删除通用附件类别
 */
CurrencyFiletype.delete = function () {
    if (this.check()) {
        var operation = function() {
            var ajax = new $ax(Feng.ctxPath + "/admin/currencyFiletype/delete", function (data) {
                Feng.success("删除成功!");
                CurrencyFiletype.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("currencyFiletypeId", CurrencyFiletype.seItem.id);
            ajax.start();
        }
        Feng.confirm("是否刪除该附件类别?", operation);
    }
};

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

/**
 * 查询通用附件类别列表
 */
CurrencyFiletype.search = function () {
    CurrencyFiletype.table.refresh({query: CurrencyFiletype.formParams()});
};

/**
 * 重置
 */
CurrencyFiletype.reset = function (){
    $("#type").val("");
    $("#name").val("");
    $("#must").val("");
    $("#active").val("");
}

$(function () {
    var defaultColunms = CurrencyFiletype.initColumn();
    var table = new BSTable(CurrencyFiletype.id, "/admin/currencyFiletype/list", defaultColunms);
    table.setPaginationType("server");
    table.setOnLoadSuccess(function(){
        $(".imgUrl").viewer({
            // toolbar:false,
            fullscreen:false
        });
    });
    CurrencyFiletype.table = table.init();
    //下拉框数据动态加载
    Feng.addAjaxSelect({
        "id": "type",
        "displayCode": "code",
        "displayName": "name",
        "type": "GET",
        "url": Feng.ctxPath + "/admin/dict/findChildDictByCode?code=common_declareType"
    });

});
