<?php
$arrPM = ["1" => 'Cash',
"2" => 'Cheque',
"3" => 'Credit card',
"4" => 'Online banking',
"5" => 'Club Membership',
"6" => 'Complementary',
"7" => 'Replacement',
"8" => 'Invoice'];
?>

<link rel="stylesheet" href="<?php echo base_url();?>assets/css/cust_paginition.css" type="text/css" />
<script src="<?php echo base_url(); ?>assets/library/jquery/jquery.min.js"></script>
<div class="layout-app">
    <div class="container">
        <div class="row row-app">
            <div class="col-md-12">
                <div class="col-separator col-separator-first col-unscrollable bg-none">
                    <div class="col-table">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="widget">
                                    <?php $this->load->view('admin/flashdata_admin'); ?>
                                    <div class="widget-head">
                                        <a href="#modalAddFD" data-toggle="modal" class="btn btn-inverse pull-right mar" onclick="return add_finance_department();"><i class="fa fa-fw fa-plus"></i>Add Finance</a>
                                        <h4 class="heading">Finance Department</h4>
                                    </div>
                                    <div class="widget-body">
                                        <div class="col-md-3" style="margin-bottom: 15px;">
                                             <label>Payment Method</label>
                                            <select class="form-control" id="fb_filter_pm">
                                                <option value="">--All--</option>
                                                <?php
                                                foreach ($arrPM as $key => $pm) {
                                                    echo '<option value="'.$key.'"';
                                                    if($this->uri->segment(3) == $key) { echo ' selected="selected" ';}
                                                    echo '>'.$pm.'</option>';
                                                }
                                                ?>
                                            </select>
                                        </div>

                                        <table class="table table-white table-vertical-center margin-none table-striped footable">
                                            <thead>
                                                <tr>
                                                    <th>S.No</th>
                                                    <th>Campaign</th>
                                                    <th>Payment Method</th>
                                                    <th>Payment Date</th>
                                                    <th>BOX</th>
                                                    <th>OWL</th>
                                                    <th>RRJ</th>
                                                    <th>Think</th>
                                                    <th>Chinese</th>
                                                    <th>Science Adventure</th>
                                                    <th>Highlights</th>
                                                    <th class="text-center">Actions</th>
                                                    
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php 
                                                if (count($fdrows) > 0) { 
                                                foreach ($fdrows as $key => $row) {
                                                ?>
                                                <tr>
                                                    <td><?php echo ($key+1); ?></td>
                                                    <td>
                                                    <?php 
                                                    foreach ($arrCampaign as $key => $campaign) {
                                                        if($campaign['id'] == $row['campaign_id']) {
                                                            echo $campaign['name'];
                                                        }
                                                    }
                                                    ?>
                                                    </td>
                                                    <td>
                                                        <?php 
                                                        echo $arrPM[$row['payment_method']];
                                                        ?></td>
                                                    <td><?php echo $row['payment_date']; ?></td>
                                                    <td><?php echo $row['box']; ?></td>
                                                    <td><?php echo $row['owl']; ?></td>
                                                    <td><?php echo $row['rrj']; ?></td>
                                                    <td><?php echo $row['think']; ?></td>
                                                    <td><?php echo $row['chinese']; ?></td>
                                                    <td><?php echo $row['science_adventure']; ?></td>
                                                    <td><?php echo $row['highlights']; ?></td>
                                                    <td class="text-center">
                                                        <a class="btn edit_modal" data-edit_id="<?php echo $row['id']?>" >
                                                            <i class="fa fa-edit"></i>
                                                        </a>
                                                    </td>
                                                </tr>
                                                <?php }}else {
                                                    echo '<tr><td colspan="12" class="text-center">No data found</td></tr>';
                                                } ?>
                                            </tbody>
                                        </table>
                                        
                                    </div>
                                    <!-- // END TRENDS -->
                                </div>
                            </div>
                        </div>
                        <div class="col-separator-h"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="modalAddFD" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <a class="btn  pull-right mar"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> </a>
                <h3 class="modal-title" align="left">Add Finance</h3>
            </div>
            <div class="modal-body">
                <form>
                    <div class="row">

                        <div class="col-md-6 form-group">
                            <label>Campaign</label>
                            <select class="form-control" id="fb_campaign_id">
                                <option value="">Select One</option>
                                <?php 
                                foreach ($arrCampaign as $key => $campaign) {
                                    echo '<option value="'.$campaign['id'].'">'.$campaign['name'].'</option>';
                                }
                                ?>
                            </select>
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>Payment Method</label>
                            <select class="form-control" id="fb_payment_method">
                                <option value="">--All--</option>
                                <option value="1">Cash</option>
                                <option value="2">Cheque</option>
                                <option value="3">Credit card</option>
                                <option value="4">Online banking</option>
                                <option value="5">Club Membership</option>
                                <option value="6">Complementary</option>
                                <option value="7">Replacement</option>
                                <option value="8">Invoice</option>
                            </select>
                            <span style="color: red"></span>
                        </div>

                        

                        <div class="col-md-6 form-group">
                            <label>Payment Date</label>
                            <input type="text" name="" class="form-control datepicker" id="fb_payment_date" data-date-format="dd-mm-yyyy" >
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>BOX</label>
                            <input type="text" name="" class="form-control" id="fb_box">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>OWL</label>
                            <input type="text" name="" class="form-control" id="fb_owl">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>RRJ</label>
                            <input type="text" name="" class="form-control" id="fb_rrj">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>Think</label>
                            <input type="text" name="" class="form-control" id="fb_think">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>Chinese</label>
                            <input type="text" name="" class="form-control" id="fb_chinese">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>Science Adventure</label>
                            <input type="text" name="" class="form-control" id="fb_science_adventure">
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-6 form-group">
                            <label>Highlights</label>
                            <textarea class="form-control" id="fb_highlights"></textarea>
                            <span style="color: red"></span>
                        </div>

                        <div class="col-md-12 form-group" style="margin-top: 10px;">
                            <input type="hidden" id="edit_id" >
                            <input type="button" class="btn btn-success" id="btn_add_fd" value="Submit" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

