<!---- 
Author      : Wahyu / Tanggal : 15/09/2014
Revision    : Ferry / Tanggal : 01/01/2015
-->

<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">
                <tr>
                    <td height="26" align="left" valign="middle">
                    	<a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="parent.tab_new('New Branch Info','setup/branch_info/add/','icon-add');">New</a>
                        <input type="checkbox" id="chk_status" name="chk_status" onclick="suspend(1);" value="3" <?php echo ($detail->StatusBranch == 3)? "checked" : "";?> />Suspend
                    	<a href="#" class="easyui-linkbutton" id="refresh-linkbutton" data-options="plain:true,iconCls:'icon-reload'" onclick="location.reload()">Refresh</a>
                    </td>
                    
                    <td align="right" valign="middle">								
                        <span class="form-title">
                            <?php echo $menu_label; ?>
                        </span>
                    </td>
                </tr>
            </table>		     	
        </div>			
   </div>
   <!-- end top view -->	

   <!-- center view -->
    <div id="form-body-grey" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel"  style="width:auto;padding:5px;height:524px;" fit='true'>
            <form id="fm_branchinfo" name="fm_branchinfo" method="post">
                <table class="form-detail" width="100%" cellspacing="0" cellpadding="0">
                    <tr>
                        <td><b>City</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <input type="text" name="cmb_branch_city1" class="easyui-validatebox td_generaltaxinput" required="true" id="cmb_branch_city1" value="<?php echo $detail->CityName;?>" style="width:850px;background-color:#E3E2E2;" readonly />
                            <input type="hidden" name="cmb_branch_city" class="easyui-validatebox" required="true" id="cmb_branch_city" value="<?php echo $detail->BranchCity;?>" style="background-color:#E3E2E2;" readonly />
                    	</td>
                    </tr>
                    
                    <tr>
                        <td><b>Branch Code</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <input type="text" name="txt_branch_code" class="easyui-validatebox td_generaltaxinput" required="true" id="txt_branch_code" value="<?php echo $detail->BranchCode;?>" style="width:850px;text-transform:uppercase;background-color:#E3E2E2;" readonly />
                        </td>
                    </tr>
                    
                    <tr>
                        <td><b>Branch Name</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <input type="text" name="txt_branch_name" class="easyui-validatebox td_generaltaxinput" required="true" id="txt_branch_name" value="<?php echo $detail->BranchName;?>" style="width:850px;text-transform:uppercase" autocomplete="off" readonly />
                        </td>
                    </tr>
                    
                    <tr>
                        <td valign="top" style="padding-top:2px;"><b>Address</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <textarea name="txt_address" id="txt_address" style="resize:none;width:846px;text-transform:uppercase;" rows="4"><?php echo $detail->BranchAddress;?></textarea>
                        </td>
                    </tr>
                    
                    <tr>
                        <td><b>State</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <select name="cmb_state" id="cmb_state" class="easyui-combogrid" style="width:852px;height:20px" required="true" editable="false" readonly
                                data-options="
                                    value:'<?php echo $detail->BranchState; ?>',
                                    idField:'StateCode',
                                    textField:'StateName',
                                    url:'<?php echo base_url(); ?>custom/get_data/state',
                                    method: 'get',
                                    columns: [[
                                        {field:'StateCode',title:'State Code',width:200,halign:'center',align:'left'},
                                        {field:'StateName',title:'State Name',width:630,halign:'center',align:'left'}
                                    ]],
                                ">
                            </select>
                        </td>
                    </tr>
                    
                    <tr>
                        <td><b>Country</b></td>
                        <td width="850" style="padding:1px 0px 2px;">
                            <select name="cmb_country" id="cmb_country" class="easyui-combogrid" style="width:852px;height:20px" required="true" editable="false" readonly
                                data-options="
                                    value:'<?php echo $detail->BranchCountry; ?>',
                                    idField:'CountryCode',
                                    textField:'CountryName',
                                    url:'<?php echo base_url(); ?>custom/get_data/country',
                                    method: 'get',
                                    columns: [[
                                        {field:'CountryCode',title:'Country Code',width:200,halign:'center',align:'left'},
                                        {field:'CountryName',title:'Country Name',width:630,halign:'center',align:'left'}
                                    ]],
                                ">
                            </select>
                        </td>
                    </tr>
                    
