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

/**
 * 初始化表格的列
 */
Expert.initColumn = function () {
    return [
        {field: 'selectItem', radio: true},
            {title: '唯一标识', field: 'id', visible: false, align: 'center', valign: 'middle'},
            {title: '专家姓名', field: 'name', visible: true, align: 'center', valign: 'middle'},
            {title: '联系方式', field: 'phone', visible: true, align: 'center', valign: 'middle'},
            {title: '备注', field: 'description', visible: true, align: 'center', valign: 'middle'},
            {title: '有效状态', field: 'state', visible: true, align: 'center', valign: 'middle',
            	formatter: function (value, row, index){
                    if (value==null || value==''){
                        return "";
                    } else if (value == 1){
                        return "有效";
                    }else if(value==2){
                        return "无效";
                    }else{
                        return "";
                    }
                }
            }
    ];
};

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

/**
 * 点击添加expert
 */
Expert.openAddExpert = function () {
    var index = layer.open({
        type: 2,
        title: '添加专家',
        area: ['800px', '420px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/expert/expert_add'
    });
    this.layerIndex = index;
};

/**
 * 打开查看expert详情
 */
Expert.openExpertDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '专家详情',
            area: ['800px', '420px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/expert/expert_update/' + Expert.seItem.id
        });
        this.layerIndex = index;
    }
};
/**
 * 删除expert
 */
Expert.delete = function () {
    if(this.check()&&Expert.stateCheck()){
        var operation=function(){
            var expertId=Expert.seItem.id;
            var ajax = new $ax(Feng.ctxPath + "/expert/delete", function () {
                Feng.success("删除成功!");
                Expert.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("expertId",expertId);
            ajax.start();
        };
        Feng.confirm("是否删除用户" + Expert.seItem.name + "?",operation)
    }
};
/**
 * 查询expert列表
 */
Expert.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['phone'] = $("#phone").val();
    queryData['state'] = $("#state").val();
    Expert.table.refresh({query: queryData});
};

Expert.stateCheck = function () {
    var state=this.seItem.state;
    if(state=='1'){
        Feng.info("请先将记录改成无效状态！");
        return false;
    }else {
        return true;
    }
};

$(function () {
    var defaultColunms = Expert.initColumn();
    var table = new BSTable(Expert.id, "/expert/list", defaultColunms);
    table.setPaginationType("server");
    Expert.table = table.init();
});
