<section class="container collapse" id="tncInfo">
    <div class="jumbotron bg-white">
        <h6 class="text-danger">Subsistence Allowance</h6>
        <b>Local Travel</b>
        <ul>
            <li>
                5.5.1 A staff on overnight outstation duty at least hundred(100) km away
                from his normal place of work shall be paid subsistence allowance
                as follows: 
                
                <br><br/>
                <h6>Contract and Permanent Staff</h6>
                <hr>
                <label><b>(a) West Malaysia</b></label>
                <table class="table-bordered" width="100%">
                    <thead>
                        <tr>
                            <th>Meal Allowance</th>
                            <th>Breakfast (RM)</th>
                            <th>Lunch (RM)</th>
                            <th>Dinner (RM)</th>
                            <th>Total (RM)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td>5</td>
                            <td>10</td>
                            <td>5</td>
                            <td>20</td>
                        </tr>
                    </tbody>
                </table>
                <br/>
                <table class="table-bordered" width="100%">
                    <tbody>
                        <tr>
                            <td>Outstation Allowance</td>
                            <td>RM30.00 per day</td>
                        </tr>
                    </tbody>
                </table>
                <label><b>(b) East Malaysia (Sabah and Sarawak)</b></label>
                <table class="table-bordered" width="100%">
                    <thead>
                        <tr>
                            <th>Meal Allowance</th>
                            <th>Breakfast (RM)</th>
                            <th>Lunch (RM)</th>
                            <th>Dinner (RM)</th>
                            <th>Total (RM)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td>10</td>
                            <td>10</td>
                            <td>10</td>
                            <td>30</td>
                        </tr>
                    </tbody>
                </table>
                <br/>
                <table class="table-bordered" width="100%">
                    <tbody>
                        <tr>
                            <td>Outstation ALlowance</td>
                            <td>RM30.00 per day</td>
                        </tr>
                    </tbody>
                </table>

                <br><br/>
                <h6>Trainee</h6>
                <hr>
                <label><b>(a) West Malaysia</b></label>
                <table class="table-bordered" width="100%">
                    <thead>
                        <tr>
                            <th>Meal Allowance</th>
                            <th>Breakfast (RM)</th>
                            <th>Lunch (RM)</th>
                            <th>Dinner (RM)</th>
                            <th>Total (RM)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td>5</td>
                            <td>5</td>
                            <td>-</td>
                            <td>10</td>
                        </tr>
                    </tbody>
                </table>
                <br/>
                <table class="table-bordered" width="100%">
                    <tbody>
                        <tr>
                            <td>Outstation Allowance</td>
                            <td>RM15.00 per day</td>
                        </tr>
                    </tbody>
                </table>
                <label><b>(b) East Malaysia (Sabah and Sarawak)</b></label>
                <table class="table-bordered" width="100%">
                    <thead>
                        <tr>
                            <th>Meal Allowance</th>
                            <th>Breakfast (RM)</th>
                            <th>Lunch (RM)</th>
                            <th>Dinner (RM)</th>
                            <th>Total (RM)</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td></td>
                            <td>5</td>
                            <td>5</td>
                            <td>5</td>
                            <td>15</td>
                        </tr>
                    </tbody>
                </table>
                <br/>
                <table class="table-bordered" width="100%">
                    <tbody>
                        <tr>
                            <td>Outstation ALlowance</td>
                            <td>RM15.00 per day</td>
                        </tr>
                    </tbody>
                </table>

            </li>
            <li>
                5.5.2 The above allowance shall be calculated based on number of days
                commencing from the time of departure of the staff either from his
                home or office wherever is nearer.
            </li>
            <li>
                5.5.3 A staff whose outstation duty exceed eight (8) hours and at least
                hundred (100) km from his normal place of work but does not
                require an overnight stay will be paid half of the daily subsistence
                allowance
            </li>
            <li>
                5.5.4 For a staff whose outstation duties are less than eight (8) hours,
                and does not require an overnight stay will not be eligible for
                subsistence allowance irrespective of distance.
            </li>
            <li>
                5.5.5 SALIHIN will deduct the respective meal allowance of the
                employee is being entertained or claiming for the entertainment
                meal are being or provided by the Hotel and it is also applicable to
                employees who are attending courses where the meals are provided
                as part of the course
            </li>
        </ul>
        <small>Reference : <a href="http://salihin.noip.me/spserp/assets/uploads/hr/tnc/Travel_-_(Revision_2).pdf" target="_blank">Local and Business Travel (Revision no: 2) Effective date : 1st January 2018</a></small>
        <br>
        <b>International Travel</b>
        <ul>
            <li>5.10.8 The quotation travel allowance shall be payable only if the outstation travel involves overnight stay or if the duration of the trip is more than eight hours from the time of departure to time arrical at state/country of workplace</li>
            <li>Based on destinations of travel and Job Grades, Table as below: 
                <table class="table-bordered" width="100%">
                    <thead>
                        <tr>
                            <th>Countries</th>
                            <th>17 and Above</th>
                            <th>16 and Below</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>Singapore, Philipines and Brunei</td>
                            <td>USD 45</td>
                            <td>USD 35</td>
                        </tr>
                        <tr>
                            <td>Indonesia, Thailand, Vietnam and Myanmar</td>
                            <td>USD 35</td>
                            <td>USD 25</td>
                        </tr>
                        <tr>
                            <td>Japan</td>
                            <td>USD 80</td>
                            <td>USD 70</td>
                        </tr>
                        <tr>
                            <td>USA, UK, Canada, Europe, and Northern Ireland</td>
                            <td>USD 70</td>
                            <td>USD 60</td>
                        </tr>
                        <tr>
                            <td>China, Hong Kong, Taiwan and Korea</td>
                            <td>USD 50</td>
                            <td>USD 40</td>
                        </tr>
                        <tr>
                            <td>India, Pakisatan and Sri Lanka</td>
                            <td>USD 40</td>
                            <td>USD 30</td>
                        </tr>
                        <tr>
                            <td>Australia and New Zealand</td>
                            <td>USD 60</td>
                            <td>USD 50</td>
                        </tr>
                        <tr>
                            <td>South Africa and Other African Country</td>
                            <td>USD 45</td>
                            <td>USD 35</td>
                        </tr>
                        <tr>
                            <td>Latin American Countries</td>
                            <td>USD 40</td>
                            <td>USD 30</td>
                        </tr>
                        <tr>
                            <td>Middle East Countries</td>
                            <td>USD 58</td>
                            <td>USD 48</td>
                        </tr>
                    </tbody>
                </table>
            </li>
        </ul>
        <small>Reference : <a href="http://salihin.noip.me/spserp/assets/uploads/hr/tnc/Travel_-_(Revision_2).pdf" target="_blank">Local and Business Travel (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>Subsistence Allowance <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>

<?php $this->load->view('hr/claim/page/traveling/type'); ?>

<section class="container mb-5">

    <div class="card d-none" id="option_local">
        <div class="card-body">
            <div class="row" >
                <div class="col">

                    <div class="row">
                        <div class="col-lg-2">
                            <label><b>Date From <span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control claimdate" id="date_from_local" placeholder="Enter starting travelling date (<?php echo date('d-m-Y'); ?>)" />
                        </div>
                        <div class="col-lg-2">
                            <label><b>Date To <span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" class="form-control claimdate" id="date_to_local" placeholder="Enter ending travelling date (<?php echo date('d-m-Y'); ?>)" >
                        </div>
                    </div>
                    <br>

                    <div class="row">
                        <div class="col-lg-2">
                            <label><b>Location <span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col-lg-4">
                            <select class="form-control" id="location_local">
                                <?php foreach($local_zone as $key => $value): ?>
                                    <?php
                                        $meals      = $value->param_array['breakfast'] + $value->param_array['lunch'] + $value->param_array['dinner'];
                                        $allowance  =  $value->param_array['outstation'];
                                    ?>
                                    <option value="<?php echo $value->zone_claim_type_id; ?>" data-allowance="<?php echo $allowance; ?>" data-meals="<?php echo $meals; ?>" ><?php echo $value->zone; ?></option>
                                <?php endforeach;?>
                            </select>
                        </div>

                        <div class="col-lg-2">
                            <label><b>Overnight Status <span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col-lg-4">
                            <select class="form-control" id="overnightStatus">
                                <option value="1">Overnight</option>
                                <option value="2">Not Overnight</option>
                            </select>
                        </div>  
                    </div>
                    <br>

                    <div class="row">
                        <div class="col-lg-2">
                            <label><b>Outstation Allowance<span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col-lg-4">
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">
                                        RM
                                    </span>
                                </div>
                                <input type="text" class="form-control bg-white" value="0" id="amount_local">
                            </div>                            
                            <small>Amount = <span id="day_local"></span> days * <span id="allowance_local"></span> (outstation allowance) / <?php echo $divide; ?> (position)  / <span id="local_allowance_overnight">1</span> (overnight status)</small>
                        </div>

                        <div class="col-lg-2">
                            <label><b>Meals Allowance<span class="text-danger">*</span></b></label>
                        </div>
                        <div class="col">
                            
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <span class="input-group-text">
                                        RM
                                    </span>
                                </div>
                                <input type="text" class="form-control bg-white" value="0" id="amount_meal_local">
                            </div>
                            <small>Amount = <span id="day_local_meals"></span> days * <span id="meal_local_allowance"></span> (meals allowance) / <?php echo $divide; ?> (position) / <span id="local_meals_overnight">1</span> (overnight status)</small>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col">
                            <label><b>Staff movements</b></label>
                            <div class="row">
                                <div class="col text-right pr-4">
                                    <!-- <button class="btn btn-primary btn-sm"><i class="fas fa-sync-alt"></i></button> -->
                                </div>
                            </div>
                            <table class="table table-bordered" id="table-local">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <!-- <th>Movement Type</th> -->
                                        <th>Client</th>
                                        <th>Location</th>
                                        <th>Hours</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="createSubsistence()" id="saveLocalBtn"><i class="fas fa-save"></i> Save</button>
                        </div>
                    </div>

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

    <div class="card d-none" id="option_international">
        <div class="card-body">

            <div class="row">
                <div class="col-lg-2">
                    <label><b>Date From <span class="text-danger">*</span></b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control claimdate" id="date_from_international" placeholder="Enter starting travelling date (<?php echo date('d-m-Y'); ?>)" />
                </div>
                <div class="col-lg-2">
                    <label><b>Date To <span class="text-danger">*</span></b></label>
                </div>
                <div class="col-lg-4">
                    <input type="text" class="form-control claimdate" id="date_to_international" placeholder="Enter ending travelling date (<?php echo date('d-m-Y'); ?>)" >
                </div>
            </div>

            <br>
            <div class="row">
                <div class="col-lg-2">
                    <label><b>Location <span class="text-danger">*</span></b></label>
                </div>
                <div class="col-lg-4">
                    <select class="form-control" id="location_international">
                        <?php foreach($international_zone as $key => $value): ?>
                            <option value="<?php echo $value->zone_claim_type_id; ?>" data-above="<?php echo $value->param_array['grade_above_17_USD'];?>" data-below="<?php echo $value->param_array['grade_below_16_USD']; ?>" ><?php echo $value->zone; ?></option>
                        <?php endforeach;?>
                    </select>
                </div>


                <div class="col-lg-2">
                    <label><b>Amount <span class="text-danger">*</span></b></label>
                </div>
                <div class="col-lg-4">
                    <div class="input-group mb-3">
                        <div class="input-group-prepend">
                            <span class="input-group-text">
                                RM
                            </span>
                        </div>
                        <input type="text" class="form-control bg-white" value="0" id="amount_international" >
                    </div>

                    <small>Amount = <span id="day_international"></span> Days * <span id="allowance_international"></span> subsistence allowance * <span id="myr_rate_international">RM<?php echo $usd_to_myr; ?></span> conversion rate USD to MYR</small>
                </div>

                
            </div>

            <div class="row">
                <div class="col">
                    <label><b>Staff movements</b></label>
                    <div class="row">
                        <div class="col text-right pr-4">
                            <!-- <button class="btn btn-primary btn-sm"><i class="fas fa-sync-alt"></i></button> -->
                        </div>
                    </div>
                    <table class="table table-bordered" id="table-international">
                        <thead>
                            <tr>
                                <th>Date</th>
                                <th>Movement Type</th>
                                <th>Client</th>
                                <th>Location</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="createSubsistenceInternational()" id="saveInternationalBtn"><i class="fas fa-save"></i> Save</button>
            </div>
        </div>


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

<script>
    var staff_movement = [];
    var staff_movement_international = [];

    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()
    {
        if(claim_year == 2020 && claim_month == 2)
        {
            console.log('submission bulan dua claim');
            start_claim = '01-01-2020';
            end_claim = '31-01-2020';
        }


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

        /**
            - local
         */
        $('#date_from_local').change(function()
            {
                staff_movement = [];
                let date_from       = $('#date_from_local').val();
                let date_to         = $('#date_to_local').val();

                let from_is_valid   = moment(date_from,'DD-MM-YYYY');
                let to_is_valid     = moment(date_to,'DD-MM-YYYY');

                if(from_is_valid.isValid() && to_is_valid.isValid())
                {
                    if(date_from && date_to)
                    {
                        fetchMovement(date_from,date_to);
                    }
                }else{
                    iziToast.destroy();
                    iziToast.error({
                        title: 'Error',
                        message: 'Please enter a valid date in both date fields'
                    });
                }

                createTable();
            }
        );

        $('#date_to_local').change(function()
            {
                staff_movement = [];
                let date_from       = $('#date_from_local').val();
                let date_to         = $('#date_to_local').val();
                
                let from_is_valid   = moment(date_from,'DD-MM-YYYY');
                let to_is_valid     = moment(date_to,'DD-MM-YYYY');

                if(from_is_valid.isValid() && to_is_valid.isValid())
                {
                    if(date_from && date_to)
                    {                    
                        fetchMovement(date_from,date_to);
                    } 
                }else{
                    iziToast.destroy();
                    iziToast.error({
                        title: 'Error',
                        message: 'Please enter a valid date in both date fields'
                    });
                }

                createTable();
            }
        );


        $('#location_local').change(function()
        {
            let date_from   = $('#date_from_local').val();
            let date_to     = $('#date_to_local').val();
            if(date_from && date_to)
            {                    
                calculateLocalAmount();
            }
        });


        /**
        - international 
         */
        $('#date_from_international').change(function()
            {
                let date_from   = $('#date_from_international').val();
                let date_to     = $('#date_to_international').val();

                let from_is_valid   = moment(date_from,'DD-MM-YYYY');
                let to_is_valid     = moment(date_to,'DD-MM-YYYY');

                if(from_is_valid.isValid() && to_is_valid.isValid())
                {
                    if(date_from && date_to)
                    {
                        if(fetchMovementInternational(date_from,date_to));
                    }
                }else
                {
                    iziToast.destroy();
                    iziToast.error({
                        title: 'Error',
                        message: 'Please enter a valid date in both date fields.'
                    });
                }

                
            }
        );

        $('#date_to_international').change(function()
            {
                let date_from       = $('#date_from_international').val();
                let date_to         = $('#date_to_international').val();
                let from_is_valid   = moment(date_from,'DD-MM-YYYY');
                let to_is_valid     = moment(date_to,'DD-MM-YYYY'); 

                if(from_is_valid.isValid() && to_is_valid.isValid())
                {
                    if(date_from && date_to)
                    {
                        if(fetchMovementInternational(date_from,date_to));
                    }
                }else{
                    iziToast.destroy();
                    iziToast.error({
                        title: 'Error',
                        message: 'Please enter a valid date in both date fields.'
                    });
                }
                
            }
        );

        $('#location_international').change(function()
            {
                let date_from   = $('#date_from_international').val();
                let date_to     = $('#date_to_international').val();
                if(date_from && date_to)
                {
                    calculateInternationalAmount();
                }   
            }
        );

        $('#overnightStatus').change(function(){
            let divider = $('#overnightStatus').val();

            $('#local_allowance_overnight').html(divider);
            $('#local_meals_overnight').html(divider);
            calculateLocalAmount();
        });
    });

    /**
        - local
     */
    function fetchMovement(date_from,date_to)
    {
        if(date_from && date_to)
        {
            let url = '<?php echo base_url('hr/api_movement/getEightHourUserMovementBetweenDate/'); ?>';
            let data = 
            {
                token:'<?php echo $token; ?>',
                date_from:date_from,
                date_to:date_to,
                user_id:<?php echo $user_id; ?>,
            }

            $.ajax({
                url: url,
                type: 'POST',
                data:data,
                dataType: 'JSON',
                success: function(data)
                {

                    console.log(data);

                    if(data.results)
                    {
                        console.log(data.results);
                        staff_movement = data.results;
                    }else{
                        staff_movement = [];
                    }
                    createTable();
                    calculateLocalAmount();
                },error:function(err)
                {
                    return false;
                }
            });
        
        return true;
        }
    }

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

        $.each(staff_movement,function(index,value){
            table.row.add({
                0:moment(value.movement_date).format('DD/MM/YYYY'),
                1:value.client,
                2:value.destination,
                3:value.hours,
                4:'<button class="btn btn-danger btn-sm" onclick="removeStaffMovement('+index+')" ><i class="fas fa-trash-alt"></i></button>',
            }).draw();
        });
    }

    function removeStaffMovement(index)
    {
        staff_movement.splice(index,1);
        createTable();
        calculateLocalAmount();
    }

    function calculateLocalAmount()
    {
        var date_from   = moment($('#date_from_local').val(),'DD-MM-YYYY');
        var date_to     = moment($('#date_to_local').val(),'DD-MM-YYYY');
        // var duration    = moment.duration(date_to.diff(date_from)).asDays()+1;
        
        let overnight = $('#overnightStatus').val(); 

        let travel_dates = new Array();
        $.each(staff_movement,function(index,movement){
            if(travel_dates.length == 0)
            {
                travel_dates.push(movement.movement_date);
            }else{
                let a = true;
                $.each(travel_dates,function(ind,dte){
                    if(dte == movement.movement_date)
                    {
                        a = false;
                    }
                });

                if(a == true)
                {
                    travel_dates.push(movement.movement_date);
                }
            }

        });        

        let duration = travel_dates.length;

        $('#day_local').text(duration);
        $('#day_local_meals').text(duration);

        var subsistence_allowance   = $('#location_local option:selected').data('allowance');
        var meals_allowance         = $('#location_local option:selected').data('meals');
        $('#allowance_local').text('RM'+subsistence_allowance);
        $('#amount_local').val(duration * subsistence_allowance /  <?php echo $divide; ?> / overnight );
        $('#meal_local_allowance').text('RM'+meals_allowance);
        $('#amount_meal_local').val(duration * meals_allowance / <?php echo $divide; ?> / overnight);
    }


    /**
    create subsistence local
     */
    function createSubsistence()
    {
        let input_fills         = is_requiredFills();
        let have_movement       = is_staffMovement();

        if(input_fills && have_movement)
        {
            $('#saveLocalBtn').attr('disabled',true);
            let url         = '<?php echo base_url('hr/api/claim/createSubsistence/'); ?>';
            let type        = 'POST';
            let data        =   {
                                    token:'<?php echo $token; ?>',
                                    date_from:$('#date_from_local').val(),
                                    date_to:$('#date_to_local').val(),
                                    location:$('#location_local').val(),
                                    type:0,
                                    staff_movements: staff_movement,
                                    claim_id: <?php echo $claim_id; ?>,
                                    outstation_allowance: $('#amount_local').val(),
                                    meal_allowance:$('#amount_meal_local').val(),
                                }

            let redirect   = '<?php echo base_url('hr/claim/myClaimDetail/'.$year.'/'.$month.'/'.$user_id.'/'.$claim_id); ?>';
            // let redirect = null;
            ajxRequest(url, type, data, redirect);
        }
    }

    function is_requiredFills()
    {
        let date_from   = $('#date_from_local').val();
        let date_to     = $('#date_to_local').val();
        let location    = $('#location_local').val();

        if(date_from && date_to && location)
        {
            return true;
        }else{
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Date from , date to, location are required',
            });

            return false;
        }
    }

    function is_staffMovement()
    {

        if(staff_movement.length > 0)
        {
            return true;
        }else{
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Unable to find any movement',
            });
            return false;
        }

    }





    /**
        - international 
     */
    function fetchMovementInternational(date_from,date_to)
    {
        if(date_from && date_to)
        {
            let url = '<?php echo base_url('hr/api_movement/getUserMovementBetweenDate/'); ?>';
            let data = 
            {
                token:'<?php echo $token; ?>',
                date_from:date_from,
                date_to:date_to,
                user_id:<?php echo $user_id; ?>,
            }

            $.ajax({
                url: url,
                type: 'POST',
                data:data,
                dataType: 'JSON',
                success: function(data)
                {
                    if(data.results){
                        staff_movement_international = data.results;
                    }else{
                        staff_movement_international = [];
                    }
                    createTableInternational();
                    calculateInternationalAmount();

                },error:function(err)
                {
                    console.log('Error : ');
                    console.log(err.responseText);
                    return false;
                }
            });

            return true;
        }
        return false;
    }

    function createTableInternational()
    {
        // console.log(staff_movement_international);
        var table   = $('#table-international').DataTable();
        table.clear().draw();

        $.each(staff_movement_international,function(index,value){
            table.row.add({
                0:moment(value.movement_date).format('DD/MM/YYYY'),
                1:value.movement_type,
                2:value.client,
                3:value.destination,
                4:'<button class="btn btn-danger btn-sm" onclick="removeStaffMovementInternational('+index+')" ><i class="fas fa-trash-alt"></i></button>',
            }).draw();
        });
    }

    function removeStaffMovementInternational(index)
    {
        staff_movement_international.splice(index,1);
        createTableInternational();
    }

    function calculateInternationalAmount()
    {
        var date_from   = moment($('#date_from_international').val(),'DD-MM-YYYY');
        var date_to     = moment($('#date_to_international').val(),'DD-MM-YYYY');
        // var duration = moment.duration(date_to.diff(date_from)).asDays()+1;

        let travel_dates = new Array();

        $.each(staff_movement_international,function(index,movement){
            if(travel_dates.length == 0)
            {
                travel_dates.push(movement.movement_date);
            }else{
                let a = true;
                $.each(travel_dates,function(ind,dte){
                    if(dte == movement.movement_date)
                    {
                        a = false;
                    }
                });

                if(a == true)
                {
                    travel_dates.push(movement.movement_date);
                }
            }
        });    

        duration = travel_dates.length;

        $('#day_international').text(duration);
        var grade = <?php echo (!empty($employee_grade))?$employee_grade:0; ?>
        
        if(grade >= 17)
        {
            $allowance = $('#location_international option:selected').data('above');
        }else{
            $allowance = $('#location_international option:selected').data('below');
        }
        $('#allowance_international').text('USD '+$allowance);

        var amount = duration * $allowance * <?php echo $usd_to_myr; ?>;
        $('#amount_international').val(amount.toFixed(2));
    }

    /**
    farouq
     */
    function createSubsistenceInternational()
    {
        let required_fills  = is_requiredFillsInternational();
        let have_movement   = is_staffMovementInternational();

        if(required_fills && have_movement)
        {
            $('#saveInternationalBtn').attr('disabled',true);
            
            let url         = '<?php echo base_url('hr/api/claim/createSubsistence/'); ?>';
            let type        = 'POST';
            let data        =   {
                                    token:'<?php echo $token; ?>',
                                    date_from:$('#date_from_international').val(),
                                    date_to:$('#date_to_international').val(),
                                    location:$('#location_international').val(),
                                    type:1,
                                    staff_movements: staff_movement_international,
                                    claim_id: <?php echo $claim_id; ?>,
                                    amount: $('#amount_international').val(),
                                }

            let redirect   = '<?php echo base_url('hr/claim/myClaimDetail/'.$year.'/'.$month.'/'.$user_id.'/'.$claim_id); ?>';
            // let redirect = null;
            ajxRequest(url, type, data, redirect);
        }
    }

    function is_requiredFillsInternational()
    {
        let date_from_international         = $('#date_from_international').val();
        let date_to_international           = $('#date_to_international').val();
        let location_international          = $('#location_international').val();

        if(date_from_international && date_to_international && location_international)
        {
            return true;
        }else
        {
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Date from, Date to nad Location are required',
            });
            return false;
        }
    }

    function is_staffMovementInternational()
    {
        if(staff_movement_international.length > 0)
        {
            return true;
        }else{
            iziToast.error({
                position: 'bottomRight',
                title: 'Failed',
                message: 'Unable to find any movement',
            });
            return false;
        }
        console.log('staff movement internatopanal ');
    }
</script>