<!---- 
Author  : Ladur
Tanggal : 12-08-2014    
Revisi  :
-->
<?php
//---------Untuk privellege jika dia ada tombol save----------
    if($group_user->SavePrivillage == 1){
        $cek_autority_save  = "";
    }
    else{
        $cek_autority_save  = "disabled";
    }
//-------------end----------------------------
?>

<style type="text/css">
    #datagrid-view{
        overflow: scroll;
    }
</style>

<div class="easyui-layout" data-options="fit:true,border:false">
    <!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false" >
        <div id="form-header-wrapper">
            <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0" id="tab_transstatus">
            
            </table>                
        </div>          
    </div>
    <!-- End of top view -->
    
    <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" style="width:1180%;height:526px;padding:5px;" fit="true">
            <form id="fm_variant" name="fm_variant" method="post">
                <input type="hidden" name="detail" id="detail"/>
                
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                       
                    <td width="50%" valign="top">
                        <table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                            <td>&nbsp;</td>
                            <td width="150" style="background:#333399;text-align:center;color:#FFF;">DOrder No</td>
                            <td width="122" style="background:#333399;text-align:center;color:#FFF;">DOrder Date</td>
                        </tr>
                        <tr height="20">
                            <td>&nbsp;</td>
                            <td>
                                <input name="txt_do_no" id="txt_do_no" type="text" value="<?=$data_do->DOrdNumber;?>" class="easyui-textbox" style="width:170px;height:20px;text-align:center" readonly>
                            </td>
                            <td>
                                <input type="text" name="txt_do_date" id="txt_do_date" value="<?=$data_do->DOrdDate?>" class="easyui-datebox validBackward" onClick="validBackward();" style="width:122px;height:25px" editable="false" required readonly />
                            </td>
                        </tr>
                        </table>
                    </td>
                </tr>
                <tr><td colspan="2" height="20"></td></tr>
                </table>
                <!-- End of Form header -->
                
                <!-- Datagrid -->
                <table id="dtg_variant" class="easyui-edatagrid"
                    idField="id" title="Variants"
                    rownumbers="true" fitColumns="true" 
                    singleSelect="true" style="height:400px;"
                    data-options ="
                            url: '<?= base_url().$menu_link; ?>/get_detail_deiveryordervariant/<?=$data_do->DOrdNumber;?>'
                       ">
                    <thead>
                    <tr>
                        <th align="center" valign="middle" data-options="field:'ItemCode',width:400,align:'left'">ItemCode</th>
                        <th align="center" valign="middle" data-options="field:'ItemDesc',width:400,align:'left'">Description</th>
                        <th align="center" valign="middle" formatter="formatRp" data-options="field:'ItemQty',width:120,align:'center'">QTY</th>
                        <th align="center" valign="middle" data-options="field:'ItemUnit',width:400,align:'left'">Unit</th>
                    </tr>
                    </thead>
                </table>
            </form>
        </div>
    </div>
    <div id="form-footer" data-options="region:'south',border:false">
        <table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>&nbsp;</td>
                <td align="right">
                 <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
                 <a href="javascript:void(0)" class="easyui-linkbutton form-button save-button" iconCls="icon-save" onclick="save_me()" <?php echo $cek_autority_save;?>>Save</a>
                 <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="javascript:location.reload()">Cancel</a>      
                </td>
             </tr>
         </table>
        </div>
</div>

