<div class="collapse" id="collapseStatutory">
	<div class="card card-body">

        <div class="container">
            <div class="row">
                <h4 class="card-title float-left">Statutory Details</h4>
                <?php if(strtotime(date('d-m-Y')) <= $end_date){ ?>    
                    <div class="btn-group ml-auto float-right">
                        <button type="button" class="btn btn-primary btn-sm d-print-none <?php echo $btn_hidden; ?>" style="margin-bottom:15px; width: 200px;" data-content="<?php echo base64_encode(json_encode($statutory_details[0]));?>" onclick="openStatutoryModal(this);" data-trigger="hover" data-tooltip="tooltip" title="Add New Statutory Details">
                            <i class="fa fa-plus"></i> Add New
                        </button>
                    </div>
                <?php } ?>
            </div>
        </div>

        <hr>

        <div class="table-responsive">   
            <table id="table_statutory" class="table table-hover table-bordered">
                <thead>
                    <tr>
                        <th>Details</th>
                        <th>Created At</th>
                        <?php if($btn_hidden == ''){ ?> 
                            <th class="d-print-none">Action</th>
                        <?php } ?>
                    </tr>

                </thead>
                <tbody>
                    <?php if($statutory_details) foreach ($statutory_details as $key => $statutory): if($key == (count($statutory_details) - 1)) : ?> 
                    <tr class="link-row" data-id="" >
                        <td>
                            <b>Employee EPF Number : </b><?php echo (!empty($statutory->ee_epf_no))?$statutory->ee_epf_no:'N/A'; ?>
                            <br><b>Employee SOCSO No : </b><?php echo (!empty($statutory->ee_socso))?$statutory->ee_socso:'N/A'; ?>
                            <br><b>Income Tax No : </b><?php echo (!empty($statutory->ee_income_tax))?$statutory->ee_income_tax:'N/A'; ?>
                            <br><b>PCB Deduction (percentage) : </b><?php echo ($statutory->ee_pcb_deduction_percent > 0)?'('.($statutory->ee_pcb_deduction_percent).'%)':'N/A'; ?>
                            <br><b>Zakat Deduction (percentage) : </b><?php echo ($statutory->ee_zakat_deduction_percent > 0)?'('.($statutory->ee_zakat_deduction_percent).'%)':'N/A'; ?>
                            <br><b>Employer Rate (percentage) : </b>
                            <?php 
                            switch ($statutory->er_rate) {
                                case 0:
                                echo 'Statutory Rate - ('.$statutory->er_percent.'%)';
                                break;
                                case 1:
                                echo 'Company Rate - ('.$statutory->er_percent.'%)';
                                break;
                                case 2:
                                echo 'Personal Rate - ('.$statutory->er_percent.'%)';
                                break;
                                default:
                                break;
                            }
                            ?>
                            <br><b>Employee Rate (percentage) : </b>
                            <?php 
                            switch ($statutory->ee_rate) {
                                case 0:
                                echo 'Statutory Rate - ('.$statutory->ee_percent.'%)';
                                break;
                                case 1:
                                echo 'Company Rate - ('.$statutory->ee_percent.'%)';
                                break;
                                case 2:
                                echo 'Personal Rate - ('.$statutory->ee_percent.'%)';
                                break;
                                default:
                                break;
                            }
                            ?>
                            <br><b>Status : </b>
                            <?php echo (empty($statutory->status))?'INACTIVE':'ACTIVE'; ?>
                            <br><b>Year : </b>
                            <?php echo (!empty($statutory->year))?$statutory->year:'N/A'; ?>
                        </td>
                        <td><?php echo $statutory->created_at; ?></td>
                        <?php if($btn_hidden == ''){ ?> 
                            <?php if(strtolower($statutory->action_type) == 'personal data update' && (date('m-Y',strtotime($statutory->created_at)) == date('m-Y', $live_date)) && (strtotime(date('d-m-Y')) <= $end_date)){ ?>                      
                                <td class="text-center">
                                    <div class="btn-group">
                                        <button class="btn btn-success btn-sm" data-toggle="modal" data-target="#mdl-view-statutory" onclick="populateModalStatutory(this);" data-content="<?php echo base64_encode(json_encode($statutory));?>"><i class="fas fa-edit" ></i></button>
                                        <button class="btn btn-danger btn-sm" onclick="removeStatutoryDetails(<?php echo $statutory->id; ?>)"><i class="fas fa-trash" ></i></button>
                                    </div>
                                </td> 
                            <?php } else { ?>
                                <td class="text-center">
                                    <div class="btn-group" role="group">
                                        <button class="btn btn-success btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to update"><i class="fas fa-edit" ></i></button>
                                        <button class="btn btn-danger btn-sm" disabled style="cursor: not-allowed;" data-trigger="hover" data-tooltip="tooltip" title="Not allowed to delete"><i class="fas fa-trash-alt"></i></button>
                                    </div>
                                </td> 
                            <?php } ?>
                        <?php } ?>
                    </tr>
                    <?php endif; endforeach; ?>
                </tbody>
            </table>
        </div>				
    </div><br>  
