<!---- 
Author      : Tagor / Tanggal : 15/09/2014
Revision    : Ferry / Tanggal : 01/01/2015 / Insert Branch P/L Account while adding new branch
-->

<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="location.reload();">New</a>
                        <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="padding:5px;" 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;">
                            <select name="cmb_city" id="cmb_city" class="combo-grid" style="width:852px;height:20px" required="true" editable="true"
                                data-options="
                                    idField:'CityCode',
                                    textField:'CityName',
                                    url:'<?php echo base_url(); ?>custom/get_data/city',
                                    method: 'get',
                                    columns: [[
                                        {field:'CityCode',title:'City Code',width:200,halign:'center',align:'left'},
                                        {field:'CityName',title:'City Name',width:630,halign:'center',align:'left'}
                                    ]]
                                ">
                            </select>
                    	</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" required="true" id="txt_branch_code" value="<?php //echo $branch_code;?>" 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" required="true" id="txt_branch_name" style="width:850px;text-transform:uppercase" autocomplete="off" />
                        </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"></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="
                                    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="
                                    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" name="as_costumer" value="1"/> As Customer
                            <input type="checkbox" name="as_branch" id="as_branch" value="1" onclick="insert_citycode_to_branchcode($('#cmb_branch_city').combobox('getValue'),this.id);" style="margin-left:0px;" /> As Branch
                        </td>
                    </tr>-->
                </table>
            </form>
        </div>
    </div>
	<?php
		//---------Untuk privellege jika dia ada tombol save----------
		if($group_user->SavePrivillage == 1){
			$cek_autority_save  = "";
		}
		else{
			$cek_autority_save 	= "disabled";
		}
		//-------------end----------------------------
	?>	
    <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>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="save_transaction('<?php echo $menu_link;?>','#fm_branchinfo');" <?php echo $cek_autority_save;?>>Save</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="parent.tab_close_this('tt');" <?php echo $cek_autority_save;?>>Cancel</a>
                </td>
            </tr>
        </table>
    </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
        $('#cmb_city').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#cmb_state').combogrid('setValue', rowData.StateCode);
                insert_citycode_to_branchcode(rowData.CityCode, true);
            }
        });

        $('#cmb_state').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#cmb_country').combogrid('setValue', rowData.CountryCode);
            }
        });        
    });

    function insert_citycode_to_branchcode(CityCode, checked_as_branch){
	var new_branchcode;
        var as_branch_checked = checked_as_branch;
//	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;
                    var lastChar = last_code.substr(new_branchcode.length, last_code.length - new_branchcode.length);
				
                    if(lastChar > 0){
                        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);
	}
    }
</script>