<!-- <script type="text/javascript" src="http://www.jeasyui.com/easyui/datagrid-detailview.js"></script> -->
<script type="text/javascript">
var SavePrivillage  = '<?=$group_user->SavePrivillage;?>';
var ErrQTY = 0;
var arrSubGrid = [];
var indGridEdit = -1;
$(document).ready(function(){
    
    $('#dtg_variant').datagrid({
        view: detailview,
        detailFormatter:function(index,row){
            return '<div style="padding:2px"><table class="ddv"></table></div>';
        },
        onExpandRow: function(index,row){
            var TotalQTY    = row.ItemQty;
            var ddv         = $(this).edatagrid('getRowDetail',index).find('table.ddv');
            ddv.edatagrid({
                url:'',
                fitColumns:true,
                singleSelect:true,
                rownumbers:true,
                loadMsg:'',
                height:'auto',
                onBeginEdit:function(indEdit,rowEdit){
                    if(indGridEdit >=0){
                        if(indGridEdit != index){
                            $("#fm_variant").trigger("click");
                        }
                    }

                    indGridEdit = index;
                    save_button_begendedit('<?=$group_user->SavePrivillage;?>');
                },
                onEndEdit:function(indEdit,rowEdit){
                    save_button_endedit('<?=$group_user->SavePrivillage;?>');
                },
                toolbar:[{
                iconCls: 'icon-add',
                    handler: function(){
                        ddv.edatagrid('saveRow');
                        var item        = ddv.edatagrid('getRows');
                        var errField    = 0;
                        $.each(item,function(ind,val){
                            if((val.Variant == "") || (val.QTY == "") || (val.QTY == 0) || (typeof(val.QTY) == 'undefined')){
                               errField++;
                            }
                        });

                        if(errField > 0){
                           $.messager.alert('Info','Silahkan input Field sebelumnya dengan benar.','error');
                        }else{
                            ddv.edatagrid('addRow');
                        }

                        $('#dtg_variant').edatagrid('fixDetailRowHeight',index);
                    }
                },{
                    iconCls: 'icon-remove',
                    handler: function(){
                        var selectedrow     = ddv.edatagrid('getSelected');
                        var row_index       = ddv.datagrid('getRowIndex', selectedrow);
                        ddv.edatagrid('deleteRow',row_index);

                        ddv.edatagrid('saveRow');
                        var item            = ddv.edatagrid('getRows');
                        arrSubGrid[row.ItemCode]  = item;
                        $('#dtg_variant').edatagrid('fixDetailRowHeight',index);
                    }
               }],
                columns:[[
                    {field:'Variant',title:'Variant',width:200,align:'left',editor:'text'},
                    {field:'QTY',title:'QTY',width:80,align:'right',editor:{type:'numberbox',options:{min:0,precision:2,decimalSeparator:','}}}
                ]],
                onResize:function(){
                    $('#dtg_variant').edatagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        $('#dtg_variant').edatagrid('fixDetailRowHeight',index);
                    },0);
                }
            });
            $('#dtg_variant').edatagrid('fixDetailRowHeight',index);
        }
    });

    $("#fm_variant").on("click",function(){
        if(indGridEdit >= 0){
            var selectedrow   = $("#dtg_variant").edatagrid("selectRow",indGridEdit);
            var itemParent    = $("#dtg_variant").edatagrid('getSelected');
            var TotalQTY      = parseFloat(itemParent.ItemQty);
            var ItemCode      = itemParent.ItemCode;

            var ddv         = $("#dtg_variant").edatagrid('getRowDetail',indGridEdit).find('table.ddv');  
            ddv.edatagrid('saveRow');
            var item        = ddv.edatagrid('getRows');
            var litem       = item.length;
            var total       = 0;
            
            $.each(item, function(ind, val) {
                if((val.Variant == "") || (val.QTY == "") || (val.QTY == 0) || (typeof(val.QTY) == 'undefined')){
                    ddv.edatagrid('deleteRow',ind);
                    ddv.edatagrid('saveRow');
                }else{
                    total+= parseFloat(val.QTY);   
                }
            });

            if(total < TotalQTY){
                $.messager.alert('Info','Total QTY kurang dari parent.','error',function(){
                    // var ed2 = ddv.edatagrid('getEditor',{index:(litem-1),field:'Variant'});
                    // $(ed2.target).focus();
                });
            }else if(total > TotalQTY){
                $.messager.alert('Info','Total QTY lebih dari parent.','error',function(){
                    // var ed2 = ddv.edatagrid('getEditor',{index:(litem-1),field:'Variant'});
                    // $(ed2.target).focus();
                });
            }

            item                  = ddv.edatagrid('getRows');        
            arrSubGrid[ItemCode]  = item;
        }
    });
});

function save_me(){
    var itemGrid        = $('#dtg_variant').edatagrid('getRows');
    var subItemGrid     = [];
    var ErrComQTY       = 0;
    var indItem         = 0;
    $.each(itemGrid,function(ind,val){
        var IQTY    = parseFloat(val.ItemQty);
        var ICode   = val.ItemCode;
        var TSubQTy = 0;
        if(typeof(arrSubGrid[ICode]) == "undefined"){

        }else{
            $.each(arrSubGrid[ICode],function(inds,vals){
                TSubQTy += parseFloat(vals.QTY);
                var arrTemp = {'ItemCode':ICode,'Variant':vals.Variant,'QTY':parseFloat(vals.QTY)};
                subItemGrid.push(arrTemp);
                indItem++;
            });

            if(IQTY == TSubQTy){

            }else{
                ErrComQTY++;
                $.messager.alert('Info','Maaf, Item '+ICode+' QTY tidak sama.','error');
                return false;
            }
        }
    });

    if(ErrComQTY > 0){

    }else{
        if(indItem > 0){
            var detail = JSON.stringify(subItemGrid);
            $("#detail").val(detail);
            save_transaction_other('<?=$menu_link?>','#fm_variant');
        }else{
          $.messager.alert('Info','Maaf, Silahkan input variant terlebih dahulu.','error');  
        } 
    }
}
</script>