<link rel="stylesheet" href="<?php echo base_url();?>assets/js/validationEngine.jquery.css" type="text/css" />
<link rel="stylesheet" href="<?php echo base_url();?>assets/css/cust_paginition.css" type="text/css" />
<div class="" id="addPromo">
    <div class="modal-dialog" style="width:1150px">
        <div class="modal-content">
            <!-- Modal heading -->
            <div class="modal-header" align = "left">
                <h3 class="modal-title" align="center">Add Campaign Details</h3>
            </div>
            <!-- // Modal heading END -->
            <!-- Modal body -->
            <div class="modal-body">
                <div class="innerAll">
                    <div class="innerLR">
                        <form class="form-horizontal" role="form" id = "add_promo" action = "<?php echo base_url(); ?>index.php/promotion/add_promotion" method = "post">
                            <?php $this->load->view('admin/flashdata_admin'); ?>
                            <div class="form-group mmar">
                                <label  class="col-sm-3 control-label text-center">Campaign Code: </label>
                                <div class="col-sm-3">
                                    <input type="text" id="campaign_code" class="form-control validate[required, maxSize[100], custom[onlyLetterNumber]] text-input" name = "promo_code" id="promo_code"/>
                                </div>
                                <div id="promo_code_block" class="formErrorContent"></div>

                            </div>
                            <div class="form-group mmar">
                                <label  class="col-sm-3 control-label text-center">Campaign Name: </label>
                                <div class="col-sm-9">
                                    <input type="text" id="campaign_name" class="form-control validate[required, maxSize[100]] text-input" value="" name = "promo_name"/>
                                </div>
                            </div>
                            <div class="form-group mmar">
                                <label  class="col-sm-3 control-label text-center">Description: </label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" id="campaign_desc" name = "desc"></textarea>
                                </div>
                            </div>
                            <div class="form-group mmar">
                                <div class="col-sm-15">
                                    <table class="table table-white table-vertical-center margin-none table-striped footable" id="list_new_magazines">
                                        <thead>
                                        <tr>
                                            <th  class="text-center">Magazine Code</th>
                                            <th  class="text-center">No Of year</th>
                                            <th  class="text-center">Amount</th>
                                            <th  class="text-center">Start Date</th>
                                            <th  class="text-center">End Date</th>
                                            <th  class="text-center">Start Issue</th>
                                            <th  class="text-center">End Issue</th>
                                            <th  class="text-center">Total Issue</th>
                                            <th  class="text-center">Action</th>

                                        </tr>
                                        </thead>
                                        <tbody>
                                        <?php
                                        $intCounter = 1;
                                        foreach($magazines_details as $details) {?>
                                            <tr id="row<?php echo $intCounter;?>">
                                                <td class="text-center" >
                                                    <input type="text" class="form-control" name="magezine_code[]" value="<?php echo $details['magazine_code'];?>" readonly/>
                                                </td>
                                                <td class="text-center">
                                                    <input type="text" class="form-control" name="camp_no_of_year[]" value="<?php echo $details['no_of_year'];?>" readonly/>
                                                </td>
                                                <td class="text-center">
                                                    <input type="text" class="form-control" name="camp_amount[]" value="<?php echo $details['amount'];?>" readonly/>
                                                </td>
                                                <td class="text-center" >
                                                    <input type="text" class="form-control" name="camp_start_date[]" data-date-format="d M yyyy" value="<?php echo date("d M Y",strtotime($details['start_date']));?>" readonly/>
                                                </td>
                                                <td class="text-center">
                                                    <input type="text" class="form-control" name="camp_end_end[]" data-date-format="d M yyyy" value="<?php echo date("d M Y",strtotime($details['end_date']));?>" readonly/>
                                                </td>
                                                <td class="text-center" >
                                                    <input type="text" class="form-control" name="camp_start_issue[]" value="<?php echo $details['start_issue'];?>" readonly/>
                                                </td>
                                                <td class="text-center">
                                                    <input type="text" class="form-control" name="camp_end_issue[]" value="<?php echo $details['end_issue'];?>" readonly/>
                                                </td>
                                                <td class="text-center">
                                                    <input type="text" class="form-control" name="camp_total_issue[]" value="<?php echo $details['no_of_issue'];?>" readonly/>
                                                </td>
                                                <td class='text-center'>
                                                    <input type='button' onClick="$(this).closest('tr').remove()" value='Delete'>
                                                </td>
                                                <td class='text-center'>
                                                    <a href="#update_camp_details" data-toggle="modal" onClick="return fnGetDetails(<?php echo $details['id']; ?>, <?php echo $promotion['id']; ?>, <?php echo $intCounter; ?>)"><input type='button'  value='Edit'></a>
                                                </td>
                                            </tr>
                                            <?php $intCounter++; }?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                            <div id="promo_issue_block" class="formErrorContent" align="center"></div>
                            <a href="#nnn" id="add_new_Magazine" data-toggle="modal" onclick="return add_new_promotion();"><i class="fa fa-fw fa-plus"></i>Add Magazine to campaign</a>
                            <input type="hidden" id="num_mag" value="<?php echo $intCounter; ?>" />
                            <div class="form-group">
                                <div class="col-sm-offset-4 col-sm-8">
                                    <input type="hidden" id="system_base_url" value="<?php echo base_url();?>">
                                    <input type="hidden" id="previous_id" value="">
                                    <input type="hidden" name="promotion_id" value="<?php echo base64_encode($promotion['id']); ?>">
                                    <button type="submit" class="btn btn-primary" id="add_promo">Submit</button>
                                    <a class="btn" href="<?php echo base_url(); ?>index.php/promotion/promotion_list"> <button type="button" class="btn btn-primary">Cancel</button></a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            <!-- // Modal body END -->
        </div>
    </div>
