
<div class="modal fade" id="commission_setting_modal" data-backdrop="static" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="pg-close fs-20"></i>
                </button>
                <h5 class="add-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('add_commission_setting'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_commission_setting'); ?></h5>
            </div>
            <div class="modal-body">
                <form id="commission_setting_form" method="POST" onsubmit="return false">
                    <input type='hidden' name='commission_id' id='commission_id'/>
                    <div class="row">
                        <?php echo Forms::AlertDiv('commission_setting_alert'); ?>
                    </div>
                    <div class="row  m-t-10">
                        <div class="col-md-4 form-group">
                            <label><?php print CommonLanguage::getMessage('commission_name'); ?></label>
                            <span class="red-star">*</span>
                            <input class="form-control" id="commission_name" name="commission_name" type="text" required />
                        </div>
                    </div> 
                    <div class="row m-t-10">
                        <h3 class="semi-bold"><?php print CommonLanguage::getMessage('details'); ?></h3>
                        <div class="col-md-12 commission_details_div">
                            <div class="col-md-12 form-group">
                                <div class="col-md-2">
                                    <label><?php echo CommonLanguage::getMessage('commission_option') ?></label>
                                    <span class="red-star">*</span>
                                </div>
                                <div class="col-md-1">
                                    <label><?php echo CommonLanguage::getMessage('operation') ?></label>
                                </div>
                                <div class="col-md-2">
                                    <label><?php echo CommonLanguage::getMessage('value_from') . ' <span class="red-star">( in ' . smc_session()['local_currency_symbol'] . ' )</span>' ?></label>
                                </div>
                                <div class="col-md-2">
                                    <label><?php echo CommonLanguage::getMessage('value_to') . ' <span class="red-star">( in ' . smc_session()['local_currency_symbol'] . ' )</span>' ?></label>
                                </div>
                                <div class="col-md-2">
                                    <label><?php echo CommonLanguage::getMessage('commission_type') ?></label>
                                    <span class="red-star">*</span>
                                </div>
                                <div class="col-md-2">
                                    <label><?php echo CommonLanguage::getMessage('commission_amount') . ' <span class="red-star">( in ' . smc_session()['local_currency_symbol'] . ' )</span>' ?></label>
                                    <span class="red-star">*</span>
                                </div>
                            </div>
                            <div class="row add-row m-t-10 " data-index="0">
                                <div class="col-md-2">
                                    <select name="commission_option" class="full-width " data-init-plugin="select2">
                                        <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                                        <option value="1"><?php echo CommonLanguage::getMessage('Daily'); ?></option>
                                        <option value="2"><?php echo CommonLanguage::getMessage('monthly'); ?></option>
                                        <option value="3"><?php echo CommonLanguage::getMessage('weekly'); ?></option>
                                    </select>
                                </div>
                                <div class="col-md-1">
                                    <select name="condition_operation" class="full-width " data-init-plugin="select2">
                                        <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                                        <option value="1"><?php echo '>'; ?></option>
                                        <option value="2"><?php echo '<'; ?></option>
                                        <option value="3"><?php echo CommonLanguage::getMessage('between'); ?></option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <input name="condition_value" class="form-control" type="number" min="0" step="any"/>
                                </div>
                                <div class="col-md-2">
                                    <input name="condition_value_to" class="form-control hidden" type="number" min="0" step="any"/>
                                </div>
                                <div class="col-md-2">
                                    <select name="commission_type" class="full-width " data-init-plugin="select2">
                                        <option value=""><?php echo CommonLanguage::getMessage('select'); ?></option>
                                        <option value="1">%</option>
                                        <option value="2"><?php echo CommonLanguage::getMessage('value'); ?></option>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <input name="commission_amount" class="form-control" type="number" min="0" step="any"/>
                                </div>
                                <div class="col-md-1 buttons">
                                    <span class="fa fa-plus" style="color:#4c0870;margin-top:15px;margin-right:25px;cursor:pointer;"></span>
                                    <span class="fa fa-minus" style="color:#ed9421;margin-top:15px;cursor:pointer;"></span>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('Close'); ?></button>
                <button style="color:white !important;" class="btn bg-success-dark text-white" id="submit_commission_setting"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
            <?php echo Forms::ModalLoader('commission_setting_loader'); ?>
        </div>
    </div>
