<script type="text/javascript">
    $(document).ready(function () {
        $('.datatables').DataTable();
        $('.datetimepicker').datetimepicker({
            viewMode: 'months',
            format: 'DD-MM-YYYY'
        });
        //$("#typeofsale").select2();
    });

    function add_sale_row() {
        $("#add_sale_row_modal").modal("show");
        $("#pop_pup_btn").html('<i class="fa fa-plus"></i> <?php echo $this->lang->line("add"); ?>');
        $("#pop_pup_btn").attr("onclick", "popup_add_salerow(this)");
    }

    function popup_add_salerow() {
        if ($("#modal_form_add_new").validationEngine('validate')) {
            var oTable = $("#datatale_sale_list").dataTable();
            var row_no = $("#datatale_sale_list").attr('data-rowno') * 1 + 1;
            $("#datatale_sale_list").attr('data-rowno', row_no);
            description = $("#new_sale_description").val();
            amnt = accounting.formatNumber($("#new_sale_amount").val(), 2);
            ck = '<input onchange="enabled_or_disabled_delete_btn(this)" type="checkbox" />';
            var a = oTable.fnAddData([
                '<td><div id="row_no_'+ row_no +'" class="width-100 text-center">' + row_no + '</div></td>',
                '<td><input type="hidden" value="' + description + '" name="sale_description[]" /><a onclick="edit_row(this)" href="javascript:">' + description + '</a></td>',
                '<td><div class="width-100 text-right"><input type="hidden" value="' + amnt + '" name="sale_amnt[]" />' + amnt + '</div></td>',
                '<td><div class="width-100 text-center">' + ck + '</div></td>'
            ]);
            total = datatable_total("datatale_sale_list", 2);
            $("#new_sale_grandtotal").val(accounting.formatNumber(total, 2));
            $("#new_sale_description").val("");
            $("#new_sale_amount").val("");
            $("#add_sale_row_modal").modal("hide");
        }

    }


    function conf_delete_selected_row() {
        try {
            delete_row = '0';
            var oTable = $("#datatale_sale_list").DataTable();
            ln = $("select[name=datatale_sale_list_length]").val();
            oTable.page.len($("#datatale_sale_list").attr('data-rowno')).draw();
            $(oTable.rows().nodes()).find(':checkbox').each(function () {
                if ($(this).is(':checked')) {
                    oTable.row($(this).parents('tr')).remove();
                    delete_row++;
                }
            });
            row_no = $("#datatale_sale_list").attr('data-rowno') - delete_row;
            $("#datatale_sale_list").attr('data-rowno', row_no);
            oTable.page.len(ln).draw();
            datatable_reoder_rowno("datatale_sale_list");

            total = datatable_total("datatale_sale_list", 2);
            $("#new_sale_grandtotal").val(accounting.formatNumber(total, 2));
            check_box_handler();
        } catch (err) {

        }
    }

    function delete_selected_row() {
        confirmBox('conf_delete_selected_row()', '<?php echo $this->lang->line('msgconfirm'); ?>');
    }

    function select_or_unselect_all(me) {
        var oTable = $("#datatale_sale_list").dataTable();
        if ($(me).is(":checked")) {
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', true);
            $("#btn_delete_sales").removeAttr("disabled");
        } else {
            $(oTable.fnGetNodes()).find(':checkbox').prop('checked', false);
            $("#btn_delete_sales").attr("disabled", "disabled");
        }
    }

    function check_box_handler() {
        var oTable = $("#datatale_sale_list").dataTable();
        $(oTable.fnGetNodes()).find('input[type=checkbox] :checked');
        var notfind = true;
        $(oTable.fnGetNodes()).find(':checkbox:checked').each(function () {
            $("#btn_delete_sales").removeAttr("disabled");
            notfind = false;
            return true;
        });
        if (notfind) {
            $("#btn_delete_sales").attr("disabled", "disabled");
        }
    }

    function enabled_or_disabled_delete_btn(me) {
        if ($(me).is(":checked")) {
            $("#btn_delete_sales").removeAttr("disabled");
        } else {
            check_box_handler();
        }
    }

    function verify_bebore_submit_update() {
        var oTable = $("#datatale_sale_list").DataTable();
        if (oTable.page.info().recordsTotal < 1) {
            notify('<?php echo $this->lang->line('msg_no_entry_found'); ?>', false, 'warning');
            return false;
        }
        if ($("#frm_submit_insert_sales").validationEngine('validate')) {
            confirmBox('conf_update_sale()', '<?php echo $this->lang->line('msgconfirm'); ?>');

        }
    }

    function conf_update_sale() {
        $("#frm_submit_insert_sales").submit();
    }

    function edit_row(me) {
        tr = $(me).parents("tr");
        desc = $(tr).find("td:eq(1) a").text();
        amt = $(tr).find("td:eq(2)").text();

        $("#add_sale_row_modal").modal("show");

        $("#new_sale_description").val(desc);
        $("#new_sale_amount").val(amt);

        $("#pop_pup_btn").html("<?php echo $this->lang->line('update'); ?>");
        $("#pop_pup_btn").attr("onclick", "update_in_table(this)");
        $("#pop_pup_btn").attr("data-rowid", $(tr).find("td:eq(0) div").attr("id"));
    }

    function update_in_table(me) {
        tr = $("#"+$(me).attr("data-rowid")).parents("tr");
        description = $("#new_sale_description").val();
        amnt = accounting.formatNumber($("#new_sale_amount").val(), 2);
        $(tr).find("td:eq(1) input").val(description);
       
        $(tr).find("td:eq(1) a").text(description);
        $(tr).find("td:eq(2)").html('<div class="width-100 text-right"><input type="hidden" value="' + amnt + '" name="sale_amnt[]" />'+amnt+'</div>');

        total = datatable_total("datatale_sale_list", 2);
        $("#new_sale_grandtotal").val(accounting.formatNumber(total, 2));
        $("#new_sale_description").val("");
        $("#new_sale_amount").val("");
       $("#add_sale_row_modal").modal("hide");
    }