</div>

<input type="hidden" id="system_base_url" value="<?php echo base_url();?>" />
<div class="modal fade" id="nnn" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content" id="tran_preview2">

        </div>
    </div>
</div>
</div>


<div class="modal fade" id="update_camp_details" data-backdrop="static">
    <div class="modal-dialog">
        <div class="modal-content" id="update_camp">

        </div>
    </div>
</div>
</div>
<script src="<?php echo base_url();?>assets/js/main.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>assets/js/promotion.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>assets/plugins/forms_elements_multiselect/js/jquery.multi-select.js?v=v2.0.0-rc1&sv=v0.0.1.2"></script>
<script src="<?php echo base_url();?>assets/plugins/forms_elements_bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<script src="<?php echo base_url();?>assets/js/jquery.validationEngine-en.js" type="text/javascript"></script>
<script src="<?php echo base_url();?>assets/js/jquery.validationEngine.js" type="text/javascript"></script>


<script>
    $(document).ready(function() {
        $("#add_promo").validationEngine();
        $("#make_download").hide();

        $("#archived_check").click(function(){
            if($("#archived_check").val()) {
                $("#make_download").show()
            }
        });

        $("#unarchived_check").click(function(){
            $("#make_download").hide()
        });
    });

    // Function to add new campaign. It will lead all validations before the adding it.
    function add_new_promotion()
    {
        var NumberOfYears = $("#campaign_no_years").val();
        if (NumberOfYears == "") {
            alert("Please select number of years to proceed");
            location.reload();
        }
        var arrMagazines = new Array();
        $("input[name^='magezine_code[]']").each(function(index, element){
            arrMagazines.push($(element).val());
        });
        var strIds = arrMagazines.join();
        var system_base_url = $("#system_base_url").val();
        var html = "<div align='center'><img src='"+system_base_url+"assets/images/loader.gif'/></div>";
        $("#tran_preview2").html(html);
        $.ajax({
            url:"<?php echo base_url(); ?>index.php/promotion/fnAddRemainingMagazine",
            'data' : {
                'prev_ids' :strIds,
                'no_of_years': NumberOfYears
            },
            'type' : 'GET',
            success:function(result){
                $("#tran_preview2").html(result);
            }
        });
    }


    //Function to get the campaigns details with the input of Row id, Campaign Id
    function fnGetDetails(intCampMagId, intCampId, intRowId)
    {
        $.ajax({
            url:"<?php echo base_url(); ?>index.php/promotion/fnGetCampaignDetails",
            'data' : {
                'prev_ids' :intCampMagId,
                'campaign_id' :intCampId,
                'row_id':intRowId
            },
            'type' : 'GET',
            success:function(result){
                $("#update_camp").html(result);
            }
        });
    }
</script>