</div>
<script>

    $(function () {

        $("#commission_setting_form").on("change","select[name=condition_operation]",function(){
            var tr = $(this).parents('.add-row');
            if($(this).val()==3)
                $("input[name=condition_value_to]",tr).removeClass("hidden")
            else
                $("input[name=condition_value_to]",tr).val("").addClass("hidden")
        })
        
        $('#submit_commission_setting').on('click', function () {
            if (!$("#commission_setting_form").valid())
                return;
            var Data = new FormData(document.getElementById("commission_setting_form")),
                    details = [], is_negative = false;
            $('.add-row').each(function () {
                var parent = $(this);
                if ($('select[name=commission_option]', parent).val() && $('select[name=commission_type]', parent).val() && $('input[name=commission_amount]', parent).val()) {
                    details.push({
                        commission_option: $('select[name=commission_option]', parent).val(),
                        condition_operation: $('select[name=condition_operation]', parent).val(),
                        condition_value: $('input[name=condition_value]', parent).val(),
                        condition_value_to: $('input[name=condition_value_to]', parent).val(),
                        commission_type: $('select[name=commission_type]', parent).val(),
                        commission_amount: $('input[name=commission_amount]', parent).val()
                    });
                }
                if (parseFloat($('input[name=commission_amount]', parent).val()) < 0 || parseFloat($('input[name=condition_value]', parent).val()) < 0)
                    is_negative = true
            });
            if (details.length == 0) {
                AddAlert("#commission_setting_alert", "<?php echo CommonLanguage::getMessage('input_option'); ?>", "danger");
                return;
            }
            if (is_negative) {
                AddAlert("#commission_setting_alert", "<?php echo CommonLanguage::getMessage('negative_value'); ?>", "danger");
                return;
            }
            Data.append('details', JSON.stringify(details));
            $("#commission_setting_loader").removeClass('hidden');
            $.ajax({
                url: "HRMAjax/submitCommissionSetting",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('commission_setting_modal', 'commission_setting_alert');
                            RebuildDataTable($('.panel-heading .panel-title').html(), [0, 1], 'HRMAjax/commissionSettingAjax');
                        }
                        AddAlert("#commission_setting_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    } else {
                        AddAlert("#commission_setting_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);

                    }
                    $("#commission_setting_loader").addClass('hidden');
                }
            });
        });
        $("#new_commission_setting").on("click", function () {
            $("#commission_id").val(0);
            $("#commission_setting_modal .add-label").removeClass('hidden');
        });
        $("#tableWithSearch").on("click", ".edit", function () {
            var commission_id = $(this).parents('div.Action').attr('data-id');
            $("#commission_id").val(commission_id);
            $("#commission_setting_modal .edit-label").removeClass('hidden');
            CommonViewById('HRMAjax/getCommissionSettingData', {commission_id: commission_id},
                    '#commission_setting_loader', function (response) {
                        var main = response['main'][0], details = response['details'],
                                current_row = $(".commission_details_div .add-row:eq(0)").clone();
                        $('#commission_name').val(main.commission_name);
                        $(".commission_details_div .add-row").remove();
                        $.each(details, function (index, value) {
                            current_row = current_row == null ? $(".commission_details_div .add-row:eq(0)").clone() : current_row;
                            $('input[name=condition_value]', current_row).val(details[index].condition_value);
                            $('input[name=condition_value_to]', current_row).val(details[index].condition_value_to);
                            $('input[name=commission_amount]', current_row).val(details[index].commission_amount);
                            $(".commission_details_div").append(current_row);
                            $(".select2-container", current_row).remove();
                            $('select[name=commission_option]', current_row).val(details[index].commission_option);
                            $('select[name=condition_operation]', current_row).val(details[index].condition_operation);
                            $('select[name=commission_type]', current_row).val(details[index].commission_type);
                            $("select", current_row).select2();
                            $(".select2-container", current_row).show();
                            current_row = null
                        });
                                                $(".fa-minus").css("display",'inline-block');

                    });
        });
        $(".commission_details_div").on("click", " .fa-plus", function (e) {
            $(".add-row  .fa-minus").css("display", "inline-block");
            var Parent = $(this).parents(".add-row"), NewItem = Parent.clone().insertAfter(Parent);
            NewItem.find("input").val('');
            NewItem.find("input[name=condition_value_to]").addClass("hidden");
            NewItem.find(".select2-container").remove();
            NewItem.find("select").val('').select2();
            $(".select2-container", NewItem).show();
        });
        $(".commission_details_div").on("click", ".fa-minus", function (e) {
            if ($(".add-row").length != 1)
                $(this).closest(".add-row").empty().remove();
        });

    });

</script>