<section class="container collapse" id="tncInfo">
    <div class="jumbotron bg-white">
        <h6 class="text-danger">Firm Event Benefit</h6>
        <ul>
            <li>
                4.14.1 In recognition and appreciation of the efforts and cooperation of
                committee in carrying out office related events, Staff will be paid
                overtime allowance at RM10/hour during weekday/weekend (Pre
                Event). The Event Date allowance is not claimable
            </li>
            <li>
                4.14.2 All claims must be forwarded to the Human Resource Department on the normal claim procedure.
            </li>
        </ul>
        <small>Reference : <a href="http://salihin.noip.me/spserp/assets/uploads/hr/tnc/Claims__Allowances_-(Revision_2).pdf" target="_blank">Claims and Allowances (Revision no: 2) Effective date : 1st January 2018</a></small>
    </div>
</section>

<section class="container">
    <div class="card no-shadow">
        <div class="card-body">
            <div class="row">
                <div class="col">
                    <h2>Firm Event Benefit <small>(Submission <?php echo date('F-Y',strtotime('01-'.$month.'-'.$year)); ?>)</small> </h2>
                </div>
                <div class="col-2 text-right">
                    <button class="btn btn-sm btn-outline-dark" data-trigger="hover" data-tooltip="tooltip" title="" type="button" data-toggle="collapse" data-target="#tncInfo" data-original-title="Terms and Condition Reference">
                        <i class="fas fa-info"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
</section>

<section class="container mb-5">
    <div class="card no-shadow">
        <div class="card-body">

            <div class="row">
                <div class="col-lg-2">
                    <label><b>Date</b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text"  class="form-control claimdate" id="prep_date" />
                </div>
            </div>
            <br>


            <div class="row">
                <div class="col-lg-2">
                    <label><b>Event Name</b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control" id="event_name"/>
                </div>
                <div class="col-lg-2">
                    <label><b>Event Date</b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control claimdate" id="event_date" />
                </div>
            </div>
            <br>


            <div class="row">
                <div class="col-lg-2" >
                    <label><b>Start Time</b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control timepicker" id="event_start" >
                </div>
                <div class="col-lg-2">
                    <label><b>End Time</b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control timepicker" id="event_end" />
                </div>  
            </div>
            <br/>

            <div class="row">
                <div class="col">
                    <label><b>Attachments</b></label>
                    <div class="row">
                        <div class="col text-right pr-4">
                            <button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#mdl-attachment"><i class="fas fa-plus"></i></button>
                        </div>
                    </div>
                    <table class="table table-bordered" id="table-attachment">
                        <thead>
                            <tr>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                </div>
            </div>


            <div class="row">
                <div class="col text-right">
                    <button class="btn btn-success" onclick="createFirmEvent()"><i class="fas fa-save"></i>Save</button>
                </div>
            </div>


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

<!-- attachements modal -->
<div class="modal fade" id="mdl-attachment" tabindex="-1" role="dialog" >
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Uploading Attachments</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="row">
            <div class="col">
                <label><b>File</b></label>
                <input type="file" class="form-control" id="attachments_doc">
            </div>
        </div>
        <div class="row">
            <div class="col">
                <label><b>Description</b></label>
                <textarea cols="30" rows="10" class="form-control" id="attachment-description"></textarea>
            </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary" data-dismiss="modal" onclick="uploadAttachment()">Save changes</button>
      </div>
    </div>
  </div>
</div>
<!-- attachments modal -->

<script>
    var attachment      = [];

    var claim_year      = <?php echo $year; ?>; //2018
    var claim_month     = <?php echo $month; ?>; //8
    
    var date = new Date();
    var full_start_date = date.setFullYear(claim_year,claim_month-1 , 1);
    var start_claim     = moment(full_start_date).subtract(2,'months').startOf('month').format('DD-MM-YYYY');

    var full_end_date = date.setFullYear(claim_year,claim_month,1);
    var end_claim     = moment(full_end_date).subtract(2,'months').endOf('month').format('DD-MM-YYYY');

    $(document).ready(function(){
        $( ".claimdate" ).datepicker({
            uiLibrary: 'bootstrap4',
            format: 'dd-mm-yyyy',
            autoclose: true,
            changeMonth: true, 
            changeYear: true,
            startDate :start_claim,
            endDate:end_claim
        });
    });

    function uploadAttachment()
    {
        console.log('uploadin attachments');
        formData = new FormData();
        formData.append('files',$('#attachments_doc').prop('files')[0]);
        formData.append('description',$('#attachment-description').val());

        $.ajax({
            url: '<?php echo base_url('hr/claim/addAttachment/84/'); ?>',
            type: "POST",             
            data: formData, 
            contentType:false,       
            cache: false,             
            processData:false,
            dataType: 'JSON',
            success: function(data)
            {
                if(data.status == false)
                {
                    console.log('failed during upload');
                    console.log(data);

                    iziToast.error({
                        position: 'bottomRight',
                        title: 'Uploading Attachments Failed',
                        message: data[0].error,
                    });
                }else
                {
                    attachment.push(data);
                    console.log('current value for attachment : ');
                    console.log(attachment);
                    createTableAttachment();
                }
            },
            error:function(err){
                console.log('error');
                console.log(err);
            }
        });

    }

    function createTableAttachment()
    {
        var table = $('#table-attachment').DataTable();
        table.clear().draw();

        $.each(attachment,function(index,value){

            let action = `<button class="btn btn-danger btn-sm" onclick="removeAttachment(${index})">
                                <i class="fas fa-trash-alt"></i>
                            </button>`;
            table.row.add({
                0:value.orig_name,
                1:value.description,
                2:action
            }).draw();
        });

    }

    function removeAttachment(index)
    {   
        attachment.splice(index,1);

        createTableAttachment();
        console.log('attachment i d: '+index);
    }

    function createFirmEvent()
    {
        let required        = is_required();
        let have_attachment = is_attachment();

        if(required && have_attachment)
        {
            console.log('creating firm events');
        }
    }

    function is_required()
    {
        let date        = $('#prep_date').val();
        let event_name  = $('#event_name').val();
        let event_date  = $('#event_date').val();
        let start_time  = $('#event_start').val();
        let end_time    = $('#event_end').val();

        if(date && event_name && event_date && start_time && end_time){
            return true;
        }else{
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Date, Event Name, Event date, Start time and End Date are required',
            });
            return false;
        }
    }

    function is_attachment()
    {
        if(attachment.length > 0)
        {   
            return true;
        }else{
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Attachment is required',
            });
            return false;
        }
    }
</script>