<div class="modal fade" id="current_call_modal">
    <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-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title bold"><?php print CommonLanguage::getMessage('current_call'); ?></h4>
            </div>
            <div class="modal-body">
                <form id="current_call_form" method="POST" onsubmit="return false">
                    <?php echo Forms::AlertDiv('current_call_alert'); ?>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label for="type"><?php print CommonLanguage::getMessage('title'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" value="" name="call_title" id="call_title"  class="call_title form-control" required>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group">
                                <label><?php print CommonLanguage::getMessage('Date'); ?></label>
                                <span class="red-star">*</span>
                                <input type="text" id="current_date" name="current_date" class="form-control" required/>
                            </div>
                        </div>
                        <div class="col-md-3">
                            <div class="form-group ">
                                <label><?php print CommonLanguage::getMessage('time'); ?></label>
                                <span class="red-star">*</span>
                                <input type="text" id="current_call_time" name="current_call_time" class="form-control" required />
                            </div>
                        </div>
                        <div class="col-md-3">
                            <label><?php print CommonLanguage::getMessage('duration'); ?></label>
                            <span class="red-star">*</span>
                            <input type="text" id="current_activity_duration" name="current_activity_duration" class="form-control" placeholder="" required/>
                        </div>
                    </div>
                    <div class="row form-group">
                        <div class="col-md-3">
                            <label for="type"><?php print CommonLanguage::getMessage('Action'); ?></label>
                            <span class="red-star">*</span>
                            <select name="action_type_id" id="action_type_id" class="full-width" data-init-plugin="select2" required>
                                <option value=""><?php  echo CommonLanguage::getMessage('select');?></option>
                                <option value="1"><?php  echo CommonLanguage::getMessage('reorder');?></option>
                                <option value="2"><?php  echo CommonLanguage::getMessage('no_need');?></option>
                                <option value="3"><?php  echo CommonLanguage::getMessage('call_again');?></option>
                            </select>
                        </div>
                        <div class="col-md-3 next_date_div hidden">
                            <label><?php  echo CommonLanguage::getMessage('next_date');?></label>
                            <span class="red-star next_date_div hidden">*</span>
                            <input type="text" id="current_next_date" name="current_next_date" class="form-control"/>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-md-12 form-group">
                            <label><?php print CommonLanguage::getMessage('notes'); ?></label>
                            <textarea type="text" id="call_note" name="call_note" class="form-control" ></textarea>
                        </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_current_call" class="btn bg-complete-dark"><?php print CommonLanguage::getMessage('Save'); ?></button>
            </div>
        </div>
        <?php echo Forms::ModalLoader('current_call_loader'); ?>
    </div>
</div>
<script>
    var next_call_days = <?php  echo json_encode( smc_session()['default_setting']['next_call_days']);?>;
    $(function(){
        $('#current_next_date').datetimepicker({'format': 'YYYY-MM-DD'});
        $("#current_date").datetimepicker({'format': 'YYYY-MM-DD'});
        $("#current_call_time").datetimepicker({'format': 'HH:mm'});
        $('#action_type_id').on('change',function(){
            if($(this).val() == 3){
                $('.next_date_div').removeClass('hidden'); 
                $('#current_next_date').attr('required',true); 
                updateNextDate();
            }else{
                $('.next_date_div').addClass('hidden');
                $('#current_next_date').attr('required',false);
            }
        });
        $("#current_activity_duration").timepicker({showMeridian: false,maxHours: 8,defaultTime:'00:15',step: '15'});
        $("#current_call_modal").on("hidden.bs.modal", function () {
            $('.next_date_div').addClass('hidden');
            $('#current_next_date').attr('required',false);
        });
        $("#current_call_modal").on("show.bs.modal", function () {
            $('.call_title').val('Call '+$('.customer_profile_name').html());
            updateNextDate();
            $("#current_activity_duration").val("00:15");
            $('#current_date').data("DateTimePicker").date(new Date());
            $('#current_call_time').data("DateTimePicker").date(new Date());
        });

        $("#submit_current_call").on("click", function () {
            if (!$("#current_call_form").valid())
                return;
            var Data = new FormData(document.getElementById("current_call_form"));
            Data.append('organization_id',$('#org_id').val());
            Data.append('contact_id',$('#current_contact_id').val() || 0);
            Data.append('deal_id',$('#current_deal_id').val() || 0);
            $("#current_call_loader").removeClass('hidden');
            $.ajax({
                url: "crmAjax/submitCurrentCall",
                method: "POST",
                data: Data,
                processData: false,
                contentType: false,
                dataType: "JSON",
                complete: function (jqXHR, status) {
                    if (status == 'success') {
                        if (jqXHR.responseJSON[1] == "success") {
                            if($('#current_origin').val() == 'cust_prof'){
                                loadlog($('#current_origin').val(), $("#org_id").val());
                                loadActivities($("#org_id").val())
                            }else if($('#current_origin').val() == 'cont_prof'){
                                loadlog($('#current_origin').val(), $("#org_id").val(),$("#current_contact_id").val());
                                loadActivities(0,0,$("#current_contact_id").val())
                            }else if($('#current_origin').val() == 'deal_prof'){
                                loadlog($('#current_origin').val(), $("#org_id").val(),$("#current_contact_id").val(),$("#current_deal_id").val());
                                loadActivities(0,$("#current_deal_id").val(),0)
                            }
                            hideModalAlert('current_call_modal', 'current_call_alert');
                        }
                        AddAlert("#current_call_alert", jqXHR.responseJSON[0], jqXHR.responseJSON[1]);
                    }
                    $("#current_call_loader").addClass('hidden');
                }
            });
        });
    });
    function updateNextDate(){
        if(next_call_days){
            var next_date = new Date();
            next_date.setDate(next_date.getDate() + parseInt(next_call_days));
            $('#current_next_date').data("DateTimePicker").date(next_date);
        }else{
            $('#current_next_date').data("DateTimePicker").date(new Date());
        }
    }
</script>