</div>

<div class="modal fade" id="mdl-view-statutory" tabindex="-1" role="dialog" >
    <div class="modal-dialog" role="document" style="max-width:60%;">
        <div class="modal-content">
            <form id="statutoryForm">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Statutory Details</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>

                <div class="modal-body">
                    <input type="hidden" class="form-control" value="<?php echo current_url(); ?>" name="url">
                    <input type="hidden" class="form-control" name="id" id="idUpdateStatutory">

                    <div class="row">
                        <div class="col">
                            <label><b>Employee EPF NO</b></label>
                            <input type="text" class="form-control" name="epf_no" id="epfUpdateStatutory" disabled>
                        </div>
                        <div class="col">
                            <label><b>Employee SOCSO No</b></label>
                            <input type="text" class="form-control" id="socsoUpdateStatutory" name="socso" disabled>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col">
                        <label><b>Employer Rate</b></label>
                            <p name="er_rate" id="er_rate"></p>
                        </div>
                        
                        <div class="col">
                        <label><b>Employee Rate</b></label>
                            <input type="text" class="form-control" name="er_percent" id="er_percent" disabled>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col">
                            <label><b>Employee Rate</b></label>
                            <p id="ee_rate" name="ee_rate"></p>
                        </div>
                        <div class="col">
                            <label><b>Amount Percentage</b></label>
                            <?php if(strtolower($statutory->action_type) == 'personal data update' || (date('m-Y',strtotime($statutory->created_at)) == date('m-Y', $live_date)) && (strtotime(date('d-m-Y')) <= $end_date)){ ?>
                                <select class="form-control" name="ee_percent" id="ee_percent" required>
                                    <option value="9">9%</option>
                                    <option value="11">11%</option>                         
                                </select>
                            <?php } else { ?>
                                <input type="text" class="form-control" name="ee_percent" id="ee_percent" disabled>
                            <?php } ?>
                        </div>
                    </div>

                    <div class="row">
                        
                        <div class="col">
                            <label><b>Income Tax No</b></label>
                            <input type="text" class="form-control" id="incomeTaxUpadateStatutory" name="income_tax" disabled>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col">
                            <label><b>PCB Deduction</b></label>
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <button class="btn btn-default btn-sm" type="button" style="cursor: default;" disabled>%</button>
                                </div>
                                <input type="text" class="form-control" id="pcbUpdateStatutory" name="pcb_deduction" onkeyup="change_zakat_percent()">
                            </div>
                        </div>
                        <div class="col">
                            <label><b>Zakat Deduction</b> <small><i>*(Zakat = 100% - % PCB Deduction)</i></small></label>
                            <div class="input-group mb-3">
                                <div class="input-group-prepend">
                                    <button class="btn btn-default btn-sm" type="button" style="cursor: default;" disabled>%</button>
                                </div>
                                <input type="text" class="form-control" id="zakatUpdateStatutory" name="zakat_deduction" disabled>
                            </div>                        
                        </div>
                    </div>

                    <div class="row">
                        <div class="col"> 
                            <label><b>Status</b></label>
                            <select name="status" id="idUpdateStatus" class="form-control" disabled>
                                <option value="1">Active</option>
                                <option value="0">Inactive</option>
                            </select>
                        </div>
                        <div class="col">
                            <label><b>Year</b></label>
                            <input type="text" name="year" id="idUpdateYear" class="form-control" disabled>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-dismiss="modal">Cancel</button>
                    <button type="button" id="btnSaveStatutory" class="btn btn-primary">Save</button>
                </div>
            </form>
        </div>
    </div>
</div>

