$(function () {
    zjTableInit();
    $("#searchBtn").click(function () {
        var businessId =$('#BUSINESS_ID option:selected') .val()
        var wfDefintionId =$('#WORKFLOW_DEFINITION_ID option:selected') .val()
        var activityName =$('#ACTIVITY_NAME option:selected') .val()
        var cqjs = $("#cqjs").val();
        var cqbl = $("#cqbl option:selected").val();
        var Url = configUrl+"/bdczj/queryZjxx";
        tableReload("bdczj-grid-table", Url, {businessId: businessId, wfDefintionId: wfDefintionId, activityName: activityName,cqjs:cqjs, cqbl:cqbl});
    });

    loadWorkflowDefinitionList($('select[name=WORKFLOW_DEFINITION_ID]'), '');
    $('select[name=BUSINESS_ID]').change(function () {
        var businessId = $(this).val();
        loadWorkflowDefinitionList($(this).parents('.row-fluid').find('select[name=WORKFLOW_DEFINITION_ID]'), businessId);
    });
    $('select[name=WORKFLOW_DEFINITION_ID]').change(function () {
        var wfDefintionId = $(this).val();
        loadWorkflowActivityList($('.row-fluid').find('select[name=ACTIVITY_NAME]'), wfDefintionId);
    });
});

function loadWorkflowDefinitionList(elements, businessId) {
    var url = configUrl + '/bdczj/getWorkflowDefinitions';
    var param = {
        businessId: businessId
    };
    $.post(url, param, function (result) {
        result.unshift({workflowName: '', workflowDefinitionId: ''});
        var data = {
            workflowDeinfitionList: result
        }
        //var html = template('workflowDeinfitionList', data);
        $(elements).each(function () {
            $(this).empty();
            if (data != null) {
                for (var i = 0; i < data.workflowDeinfitionList.length; i++) {
                    $(this).append(" <option value=" + data.workflowDeinfitionList[i].workflowDefinitionId + ">" + data.workflowDeinfitionList[i].workflowName + "</option>");
                }
            }

        });
    });
}

function loadWorkflowActivityList(elements, wfDefintionId) {
    var url = configUrl + '/bdczj/getWorkflowActivitys';
    var param = {
        wfDefintionId: wfDefintionId
    };
    $.post(url, param, function (result) {
        result.unshift({activityName: '', activityId: ''});
        var data = {
            workflowActivityList: result
        };
        $(elements).each(function () {
            $(this).empty();
            if (data != null) {
                for (var i = 0; i < data.workflowActivityList.length; i++) {
                        $(this).append(" <option value=" + data.workflowActivityList[i].activityName + ">" + data.workflowActivityList[i].activityName + "</option>");
                }
            }

        });
    });
}

function openTask(taskId) {
    var url = portalUrl + '/taskHandle?taskid=' + taskId;
    openWin(url, '_task');
}