</script>
<style>

</style>
<!-- Content Wrapper. Contains page content -->

<div id="add_sale_row_modal" class="modal fade in">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 id="myModalLabel" class="modal-title"><?php echo $this->lang->line('addnewsalerow') ?></h4>

            </div>
            <div class="modal-body">
                <div class="row" >
                    <div class="col-md-12">
                        <form role="form" id="modal_form_add_new">
                            <div class="form-group">
                                <label><?php echo $this->lang->line('description'); ?></label>
                                <input type="text" placeholder="<?php echo $this->lang->line('description'); ?>" id="new_sale_description" class="form-control validate[required]">
                            </div>
                            <div class="form-group">
                                <label><?php echo $this->lang->line('amount'); ?></label>
                                <input type="text" placeholder="<?php echo $this->lang->line('amount'); ?>" id="new_sale_amount" class="form-control calculator validate[required]">
                            </div>
                        </form>
                    </div>

                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><?php echo $this->lang->line('close') ?></button> 
                <button class="btn btn-primary" type="button" id="pop_pup_btn"  onclick="popup_add_salerow(this)"> <i class="fa fa-plus"></i> <?php echo $this->lang->line('add'); ?></button>

            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div>


<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
        <h1 class="module-highlight"><span class="number">2.0</span> <?php echo $this->lang->line('module'); ?></h1>
        <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-home"></i> <?php echo $this->lang->line('home'); ?></a></li>
            <li><a href="#"><?php echo $this->lang->line('module'); ?></a></li>
            <li class="active"><?php echo $this->lang->line('updateSales'); ?></li>
        </ol>
    </section>
    <!-- Main content -->
    <section class="content">
        <form id="frm_submit_insert_sales" method="post" action="<?php echo base_url(); ?>sales/save_update_sale">
            
            <div class="row">
                <div class="col-md-12">
                    <!-- Horizontal Form -->

                    <!-- general form elements disabled -->
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            
							<label class="box-title"><?php echo $this->lang->line('newSales'); ?></label>
                            <div class="pull-right">
                                <button type="button"  data-toggle="tooltip" onclick="add_sale_row()" class="btn btn-sm bg-olive" title="<?php echo $this->lang->line('addnewsalerow') ?>"><span class="glyphicon glyphicon-plus"></span> <?php echo $this->lang->line('addnewsalerow'); ?></button>
                                <button id="btn_delete_sales" data-toggle="tooltip" disabled="disabled" type="button" onclick="delete_selected_row(this)" class="btn bg-red btn-sm" title="<?php echo $this->lang->line('deletenewsalerow') ?>"><span class="glyphicon glyphicon-trash"></span> <?php echo $this->lang->line('deletenewsalerow'); ?></button>
                                <a class="btn bg-orange btn-sm" href="<?php $id = ($sale_master) ? $sale_master->ID : '';
                                echo base_url() . 'sales/print_sale/' . $id; ?>" data-toggle="tooltip"><span class="glyphicon glyphicon-print"></span> <?php echo $this->lang->line('printSalesListing'); ?></a>

                            </div>
                        </div><!-- /.box-header -->
                        <div class="box-header with-border">
						<fieldset class="fsStyle">
                                <legend class="legendStyle">
                                    <a href="javascript:void(0);" data-toggle="collapse" data-target="#filter"><?php echo $this->lang->line('detail'); ?></a>
                                </legend>
                        	<!-- <h3>
            						<?php //echo $this->lang->line('updateSales'); ?> 
        						</h3> -->
                        	<div style="margin-top:10px;" class="row form-horizontal">
                                <div class="col-md-5">
                                    <div class="form-group">
                                        <label for="dateFrom" class="col-sm-5 control-label"><?php echo $this->lang->line('date'); ?>: </label>
                                        <div class="col-sm-7">
                                            <input type="hidden" name="journalNo" name="journalNo" value="<?php echo $journalNo; ?>"/>
                                            <input type="hidden" name="master_id" name="master_id" value="<?php echo ($sale_master) ? $sale_master->ID : ''; ?>"/>
                                            <input type="text" name="sale_date" value="<?php echo ($sale_master) ? date("d-m-Y", strtotime($sale_master->saleDate)) : ""; ?>" class="form-control datetimepicker validate[required]" id="dateFrom" placeholder="<?php echo $this->lang->line('date'); ?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="dateFrom" class="col-sm-5  control-label"><?php echo $this->lang->line('bank'); ?>: </label>
                                        <div class="col-sm-7">
                                            <select id="bank" name="bank" class="form-control col-sm-7 validate[required]" placeholder="<?php echo $this->lang->line('selectBank'); ?>">
                                                <option value=""><?php echo $this->lang->line('selectBank'); ?></option>
                                                <?php
                                                $selected = ($sale_master) ? $sale_master->bankID : -1;
                                                if (!empty($bankaccount)):
                                                    foreach ($bankaccount as $row):
                                                        ?>
                                                        <option <?php echo ($row->ID == $selected) ? "selected='selected'" : ''; ?> value="<?php echo $row->ID; ?>"><?php echo $row->bankName; ?></option>
                                                        <?php
                                                    endforeach;
                                                endif;
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="dateTo" class="col-sm-5 control-label"><?php echo $this->lang->line('formno'); ?>: </label>
                                        <div class="col-sm-7">
                                            <input type="text" disabled value="<?php echo ($sale_master) ? $sale_master->formNo : "" ?>" class="form-control" id="formNo" placeholder="<?php echo $this->lang->line('autogen'); ?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label for="dateFrom" class="col-sm-5 control-label"><?php echo $this->lang->line('typeofsale'); ?>: </label>
                                        <div class="col-sm-7">
                                            <select readonly id="typeofsale" name="typeofsale" class="form-control col-sm-7" placeholder="><?php echo $this->lang->line('typeofsale'); ?>">
                                                <?php
                                                if ($typeofsale):
                                                    foreach ($typeofsale as $row):
                                                        ?>
                                                        <option  value="<?php echo $row->ID; ?>"><?php echo $row->acctName; ?></option>
                                                        <?php
                                                    endforeach;
                                                endif;
                                                ?>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="box-body">
                            <table data-rowno="<?php echo ($sale_detail) ? sizeof($sale_detail) : 0; ?>" id="datatale_sale_list" class="table table-bordered table-striped datatables">
                                <thead>
                                    <tr>
                                        <th class="text-center"><?php echo $this->lang->line('no'); ?></th>
                                        <th><?php echo $this->lang->line('description'); ?></th>
                                        <th class="text-right"><?php echo $this->lang->line('amount'); ?></th>
                                        <th class="text-center"><input onchange="select_or_unselect_all(this)" type="checkbox" /></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    $i = 0;
                                    if ($sale_detail) {

                                        foreach ($sale_detail as $row) {
                                            ?>
                                            <tr>
                                                <td class="text-center"><div id="row_no_<?php echo ++$i; ?>"><?php echo $i; ?></div></td>
                                                <td><input type="hidden" value="<?php echo $row->description; ?>" name="sale_description[]" /><a onclick="edit_row(this)" href="javascript:"><?php echo $row->description; ?></a></td>
                                                <td class="text-right"><input type="hidden" value="<?php echo $row->amount; ?>" name="sale_amnt[]" /><?php echo number_format($row->amount, 2); ?></td>
                                                <td class="text-center"><input onchange="enabled_or_disabled_delete_btn(this)" value="<?php echo $row->ID; ?>" name="sales_id[]" type="checkbox" /></td>
                                            </tr>
                                            <?php
                                        }
                                    }
                                    ?>

                                </tbody>
                                <tfoot>
                                    <tr>
                                        <td colspan="4" class="text-right">
                                            <div class="form-inline">
                                                <div class="form-group">
                                                    <label><?php echo $this->lang->line('grandtotal'); ?> : </label>
                                                    <input readonly value="<?php echo number_format(($sale_master) ? $sale_master->grandTotal : 0, 2) ?>" type="text" class="form-control text-right" name="new_sale_grandtotal" id="new_sale_grandtotal" placeholder="0.00">
                                                </div>
                                            </div>
                                        </td>
                                    </tr>
                                </tfoot>
                            </table>

                        </div><!-- /.box-body -->
                    </div><!-- /.box -->
                </div>
            </div>
            <div class="row">
                <div class="box-body">
                    <div style="padding:10px;" class="col-md-12 text-right thumbnail">
                        <a href="<?php echo base_url(); ?>sales/saleslist" class="btn btn-primary"><?php echo $this->lang->line('gotosalelist'); ?></a>
                        <a href="<?php echo base_url() . 'report/journalDetails/' . $journalID; ?>" class="btn btn-primary "><?php echo $this->lang->line('journalentry'); ?></a>
                        <button type="button" onclick="verify_bebore_submit_update(this)" class="btn btn-primary"><?php echo $this->lang->line('update'); ?></button>
                    </div>
                </div>
            </div>
        </form>
    </section><!-- /.content -->
</div><!-- /.content-wrapper -->

