<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <h4 class="page-title">Account Mapping for "<?php echo $eventdetails[0]['account_event'];?>":</h4>
            </div>
        </div>
        
        <div class="row">
            <div class="col-md-12">
                <div class="card-box ">
                  <?php foreach($eventdetails as $event){ ?>
                    <form method="post" action="<?php echo base_url('updateeventmapping');?>" >
                        <div class="row div_<?php echo $event['id_account_event_mapping'];?>">
                            <input type="hidden" name="idaccountevent" value="<?php echo $event['id_account_events'];?>">
                            <div class="col-md-2">
                                <div class="form-group">
                                    <label for="txtentityname" class="control-label">Entity Name</label>
                                    <input readonly="readonly" tabindex="1" type="text" class="form-control" placeholder="Entity Name"  name="txteditentityname" value="<?php echo $event['entity_name']; ?>">
                                </div> 
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="txtdescription" class="control-label">Description</label>
                                    <input tabindex="1" type="text" class="form-control" placeholder="Description" id="txtdescription_<?php echo $event['id_account_event_mapping'];?>" name="txtdescription" value="<?php echo $event['description']; ?>">
                                </div> 
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <label for="txtaccounthead" class="control-label">Account Head: </label>
                                    <input style="border: none;" id="txtidaccounthead_<?php echo $event['id_account_event_mapping'];?>" type="text" readonly="readonly" name="txtidaccounthead" value="<?php echo $event['id_account_heads']; ?>">
                                    <input tabindex="1" type="text" class="form-control" readonly="readonly" placeholder="Account Head" id="txtaccounthead_<?php echo $event['id_account_event_mapping'];?>" name="txtaccounthead" value="<?php echo $event['account_head']; ?>">
                                </div> 
                            </div>
                            <div class="col-md-2">
                                <button type="button" onclick='openah("<?php echo $event['id_account_event_mapping']; ?>");' class="btn btn-sm btn-small m-t-30">change</button>
                            </div>
                        </div>  
                        <div class="row div_<?php echo $event['id_account_event_mapping'];?>">
                            <div class="col-md-2">
                                <select class="form-control" name="txttransactiontype" id="txttransactiontype_<?php echo $event['id_account_event_mapping'];?>">
                                    <option value='debit' <?php if($event['transaction_type']=='debit'){echo "selected='selected'";}?>>Debit</option>
                                    <option value='credit' <?php if($event['transaction_type']=='credit'){echo "selected='selected'";}?>>Credit</option>
                                </select>
                            </div>
                            <div class="col-md-2">
                                <input type="hidden" name="txtideventmapping"  value="<?php echo $event['id_account_event_mapping'];?>">
                                <button  type="submit" class="btn btn-warning waves-effect waves-light"><i class="fa fa-pencil"></i> Update</button>
                            </div>
                        </div>
                    <hr/>
                    </form>
                     <?php } ?>
                </div>
            </div>
        </div>
        
        
    </div>
</div>
<div class="modal fade none-border" id="ahmodal" tabindex="-1" role="dialog" aria-labelledby="" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"><strong>Account Heads</strong></h4>
            </div>
            <div class="modal-body">
                 <div class="row" id="ah-divsearch">
                    <div class="col-lg-12">
                        <div id="ah-searchpanel" class="card-box">
                            <h4 class="header-title m-t-0 m-b-30">Select Account Head:</h4>
                            <div class="row">
                                <div class="col-md-12">
                                    <select class="form-control" id="selectaccount">
                                        <?php foreach($mainaccounts as $ma){?>
                                        <option value="<?php echo $ma['id_account_heads']; ?>"><?php echo $ma['account_sub_control'] .' '.$ma['account_head_number'] .' '. $ma['account_head'];?></option>
                                        <?php } ?>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                 </div>
            </div>
            <div class="modal-footer">
                <input type="text" name="idaccounteventmapping" id="idaccounteventmapping"  />
                <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                <button type="button" onclick="replaceah();" class="btn btn-custom waves-effect waves-light">Replace</button>
            </div>
        </div>
    </div>
</div>

<script>
    
    $(document).ready(function(){
       $("#selectaccount").select2(); 
    });
                
     function openah($id) {
        $("#idaccounteventmapping").val($id);
        $("#ahmodal").modal('show');
    }
            
    function replaceah(){
        var data = $("#selectaccount").select2('data');
       
        var account_id = parseInt(data.id);
        var account_head = data.text;
        
        var controlname= "#txtidaccounthead_"+$("#idaccounteventmapping").val();
        $(controlname).val(account_id);
        
        var controlname= "#txtaccounthead_"+$("#idaccounteventmapping").val();
        $(controlname).val(account_head);
        
         var controlname= ".div_"+$("#idaccounteventmapping").val();
        $(controlname).css("background-color","red");
        
        $("#ahmodal").modal('hide');
    }
    </script>