</div>
</div>
</div>
<input type="hidden" id="system_base_url" value="<?php echo base_url();?>" />
<script src="<?php echo base_url(); ?>assets/library/jquery/jquery.min.js"></script>
<script src="<?php echo base_url(); ?>assets/js/subscriber.js"></script>
<script type="application/javascript">
$(function () {

   // $('.datepicker').datepicker();
      
    $(document).on('change', '#fb_filter_pm', function () {
        if(this.value != '')  {
            window.location = '<?php echo base_url();?>index.php/financedepartment/index/'+this.value;
        }else {
            window.location = '<?php echo base_url();?>index.php/financedepartment/';
        }
    });
    
    $(document).on('click', '#btn_add_fd', function () {

        var fb_campaign_id = $('#fb_campaign_id').val();
        var fb_payment_date = $('#fb_payment_date').val();
        var fb_payment_method = $('#fb_payment_method').val();
        var fb_box = $('#fb_box').val();
        var fb_owl = $('#fb_owl').val();
        var fb_rrj = $('#fb_rrj').val();
        var fb_think = $('#fb_think').val();
        var fb_chinese = $('#fb_chinese').val();
        var fb_science_adventure = $('#fb_science_adventure').val();
        var fb_highlights = $('#fb_highlights').val();

        var edit_id = $('#edit_id').val();

        $('#fb_campaign_id').next('span').text('');
        $('#fb_payment_date').next('span').text('');
        $('#fb_payment_method').next('span').text('');
        $('#fb_box').next('span').text('');
        $('#fb_owl').next('span').text('');
        $('#fb_rrj').next('span').text('');
        $('#fb_think').next('span').text('');
        $('#fb_chinese').next('span').text('');
        $('#fb_science_adventure').next('span').text('');
        $('#fb_highlights').next('span').text('');

        var post_data = {
            fb_campaign_id:fb_campaign_id,
            fb_payment_date:fb_payment_date,
            fb_payment_method:fb_payment_method,
            fb_box:fb_box,
            fb_owl:fb_owl,
            fb_rrj:fb_rrj,
            fb_think:fb_think,
            fb_chinese:fb_chinese,
            fb_science_adventure:fb_science_adventure,
            fb_highlights:fb_highlights,
            edit_id:edit_id
        };

        var request;
        if (request) {
            request.abort();
        }
        request = $.ajax({
            url: '<?php echo base_url();?>index.php/financedepartment/add_finance_department',
            type: "POST",
            dataType: 'JSON',
            data: post_data
        });
        request.done(function(response) {
            if (response['status'] == 1) {
                location.reload();
            } else {
                alert(response['message']);
            }
        });
        request.fail(function(jqXHR, textStatus, errorThrown) {
            alert("The following error occurred: " + textStatus, errorThrown);
            //location.reload();
        });
        request.always(function() {}); 
    });


    $(document).on('click', '.edit_modal', function () {
        var edit_id = $(this).data('edit_id');
        if(edit_id != '') {
            request = $.ajax({
                url: '<?php echo base_url();?>index.php/financedepartment/get_finance_department',
                type: "POST",
                dataType: 'JSON',
                data: { edit_id:edit_id }
            });
            request.done(function(response) {
                if(response) {
                    $('#modalAddFD').modal();
                    var date = response[0]['payment_date'].split('-');

                    $('#fb_campaign_id').val(response[0]['campaign_id']);
                    $('#fb_payment_date').val( date[2] +'-'+date[1]+'-'+date[0]);
                    $('#fb_payment_method').val(response[0]['payment_method']);
                    $('#fb_box').val(response[0]['box']);
                    $('#fb_owl').val(response[0]['owl']);
                    $('#fb_rrj').val(response[0]['rrj']);
                    $('#fb_think').val(response[0]['think']);
                    $('#fb_chinese').val(response[0]['chinese']);
                    $('#fb_science_adventure').val(response[0]['science_adventure']);
                    $('#fb_highlights').val(response[0]['highlights']);

                    $('#edit_id').val(edit_id);
                }
            });
            request.fail(function(jqXHR, textStatus, errorThrown) {
                alert("The following error occurred: " + textStatus, errorThrown);
                //location.reload();
            });
        }
    });

});

function add_finance_department()
{
    // var base_url = $("#system_base_url").val();
    // var html = "<div align='center'><img src='"+base_url+"assets/images/loader.gif'/></div>";
    // $("#tran_preview").html(html);
    // $.ajax({
    //     url: base_url+"index.php/subscriber/add_subscriber",
    //     'type' : 'GET',
    //     success:function(result){
    //         $("#tran_preview").html(result);
    //         //alert(result);
    //     }
    // });
}
</script>

<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/js/bootstrap-datetimepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.7.14/css/bootstrap-datetimepicker.min.css">
 -->