
// 检索list数据
function queryTable(){
    var data = {}
    var keyword = $("#keyword").val();
    if(keyword!=''){
        data.name = keyword ;
    }
    var cols = [
                    {width: 30, text: '序号', flex: false, colClass: 'text-center',field:'num'},
                    {width: 160, text: '考核办法', flex: false,colClass: 'text-center',field:'name'},
                    // {width: 80, text: '被考核部门',  flex: true, colClass: 'text-center',field:'checked_dept_name'},
                    // {width: 100, text: '被考核职位',  flex: true, colClass: 'text-center',field:'checked_position_name'},
                    {width: 80, text: '类型',  flex: false, colClass: 'text-center',field:'rule_type_name'},
                    {width: 100, text: '考核项',  flex: false, type: 'string',colClass: 'text-center modity-flag',field:'checked_item_num' },
                    {width: 80, text: '操作',  flex: false, colClass: 'text-center',field:'id',oper:[
                         {func:'showEditRule',text:'编辑',col_class:'icon-remove-circle'},
                         {func:'deleteCheckRule',text:'删除',col_class:'icon-remove-circle'}
                    ]},
                ] ;
    
    $('#ruleDatatable').mytable({'cols':cols,
                            'url':"/checkRule/getAllRule/",
                            'param':data}
                            );
     $('#addModal').modal('hide');

}

function setRuleTypeSelect(div, selectId) {
    post_common_service("/constant/getConstantByFlag", {code_flag: "rule_type"}, function(data) {
        var optStr = '<option value="">无</option>';
        for (var i in data) {
            if (data[i].code_value == selectId)
                optStr += '<option value="' + data[i].code_value + '" selected="selected">' + data[i].code_name + '</option>';
            else
                optStr += '<option value="' + data[i].code_value + '">' + data[i].code_name + '</option>';
        }
        $(div).html('');
        $(div).html(optStr);
    });
}

// 删除
function deleteCheckRule(id){
    deleteItem4Common(id,"/checkRule/delete/",queryTable);
}
// function addOrUpdateConstant(url){
//     var valid=1;
//     if(!$("#valid").is(':checked')) valid=0;
//     var param ={
//         'id':cur_id,
//         'code_flag':$('#code_flag').val(),
//         'flag_name':$('#flag_name').val(),
//         'code_value':Number($('#code_value').val()),
//         'code_name':$('#code_name').val(),
//         'sort_no':Number($('#sort_no').val()),
//         'remark':$('#remark').val(),
//         'valid':valid
//     };
//     addOrUpdateItem4Common(param,url,queryConstant);
// }

function showEditRule(id){
    cur_id='';
    var rowData=getItemByIdFromArr(id,$('#ruleDatatable').mytable('getTableData'));
    if(rowData) {
        cur_id = rowData.id;
        $('#check_rule_name').val(rowData.name);
        $('#rule_type').val(rowData.rule_type);
        // alert(rowData);
        showPopup4Common('修改考核办法',function(){
            addOrUpdateCheckRule("/checkRule/update/");
        },'400px','#edit-rule-div');
        setRuleTypeSelect("#rule_type", rowData.rule_type);
    }
}

function showAddCheckRule(){
    $('#check_rule_name').val('');
    showPopup4Common('添加考核办法',function(){
        return addOrUpdateCheckRule("/checkRule/add");
    },'400px','#edit-rule-div');
    setRuleTypeSelect("#rule_type", null);
    // $('#addModal').modal('show');
}

var all_data = [];

var dept_selectinput;

// 查询所有的考核项
get_common_service("/dept/get/all","",function(data){
    all_data = [];
    for (var i = 0; i < data.length; i++) {
        var m = data[i];
        var zdataItem = {};
        zdataItem["id"] = m.id;
        zdataItem["value"] = m.id;
        zdataItem["pId"] = null;
        zdataItem["name"] = m.organname;
        zdataItem["children"] = addobj(m);
        all_data.push(zdataItem);
    }
    dept_init();
},function(error){})

function addobj(data) {
    var zdata = [];
    if (typeof(data.childs) != "undefinde" && data.childs != null &&
        data.childs.length > 0) {
        for (var i = 0; i < data.childs.length; i++) {
            var m = data.childs[i];
            var zdataItem = {};
            zdataItem["id"] = m.id;
            zdataItem["value"] = m.id;
            zdataItem["pId"] = data.id;
            zdataItem["icon"] = "";
            zdataItem["name"] = m.organname;
            zdataItem["children"] = addobj(m);
            zdata.push(zdataItem);
        }
        return zdata;
    } else {
        return [];
    }
}



$("#check_item_select").delegate("#treeSelect_panel_0","blur",function(){
    if($("#treeSelect_panel_0").is(":hidden")){
        if(dept_selectinput.value !="" && dept_selectinput.value != null){
            getJob(dept_selectinput.value);
        }
    }
})


function dept_init(){
    dept_selectinput = new TreeSelect({
            element: '#check_item_select',
            data: all_data,
            valueKey: "id",
            placeholder: "请选择部门"
    });
}

/**
 * 检索部门下的职位
 */
function getJob(dept_id){
    var data = {"dept_id": dept_id};
    post_common_service("/job/getList", data , function(data){
        $("#position_id").empty();
         $("#position_id").append("<option value=''>请选择职位</option>");
        for(var i=0;i<data.length;i++){
            $("#position_id").append("<option value='"+data[i].id+"'>"+data[i].name+"</option>")
        }
    }, function(error){});
}

/**
 * 添加考核办法 - 保存
 */
function addOrUpdateCheckRule(url){

    layer.confirm('相同类型的考核办法只能保留一条，可能会影响到之前数据，是否确定保存？', {
        btn: ['是', '否'], //按钮
        shadeClose: true,
    }, function() {
        if(idVal("check_rule_name") == ''){
            tip("请输入考核办法");
            return false;
        }
        // if(idVal("rule_type") == ''){
        //     tip("请选择类型");
        //     return false;
        // }
        // if(idVal("position_id") == ''){
        //     tip("请选择职位");
        //     return false;
        // }
        // if(dept_selectinput.value==null || dept_selectinput.value==''){
        //     tip("请选择部门信息");
        //     return false ;
        // }
        var param ={
            "name":idVal("check_rule_name") ,
            "rule_type":idVal("rule_type"),
            "id":cur_id
            // "checked_dept_id":dept_selectinput.value,
            // "checked_position_id":idVal("position_id"),
            // "checked_person_type":1
        };
        addOrUpdateItem4Common(param,url,queryTable);
    }, function() {

    });
}

function idVal(id){
    return $("#"+id).val();
}

$(document).ready(function() {
    // 初始化标题
    // $("#main_content_title").html("考核办法管理")

    // 检索表格数据
    queryTable();

    // 考核项链接
    $("#ruleDatatable").delegate("td.modity-flag","click",function(){
        var obj = $('#ruleDatatable').data("data_cache")[$(this).attr("data-row")] ;
        $.zui.store.set("check_rule_id",obj.id);
        $.zui.store.set("check_rule_name",obj.name);
        changePage("/view/assess_management/assess_way_edit.html");
    });
});