<script type="text/javascript">
    function openStatutoryModal(elem)
    {
        var json = JSON.parse(atob($(elem).data('content')));
        console.log(json);
        
        clearForm('#statutoryForm');

        if(json != '' && json != null && json != undefined)
        {
            $('#idUpdateStatutory').val(json.id);
            $('#epfUpdateStatutory').val(json.ee_epf_no);

            if(json.er_rate == 0)
            {
                $('#er_rate').text('STATUTORY RATE');
            }else if(json.er_rate == 1)
            {
                $('#er_rate').text('COMPANY RATE');
            }else if(json.er_rate == 2)
            {
                $('#er_rate').text('PERSONAL RATE');
            }else{
                $('#er_rate').text('N/A');
            }

            $('#er_percent').val(json.er_percent);


            if(json.ee_rate == 0)
            {
                $('#ee_rate').text('STATUTORY RATE');
            }else if(json.ee_rate == 1)
            {
                $('#ee_rate').text('COMPANY RATE');
            }else if(json.ee_rate == 2)
            {
                $('#ee_rate').text('PERSONAL RATE');
            }else{
                $('#ee_rate').text('N/A');
            }

            $('#ee_percent').val(json.ee_percent);
            $('#socsoUpdateStatutory').val(json.ee_socso);
            $('#incomeTaxUpadateStatutory').val(json.ee_income_tax);
            $('#pcbUpdateStatutory').val(json.ee_pcb_deduction_percent);
            $('#zakatUpdateStatutory').val(json.ee_zakat_deduction_percent);
            $('#idUpdateStatus').val(json.status);
        }else{
            $('#ee_rate').text('STATUTORY RATE');
            $('#er_rate').text('STATUTORY RATE');
            $('#idUpdateStatus').val(1);
        }

        $('#idUpdateYear').val(<?php echo date('Y'); ?>);
        $('#btnSaveStatutory').attr('onclick', 'saveStatutory()');
        $('#mdl-view-statutory').modal('show');
        }  


    function populateModalStatutory(elem)
    {
        var json = JSON.parse(atob($(elem).data('content')));
        console.log(json);
        
        clearForm('#statutoryForm');
        $('#idUpdateStatutory').val(json.id);
        $('#epfUpdateStatutory').val(json.ee_epf_no);

        if(json.er_rate == 0)
        {
            $('#er_rate').text('STATUTORY RATE');
        }else if(json.er_rate == 1)
        {
            $('#er_rate').text('COMPANY RATE');
        }else if(json.er_rate == 2)
        {
            $('#er_rate').text('PERSONAL RATE');
        }else{
            $('#er_rate').text('N/A');
        }

        $('#er_percent').val(json.er_percent);

        if(json.ee_rate == 0)
        {
            $('#ee_rate').text('STATUTORY RATE');
        }else if(json.ee_rate == 1)
        {
            $('#ee_rate').text('COMPANY RATE');
        }else if(json.ee_rate == 2)
        {
            $('#ee_rate').text('PERSONAL RATE');
        }else{
            $('#ee_rate').text('N/A');
        }

        $('#ee_percent').val(json.ee_percent);
        $('#socsoUpdateStatutory').val(json.ee_socso);
        $('#incomeTaxUpadateStatutory').val(json.ee_income_tax);
        $('#pcbUpdateStatutory').val(json.ee_pcb_deduction);
        $('#zakatUpdateStatutory').val(json.ee_zakat_deduction);
        $('#idUpdateStatus').val(json.status);
        $('#idUpdateYear').val(json.year);
        $('#btnSaveStatutory').attr('onclick', 'saveStatutory()');
    }

    function change_zakat_percent()
    {
        var pcb = $('#pcbUpdateStatutory').val();
        var zakat = 100 - pcb;

        $('#zakatUpdateStatutory').val(zakat);
    }

    function saveStatutory(){
        var postData = {
            'profiling_id'  : $("#profiling_id_new").val(),
            'ee_percent'    : $("#ee_percent").val(),
            'pcb'           : $("#pcbUpdateStatutory").val(),
            'zakat'          : $("#zakatUpdateStatutory").val(),
            'year'          : $("#idUpdateYear").val(),
        }

        console.log(postData);
        $.ajax({
            type: 'POST',
            url: '<?php echo base_url('hr/Myprofile/save_statutory/'); ?>',
            data: postData,
            dataType: 'JSON',
            success: function(data) {
                if(data.status == 'success')
                {
                    localStorage.setItem("status",data.status);
                    localStorage.setItem("msg",data.msg);

                    location.replace(data.redirect);
                }
            }
        });
    }
</script>