<!--                    <tr>
                        <td>&nbsp;</td>
                        <td width="850" style="padding:1px 0px 2px;text-align:left;">
                            <input type="checkbox" id="as_branch" name="as_branch" value="1" disabled="disabled" <?php echo ($detail->BranchStatus == 1)? "checked" : "";?> style="margin-left:0px;" /> As Branch
                            <input type="hidden" id="chk_as_branch_hidden" name="chk_as_branch_hidden" value="<?php echo ($detail->BranchStatus == 1)? 1 : '';?>" />
                        </td>
                    </tr>-->
                </table>
            </form>
        </div>
    </div>
    <div id="form-footer" data-options="region:'south',border:false">
            <table width="100%" cellpadding="0" cellspacing="0">
                <tr>
                    <td align="right">
                         <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
						 <?php
                            //---------Untuk privellege jika dia ada tombol save----------
                            if($group_user->SavePrivillage == 1){
                                $cek_autority = "";
                            }
                            else{
                                $cek_autority = "disabled";
                            }
                            //-------------end----------------------------
                         ?>			 
                         <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="edit_data('<?php echo $menu_link;?>','#fm_branchinfo');" <?php echo $cek_autority;?>>Save</a> 								
                         <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="parent.tab_close('tt');" <?php echo $cek_autority;?>>Cancel</a>	
                    </td>
                </tr>
            </table>
        </div> 
</div>

<script type="text/javascript">
    function update_status(){
	var trans_number = $('#txt_branch_code').val();
        
	$.ajax({
            type        : "POST",
            url         : "<?php echo base_url();?>setup/branch_info/get_status_transaksi",
            data        : {'number':trans_number},
            dataType    : "json",
            
            success     : function(msg){
                            var status_numb = msg.trans_status;
                            var balance1 = msg.balance;

                            if(status_numb == 2 || status_numb == 4 || status_numb == 5 || status_numb == 6){
				$.messager.alert('Info','Tidak bisa disuspend');
				return false;
                            }
                            else if(status_numb == 3){
				$.messager.alert('Info','Status sudah "Suspend" / "Cancel"');
				$('#chk_suspend').prop('checked', true);
				return false;
                            }
                            else if(balance1 != 0){
				$.messager.alert('Info','Branch memiliki aoutstanding, tidak bisa disuspend','error',function(){
					$('#chk_suspend').prop('checked', false);
				});
				return false;
                            }
                            else{
				$('#chk_suspend').prop('checked', true);
                            }
            }
	});

        return false;
    }

    function insert_citycode_to_branchcode(CityCode,checked_as_branch){
	var new_branchcode;
	var as_branch_checked = $('#'+checked_as_branch).is(':checked');
	
        if(as_branch_checked == true){
            new_branchcode  = '<?php echo $this->session->userdata('company')?>-'+CityCode;
            $('#txt_branch_code').val(new_branchcode);
		
            $.ajax({
                type        : "POST",
                url         : "<?php echo base_url()?>setup/branch_info/check_branch",
                data        : {'number':new_branchcode},
                dataType    : "json",
                
                success     : function(msg){
				var last_code = msg.last_code;
				
				if(last_code == new_branchcode.toUpperCase()){
                                    var lastChar = last_code.substr(last_code.length - 1);
					
                                    if(typeof lastChar != 'string'){
                                        if(lastChar > 1){
                                            var sliceChar = last_code.slice(0,-1); //remove last character
                                            var newNumber = lastChar+1;

                                            new_branchcode = new_branchcode + newNumber;
                                            $('#txt_branch_code').val(new_branchcode);
                                        }
                                    }
                                    else{
                                        new_branchcode = new_branchcode + 2;
                                        $('#txt_branch_code').val(new_branchcode);
                                    }
				}
                }
            });
	}
	else {
            new_branchcode = '<?php echo $this->session->userdata('company')?>-HO-'+CityCode;
            $('#txt_branch_code').val(new_branchcode);
	}
    }

    function suspend(type){
	suspend_data_new('F', type, '<?=$menu_link?>', $('#txt_no').val(), 'Branch Info');
    }
</script>