//jqGrid初始化
function zjTableInit() {
    var grid_selector = "#bdczj-grid-table";
    var pager_selector = "#bdczj-grid-pager";
    $(window).on('resize.jqGrid', function () {
        $(grid_selector).jqGrid('setGridWidth', $(".page-content").width());
    });

    var parent_column = $(grid_selector).closest('[class*="col-"]');
    $(document).on('settings.ace.jqGrid', function (ev, event_name, collapsed) {
        if (event_name === 'sidebar_collapsed' || event_name === 'main_container_fixed') {
            $(grid_selector).jqGrid('setGridWidth', parent_column.width());
        }
    });

    jQuery(grid_selector).jqGrid({
        datatype: "json",
        height: 'auto',
        jsonReader: {id: 'PRO_ID'},
        colNames: ['编号', '权利人', '义务人', '坐落', '不动产单元号', '流程类型', '节点', '抽取时间','PRO_ID','REMARK','TASKID','WIID'],
        colModel: [
            {name: 'BH', index: 'BH', width: '10%', sortable: false,formatter: function (cellvalue, options, rowObject) {

                   return '<a style="font-size: 12px;" href=\"javascript:openTask(\''+rowObject.TASKID+'\')\">'+rowObject.REMARK.split("$")[0]+'</a>';
                }},
            {name: 'QLR', index: 'QLR', width: '10%', sortable: false},
            {name: 'YWR', index: 'YWR', width: '10%', sortable: false},
            {name: 'ZL', index: 'ZL', width: '15%', sortable: false},
            {name: 'BDCDYH', index: 'BDCDYH', width: '15%', sortable: false},
            {name: 'WORKFLOW_NAME', index: 'WORKFLOW_NAME', width: '10%', sortable: false},
            {name: 'ACTIVITY_NAME', index: 'ACTIVITY_NAME', width: '5%', sortable: false},
            {name: 'CQSJ', index: 'CQSJ', width: '10%', sortable: false,formatter: function (cellvalue, options, rowObject) {
                if (!cellvalue) {
                    return "";
                }
                var data = new Date(cellvalue).Format("yyyy-MM-dd");
                return data;
            }},
            {name: 'PRO_ID', index: 'PRO_ID', width: '0%', sortable: false, hidden: true},
            {name: 'REMARK', index: 'REMARK', width: '0%', sortable: false, hidden: true},
            {name: 'TASKID', index: 'TASKID', width: '0%', sortable: false, hidden: true},
            {name: 'WIID', index: 'WIID', width: '0%', sortable: false, hidden: true},
        ],
        viewrecords: true,
        rowNum: 10,
        rowList: [10, 20, 30],
        pager: pager_selector,
       // pagerpos: "left",
        altRows: false,
        multiboxonly: false,
        rownumbers: true,
        rownumWidth: 50,
        // multiselect: true,
        loadComplete: function () {
            var table = this;
            setTimeout(function () {
                updatePagerIcons(table);
                enableTooltips(table);
                var replacement =
                    {
                        'ui-icon ui-icon-plus': 'ace-icon fa fa-plus bigger-140'
                    };
                $('.ui-pg-table:not(.navtable) > tbody > tr > .ui-pg-button > .ui-icon').each(function () {
                    var icon = $(this);
                    var $class = $.trim(icon.attr('class').replace('ui-icon', ''));

                    if ($class in replacement) icon.attr('class', 'ui-icon ' + replacement[$class]);
                })
            }, 0);

            var jqData = $(grid_selector).jqGrid("getRowData");
            $.each(jqData, function (index, data) {
                asyncLoadZjxx($(grid_selector), data.REMARK,data.PRO_ID);
                asyncSaveZjjl($(grid_selector),data.WIID,data.PRO_ID)
            })
        },
        onSelectRow: function (rowid, status) {

        },
        onSelectAll: function (rowids, status) {

        },
        editurl: "",
        caption: "",
        autowidth: true
    });
}

function asyncLoadZjxx(table, remark,proid) {
    var splitStr="$";
    if (remark != null && remark != "" && remark.split(splitStr).length > 0 && (remark.split(splitStr).length == 4 || remark.split(splitStr).length == 5 || remark.split(splitStr).length == 6 || remark.split(splitStr).length == 7)) {
        // table.setCell(proid, "BH", remark.split(splitStr)[0]);
        table.setCell(proid, "QLR", remark.split(splitStr)[1]);
        table.setCell(proid, "BDCDYH", remark.split(splitStr)[2]);
        table.setCell(proid, "ZL", remark.split(splitStr)[3]);
        table.setCell(proid, "YWR", remark.split(splitStr)[5]);
    }
}

function asyncSaveZjjl(table, wiid,proid) {
    table.setCell(proid, "CQSJ", new Date());
    $.ajax({
        type: "GET",
        url: configUrl + '/bdczj/saveZjjl?wiid='+wiid,
        dataType: "json",
        success: function (data) {
        },
        error: function () {
        }
    });
}