<div class="modal fade" id="contact_modal" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-md">
        <div class="modal-content">
            <div class="modal-header text-left">
                <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_contact'); ?></h5>
                <h5 class="edit-label h-labels text-left bold hidden"><?php echo CommonLanguage::getMessage('edit_contact'); ?></h5>
            </div>
            <div class="modal-body">
                <div class="row"><?php echo Forms::AlertDiv('contact_alert'); ?></div>
                <form id="contact_form" method="POST" onsubmit="return false">
                    <input type="hidden" name="contact_id" id="organization_contact_id">
                    <div class="row form-group">
                        <div class="col-md-6">
                            <label for="name-input"><?php print CommonLanguage::getMessage('contact_name'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" id="contact_name" name="contact_name" class="form-control" required />
                        </div>
                        <div class="col-md-6">
                            <label for="mobile-input"><?php print CommonLanguage::getMessage('Phone'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" id="contact_mobile_phone" name="contact_mobile_phone" class="form-control" required />
                        </div>

                    </div>
                    <div class="row form-group">
                        <div class="col-md-6">
                            <label for="workphone-input"><?php print CommonLanguage::getMessage('second_phone'); ?></label>
                            <input type="text" id="contact_work_phone" name="contact_work_phone" class="form-control" />
                        </div>
                        <div class="col-md-6">
                            <label for="email-input"><?php print CommonLanguage::getMessage('Email'); ?></label>
                            <input type="email" id="contact_email" name="contact_email" class="form-control" />
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-12">
                            <label for="country"><?php print CommonLanguage::getMessage('organization'); ?></label>
                            <span class="red-star">*</span>
                            <input class="contact_organization_id full-width" id="contact_organization_id" name="contact_organization_id" type="hidden" value="" data-select-name=''  required/>
                        </div>
                    </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_contact" class="btn bg-complete-dark"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
        <?php echo Forms::ModalLoader('contact_loader'); ?>
    </div>
</div>
<script>
    var contact_origin;
    $(function(){
        build_select2($("#contact_organization_id"), "xhr/getOrganizations")
        $(".panel-body").on("click",'#add_contact,.add_contact_profile',function(){
            contact_origin= $(this).attr('data-origin');
            if(contact_origin == 'list'){
                $("#contact_organization_id").prop('disabled',false);
            }else{
                $("#contact_organization_id").val($('#org_id').val())
                    .attr('data-select-name',$('#current_organization_name').val())
                    .trigger('change');
                $("#contact_organization_id").prop('disabled',true);
            }
            $("#contact_modal .add-label").removeClass('hidden');
            $("#organization_contact_id").val(0);
        });
        $("#tableWithSearch").on("click",".edit_contact",function(){
            $("#contact_modal .edit-label").removeClass('hidden')
            var contact_id = $(this).attr("data-id")
            $("#organization_contact_id").val(contact_id)
            CommonViewById("xhr/getElement",{rowId:contact_id,rowEntry:'contact_id',table:'all_contacts'},'#contact_loader',function(response){
                console.log(response)
                var Keys = Object.keys(response[0])
                $.each(Keys, function (key, value) {
                    $('#' + value).val(response[0][value])
                });
                $("#contact_organization_id").val(response[0].organization_id);
                $("#contact_organization_id").attr('data-select-name',response[0].organization_customer_name);
                build_select2($("#contact_organization_id"), "xhr/getOrganizations");
            });
        });
        $("#submit_contact").on("click", function () {
            if (!$("#contact_form").valid())
                return;
            $("#contact_loader").removeClass('hidden');
            var Data = new FormData(document.getElementById("contact_form"));
            Data.append('organization_id',$('#contact_organization_id').val());
            $.ajax({
                url: "crmAjax/submitContact",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            if(contact_origin == 'list' || $('#organization_contact_id').val() != 0){
                                RebuildDataTable($('.panel-heading .panel-title').html(),[0,1,2,3,4],'crmAjax/contactList',{organization_id:$("input[name=filter_org_id]").val()})
                            }else{
                                getperson();
                            }
                            hideModalAlert('contact_modal', 'contact_alert');
                        }
                        AddAlert("#contact_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#contact_loader").addClass('hidden');
                }
            });
        });
    });
</script>
