<div class="modal fade" id="reason-modal" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header text-left">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title bold">Add Reason for Loss</h4>
            </div>
            <div class="modal-body">
                <div class="row">
                    <?php echo Forms::AlertDiv('reason_alert'); ?>
                </div>
                <form id="formReasonLoss" method="POST" onsubmit="return false">
                    <input type="hidden" name="deal_id" id="reason_deal_id"/>
                    <div class="row form-group m-t-10">
                        <label for="reason">Reason</label>
                        <span class="red-star">*</span>
                        <input class="form-control" id="reason_for_loss" name="reason_for_loss" required />
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal"><?php print CommonLanguage::getMessage('cancel'); ?></button>
                <button style="color:white !important;"  id="submit_reason" class="btn bg-complete-dark"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
        <?php echo Forms::ModalLoader('reasonModalDisabler'); ?>
    </div>
</div>
<script>
    $(function(){
        $("#submit_reason").on("click", function () {
            if (!$("#formReasonLoss").valid())
                return;
            $("#reasonModalDisabler").removeClass('hidden');
            var Data = new FormData(document.getElementById("formReasonLoss"));
            Data.append("deal_state","Lost");
            Data.append("org_id",$('#org_id').val());
            $.ajax({
                url: "crmAjax/submitDealState",
                method: "POST",
                data: Data,
                beforeSend: function () {
                    if(reason_modal_flag == 0){
                        $(divid).fadeOut();
                    }
                },
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            hideModalAlert('reason-modal', 'reason_alert');
                            if(reason_modal_flag == 0){
                                getPipelineData()
                            }else{
                                initialState("Lost");
                                loadlog($('#current_origin').val(), $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                            }
                        }
                        AddAlert("#reason_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#reasonModalDisabler").addClass('hidden');
                }
            });
        });
    });
</script>
