<div class="wrapper">
    <div class="container">
        <!-- Page-Title -->
        <div class="row">
            <div class="col-sm-12">
                <div class="btn-group pull-right m-t-15">
                    <button type="button" style="display:none;" id="createpassbtn" class="btn btn-custom waves-effect waves-light" >Create<span class="m-l-5"><i class=" fa fa-plus"></i></span></button>
                    <button type="button" id="dispassbtn" class="btn btn-danger waves-effect waves-light" ><span class="txtbtn">List Discount Password User</span> <span class="m-l-5"><i id="icon" class="fa fa-list"></i></span></button>
                    <button type="button" onclick="openadd();" id="disreasonaddbtn" class="btn btn-primary waves-effect waves-light m-l-5" ><span class="txtbtn">Add New Reason</span> <span class="m-l-5"><i id="icon" class="fa fa-plus"></i></span></button>
                </div>
                <h4 class="page-title" >Configuration:</h4>
            </div>
        </div>
        
            <div class="row disconf">
                <div class="col-sm-12">
                    <div class="card-box">
                        <h4 class="header-title m-t-0 m-b-30">Discount Reasons:</h4>
                        <div class="row">
                            <div class="col-md-12">
                                <table class="table">
                                    <thead>
                                        <tr>
                                        <th>ID</th>
                                        <th>Reason</th>
                                        <th>Percentage</th>
                                        <th>Active</th>
                                        <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php foreach ($discount_reasons as $discountreason) { ?>
                                        <tr>
                                            <td><?php echo $discountreason['id_discount_reasons']; ?></td>
                                            <td><?php echo $discountreason['discount_reason']; ?></td>
                                            <td><?php echo $discountreason['discount_perc'].'%'; ?></td>
                                            <td><?php echo $discountreason['active']; ?></td>
                                            <td class='noprint'> 
                                                <button id='btnedit' onclick="openedit(<?php echo $discountreason['id_discount_reasons']; ?>,'<?php echo $discountreason['discount_reason'];?>', '<?php echo $discountreason['discount_perc'];?>', '<?php echo $discountreason['active'];?>');" class="btn btn-icon waves-effect waves-light btn-info m-b-5"> <i class="fa fa-keyboard-o"></i> </button>
                                            </td>
                                        <tr>
                                        <?php } ?>
                                    </tbody>
                                </table>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    </div>
   
        <!--Discount Password start-->
        <?php if (isset($dispass)) { ?>
            <?php foreach ($dispass as $pass) { ?>
                <div class="row dispassrow" id="dispassrow" style="display:none;">
                    <div class="col-sm-12">
                        <div class="card-box">
                            <h4 class="header-title m-t-0 m-b-30">Discount User: <?php echo $pass->name; ?></h4>

                            <form id="discForm<?php echo $pass->id; ?>" action="<?php echo base_url('discount_controller/discount_password_update'); ?>" method="POST">
                                <div class="row">
                                    <div class="col-md-6">
                                        <div class="p-20">
                                            <h5><b>Note!</b></h5>
                                            <span>Make sure before change password must remember new password.</span>
                                        </div>
                                    </div><!-- end col -->

                                    <div class="col-md-6">
                                        <div class="p-20">
                                            <h5><b>Name</b></h5>
                                            <input required type="text" class="form-control" maxlength="10" name="user" id="user<?php echo $pass->id; ?>" value="<?php echo $pass->name ? $pass->name : ""; ?>">

                                            <div class="m-t-20">
                                                <h5><b>Email</b></h5>
                                                <input type="email" class="form-control"  name="email" id="email<?php echo $pass->id; ?>" value="<?php echo $pass->email ? $pass->email : ""; ?>">
                                            </div>

                                            <div class="m-t-20">
                                                <h5><b>Username</b></h5>
                                                <input required type="text" class="form-control" maxlength="10" name="name" id="name<?php echo $pass->id; ?>" value="<?php echo $pass->username ? $pass->username : ""; ?>">
                                                <span id="user-message<?php echo $pass->id; ?>" class="text-danger user-message"></span>
                                            </div>

                                            <div class="m-t-20">
                                                <h5><b>Current Password</b></h5>
                                                <input required type="password" class="form-control" maxlength="10" name="current_password" id="current_password<?php echo $pass->id; ?>">
                                            </div>

                                            <div class="m-t-20">
                                                <h5><b>New Password</b></h5>
                                                <input required type="password" class="form-control" maxlength="10" name="new_password" id="new_password<?php echo $pass->id; ?>">
                                            </div>

                                            <div class="m-t-20">
                                                <h5><b>Confirm Password</b></h5>
                                                <input required type="password" class="form-control" maxlength="10" name="confirm_password" id="confirm_password<?php echo $pass->id; ?>">
                                                <span id='message<?php echo $pass->id; ?>'></span>
                                            </div>

                                            <div class="m-t-20"> 
                                                <button id="editbutton<?php echo $pass->id; ?>" type="submit" class="btn btn-custom waves-effect waves-light w-md m-b-5 ">Save</button>
                                                <input type="hidden" name="passid" id="passid" value="<?php echo $pass->id; ?>" />
                                            </div>
                                        </div>
                                    </div><!-- end col -->


                                </div><!-- end row -->

                            </form>


                        </div>
                    </div><!-- end col -->
                </div>
            <?php } ?>
        <?php } ?>
        <!--Discount password end-->


    </div>

    <!--Create start-->
    <div id="addcreate" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addcreate" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="addnewForm" action="<?php echo base_url('discount_controller/create_new_user_discount_pass'); ?>" method="POST">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Add a new User Invoice</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtname" class="control-label">Name</label>
                                    <input required type="text" class="form-control" placeholder="Name" id="txtname" name="txtname">
                                </div>
                                <div class="form-group">
                                    <label for="txtemail" class="control-label">Email</label>
                                    <input type="email" class="form-control" placeholder="Email" id="txtemail" name="txtemail">
                                </div>
                                <div class="form-group">
                                    <label for="txtusername" class="control-label">Username</label>
                                    <input required type="text" class="form-control" placeholder="Username" id="txtusername" name="txtusername">
                                    <span class="text-danger user-message"></span>
                                </div>
                                <div class="form-group">
                                    <label for="txtpass" class="control-label">Password</label>
                                    <input required type="password" class="form-control" placeholder="Password" id="txtpass" name="txtpass">
                                </div>
                                <div class="form-group">
                                    <label for="txtcnfpass" class="control-label">Confirm Password</label>
                                    <input required type="password" class="form-control" placeholder="Confirm assword" id="txtcnfpass" name="txtcnfpass">
                                    <span id='txtmessage'></span>
                                </div>
                            </div> 
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button id="savebtn" type="submit" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <!--Create end-->

    
    <!--Edit Reason start-->
    <div id="editreason" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="editreason" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="editreasonform" action="<?php echo base_url('discount_controller/update_discount_reason'); ?>" method="POST">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Edit Discount Reason</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label for="txtreasonid" class="control-label">ID</label>
                                    <input required type="text" readonly="readonly" class="form-control" placeholder="Name" id="txtreasonid" name="txtreasonid">
                                </div>
                                <div class="form-group">
                                    <label for="txtreasonname" class="control-label">Reason</label>
                                    <input required type="text" class="form-control" placeholder="Name" id="txtreasonname" name="txtreasonname">
                                </div>
                                <div class="form-group">
                                    <label for="txtreasonperc" class="control-label">Reason</label>
                                    <input required type="text" class="form-control" placeholder="Percentage" id="txtreasonperc" name="txtreasonperc">
                                </div>
                                <div class="form-group">
                                    <label for="txtreasonactive" class="control-label">Active</label>
                                    <select class="form-control" id="txtreasonactive" name="txtreasonactive">
                                        <option value="Yes">Yes</option>
                                        <option value="No">No</option>
                                    </select>
                                </div>
                                
                            </div> 
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button id="btnupdatereason" type="button" onclick="update_reason();" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <!--edit reason end-->

    <!--add Reason start-->
    <div id="addreason" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addreason" aria-hidden="true" style="display: none;">
        <div class="modal-dialog">
            <div class="modal-content">
                <form id="addreasonform" action="<?php echo base_url('discount_controller/add_discount_reason'); ?>" method="POST">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="custom-width-modalLabel">Add Discount Reason</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-12">
                                
                                <div class="form-group">
                                    <label for="txtaddreasonname" class="control-label">Reason</label>
                                    <input required type="text" class="form-control" placeholder="Name" id="txtaddreasonname" name="txtaddreasonname">
                                </div>
                                <div class="form-group">
                                    <label for="txtaddreasonperc" class="control-label">Percentage</label>
                                    <input required type="text" class="form-control" placeholder="Name" id="txtaddreasonperc" name="txtaddreasonperc">
                                </div>
                                <div class="form-group">
                                    <label for="txtaddreasonactive" class="control-label">Active</label>
                                    <select class="form-control" id="txtaddreasonactive" name="txtaddreasonactive">
                                        <option value="Yes">Yes</option>
                                        <option value="No">No</option>
                                    </select>
                                </div>
                                
                            </div> 
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                        <button id="btnaddreason" type="submit" onclick="add_reason();" class="btn btn-custom waves-effect waves-light">Save</button>
                    </div>
                </form>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div>
    <!--add reason end-->
    
    <script>
        $(document).ready(function() {
            toastr.options = {
                "closeButton": true,
                "debug": true,
                "newestOnTop": true,
                "progressBar": true,
                "positionClass": "toast-bottom-center",
                "preventDuplicates": true,
                "onclick": null,
                "showDuration": "300",
                "hideDuration": "1000",
                "timeOut": "5000",
                "extendedTimeOut": "1000",
                "showEasing": "swing",
                "hideEasing": "linear",
                "showMethod": "fadeIn",
                "hideMethod": "fadeOut"
            }

            //Add user invoice pass start..
            $("#txtemail").blur(function(e) {
                e.preventDefault();
                var email = $('#txtemail').val();
                var parts = email.split('@');
                var user = parts[0];
                var username = user.replace(/[^a-z\d]+/ig, '');
                $("#txtusername").val(username);
            });

            $('#txtcnfpass').on('keyup', function() {
                if ($(this).val() == $('#txtpass').val()) {
                    $('#txtmessage').html('matching').css('color', 'green');
                } else
                    $('#txtmessage').html('not matching').css('color', 'red');
            });
            //Add user invoice pass end..

            $("#dispassbtn").click(function() {
                $(".dispassrow").slideToggle(500);
                $(".disconf").slideToggle(500);
                $("#createpassbtn").slideToggle(500);
                if ($("#icon").attr("class") == "fa fa-percent") {
                    $("#icon").attr("class", "fa fa-list");
                    $("#dispassbtn .txtbtn").html('');
                    $("#dispassbtn .txtbtn").html('List Discount Password User');
                } else {
                    $("#icon").attr("class", "fa fa-percent");
                    $("#dispassbtn .txtbtn").html('');
                    $("#dispassbtn .txtbtn").html('Discount Types');
                }
            });

            $("#createpassbtn").click(function() {
                $("#addcreate").modal('show');
            });
            
            

            <?php foreach ($dispass as $pass) { ?>
                $('#email<?php echo $pass->id; ?>').blur(function(e) {
                    e.preventDefault();
                    var $value = $('#email<?php echo $pass->id; ?>').val();
                    var $username = createUserName($value);
                    $("#name<?php echo $pass->id; ?>").val($username);
                });

                var createUserName = function(value) {
                    var parts = value.split('@');
                    var user = parts[0];
                    var username = user.replace(/[^a-z\d]+/ig, '');
                    return username;
                };

                $('#confirm_password<?php echo $pass->id; ?>').on('keyup', function() {
                    if ($(this).val() == $('#new_password<?php echo $pass->id; ?>').val()) {
                        $('#message<?php echo $pass->id; ?>').html('matching').css('color', 'green');
                    } else
                        $('#message<?php echo $pass->id; ?>').html('not matching').css('color', 'red');
                });

                $('#discForm<?php echo $pass->id; ?>').on('submit', function(e) {
                    e.preventDefault();

                    if ($("#new_password<?php echo $pass->id; ?>").val() != $("#confirm_password<?php echo $pass->id; ?>").val()) {
                        $('#message<?php echo $pass->id; ?>').html('not matching').css('color', 'red');
                        // return false;
                    } else {
                        $.ajax({
                            url: $(this).attr('action') || window.location.pathname,
                            type: "POST",
                            data: $(this).serialize(),
                            success: function(data) {
                                if (data == "success") {
                                    toastr.success('Discount Password Updated!');
                                    $("#current_password<?php echo $pass->id; ?>").val('');
                                    $("#new_password<?php echo $pass->id; ?>").val('');
                                    $("#confirm_password<?php echo $pass->id; ?>").val('');
                                    location.reload();
                                } else if (data == $("#name<?php echo $pass->id; ?>").val()) {
                                    $('#user-message<?php echo $pass->id; ?>').text('');
                                    $('#user-message<?php echo $pass->id; ?>').text('Username already exist!');
                                } else {
                                    toastr.error('Wrong Password!');
                                }
                            },
                            error: function(jXHR, textStatus, errorThrown) {
                                alert(errorThrown);
                            }
                        });
                    }


                });
            <?php } ?>

            //Submit form for adding new user ivoice pass start.. 
            $('#addnewForm').on('submit', function(e) {
                e.preventDefault();
                if ($('#txtcnfpass').val() != $('#txtpass').val()) {
                    $('#txtmessage').html('not matching').css('color', 'red');
                } else {
                    $.ajax({
                        url: $(this).attr('action') || window.location.pathname,
                        type: "POST",
                        data: $(this).serialize(),
                        success: function(data) {
                            if (data == "success") {
                                toastr.success('Successfully added!');
                                location.reload();
                            } else if (data == $("#txtusername").val()) {
                                $('.user-message').text('');
                                $('.user-message').text('Username already exist!');
                            } else {
                                toastr.error('Something went wrong!');
                            }
                        },
                        error: function(jXHR, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                }
            });
            //Submit form for adding new user ivoice pass end..

        });

        function discount_update(id) {
            if (id) {
                var discount_rate = $("#discount_rate" + id).val();
                var discount_visits = $("#discount_visits" + id).val();
                var discount_purchase = $("#discount_purchase" + id).val();
                var discount_period = $("#discount_period" + id).val();
                var discount_active = $("#discount_active" + id).val();
                //console.log(discount_rate + '---' + discount_visits + '---' + discount_purchase + '---' + discount_period + '---' + discount_active);
                $.ajax({
                    type: 'POST',
                    url: 'discount_controller/discount_update',
                    data: {id_business_discounts: id, 
                        discount_rate: discount_rate, 
                        discount_visits: discount_visits, discount_purchase: 
                                discount_purchase, 
                        discount_period: discount_period, 
                        discount_active: discount_active},
                    success: function(data) {
                        var result = data.split("|");
                        //console.log(result);
                        if (result[0] === "success") {
                            toastr.success(data, 'Discount Updated');
                            location.reload();
                        }
                    }
                });
            }
        }
        
        function openedit(id, reason, perc, active){
            $("#txtreasonid").val(id);
            $("#txtreasonname").val(reason);
            $("#txtreasonperc").val(perc);
            $("#txtreasonactive").val(active);
        
            $("#editreason").modal('show');
            
        }
        
        function openadd(){
            $("#addreason").modal('show');
        }
        
        function update_reason(){
            if ($("#txtreasonid").val!=="") {
                var id = $("#txtreasonid").val();
                var discount_reason = $("#txtreasonname").val();
                var discount_perc = $("#txtreasonperc").val();
                var discount_active = $("#txtreasonactive").val();

                $.ajax({
                    type: 'POST',
                    url: 'discount_controller/discount_reason_update',
                    data: {id_discount_reasons: id, 
                        discount_reason: discount_reason, 
                        discount_perc: discount_perc,
                        active: discount_active},
                    success: function(data) {
                        var result = data.split("|");
                        //console.log(result);
                        if (result[0] === "success") {
                            toastr.success(data, 'Discount Reason Updated');
                            location.reload();
                        }
                    }
                });
            }
        }
        
        function add_reason(){
            $.ajax({
                type: 'POST',
                url: 'discount_controller/discount_reason_add',
                data: {
                    discount_reason: $("#txtaddreasonname").val(), 
                    discount_perc: $("#txtaddreasonperc").val(),
                    active: $("#txtaddreasonactive").val()
                },
                success: function(data) {
                    var result = data.split("|");
                    //console.log(result);
                    if (result[0] === "success") {
                        toastr.success(data, 'Discount Reason Added');
                        location.reload();
                    }
                }
            });
        }
        
    </script>