odoo.define('booking.main', function (require) {
    var core = require('web.core');
    var QWeb = core.qweb;
    var _t = core._t;

    $(document).ready(function() {
        console.log($("#pre_registration_required").length)
        if ($("#individual_booking_event").val()=='True')
        {
            $("#continue_button").hide()
        }
        
        // if ($("#pre_registration_required").length>0)
        // {
           
        //     $("#continue_button").hide()
        // }
        
        

        $('#datetimepickerstart_day').datetimepicker({
            format: 'DD/MM/YYYY',
            enableOnReadonly: true
        });
        $('#datetimepickerend_day').datetimepicker({
            format: 'DD/MM/YYYY',
            enableOnReadonly: true
        });
        
        $('#medical_info').hide()

        $('#booking_total_price').text(
            $('#booking_price_product').val()+' €'
            );
        
        

 
    });

    $( "#start_day").on("input", function(e) {
        //alert(parseDate($("#start_day").val()))
        if (parseDate($("#start_day").val())==-1 && parseDate($("#end_day").val())==-1)
        {
            console.log('end_day_change')
            console.log('start_day='+$("#start_day").val())
            console.log('end_day='+$("#end_day").val())
            console.log(parseDate($("#start_day").val()))
            console.log(parseDate($("#end_day").val()))
            
            fct_days()
        }
        else
        {
            $("#days").html()
        }
            
    });
    $( "#end_day").on("input", function(e) {
        
        if (parseDate($("#end_day").val())==-1 && parseDate($("#start_day").val())==-1)
        {
            console.log('end_day_change')
            console.log('start_day='+$("#start_day").val())
            console.log('end_day='+$("#end_day").val())
            console.log(parseDate($("#start_day").val()))
            console.log(parseDate($("#end_day").val()))

            
                fct_days()
           
            
        }
        else
        {
            $("#days").html()
        }
            
    });

    function fct_days()
    {
        // var s = $("#start_day").val().split('/');
        // var e = $("#end_day").val().split('/');
        // var end_day=e[2]+'-'+e[1]+'-'+e[0]
        // var start_day=s[2]+'-'+s[1]+'-'+s[0]
        
        var diff = Math.floor((Date.parse($("#end_day").val()) - Date.parse($("#start_day").val())) / 86400000);
        console.log(diff)
        var start_date = Date.parse($("#start_day").val())
        var end_date = Date.parse($("#end_day").val())
        var start_date_p1month=start_date.addMonths(1)
        diff1=end_date-start_date_p1month

        if (diff<=0)
        {
            $("#days").html(_t('Les dates saisies ne sont pas valides'))
            $("#booking_total_price").hide()
            $("#continue_button").hide()

        
        }
        else if (end_date>start_date_p1month)
        {
            
            $("#days").html(_t('Pour les retraites de plus d\'un mois, veuillez contacter le centre à l\'adresse suivante :<br>info@centre-kalachakra.net'))
            $("#booking_total_price").hide()
            $("#continue_button").hide()

        }
        else if (diff1==0)
        {
            
            $("#days").html(_t('soit 1 mois'))
            $("#booking_total_price").show()
            $("#continue_button").show()
           

        }
        else
        {
            html_days_message=_t('soit ')+diff.toString()+_t(' jours')
            console.log(html_days_message)
            $("#days").html(html_days_message)
            $("#booking_total_price").show()
            $("#continue_button").show()
            
            $("#days_duration").val(diff)

        }
        compute_individual_booking_prices_without_option(diff,start_day,end_day)
        compute_total_price_from_status_without_options()
        compute_total_price_with_options()
            
            


        return true
    }
    function parseDate(str) {
        var d = str.split('/');
        var date1=d[0]+'-'+d[1]+'-'+d[2]
        var m = date1.match(/^(\d{1,2})-(\d{1,2})-(\d{4})$/);
        return (m) ? -1 : null;
      }
      
      function compute_individual_booking_prices_without_option(days,start_day,end_day)
      {
        
       

        var start_date = Date.parse($("#start_day").val())
        var end_date = Date.parse($("#end_day").val())
        var start_date_p1month=start_date.addMonths(1)
        diff1=end_date-start_date_p1month

        if (days<7)
        {   
            
            $("#booking_price").val(days*$("#individual_day_price").val())
            $("#booking_member_price").val(days*$("#individual_member_day_price").val())
            $("#booking_super_member_price").val(days*$("#individual_super_member_day_price").val())

            

        }
        if (days<14 && days>6)
        {
            mod_days=days%7
            let coef_price=1+mod_days/7
            
            price_not_member=Math.round(parseInt($("#individual_week_price").val(),10)*coef_price)
            price_member=Math.round(parseInt($("#individual_member_week_price").val(),10)*coef_price)
            price_super_member=Math.round(parseInt($("#individual_super_member_week_price").val(),10)*coef_price)

            $("#booking_price").val(price_not_member)
            $("#booking_member_price").val(price_member)
            $("#booking_super_member_price").val(price_super_member)

        }
        if (days<21 && days>13)
        {
            mod_days=days%14
            let coef_price=1+mod_days/14

            price_not_member=Math.round(parseInt($("#individual_2weeks_price").val(),10)*coef_price)
            price_member=Math.round(parseInt($("#individual_member_2weeks_price").val(),10)*coef_price)
            price_super_member=Math.round(parseInt($("#individual_super_member_2weeks_price").val(),10)*coef_price)
           
            $("#booking_price").val(price_not_member)
            $("#booking_member_price").val(price_member)
            $("#booking_super_member_price").val(price_super_member)

        }

        if (days>20 && end_date<start_date_p1month)
        {
            mod_days=days%21
            let coef_price=1+mod_days/21

            price_not_member=Math.round(parseInt($("#individual_3weeks_price").val(),10)*coef_price)
            price_member=Math.round(parseInt($("#individual_member_3weeks_price").val(),10)*coef_price)
            price_super_member=Math.round(parseInt($("#individual_super_member_3weeks_price").val(),10)*coef_price)

            $("#booking_price").val(price_not_member)
            $("#booking_member_price").val(price_member)
            $("#booking_super_member_price").val(price_super_member)

        }
        if (diff1==0)
        {
            $("#booking_price").val(parseInt($("#individual_month_price").val(),10))
            $("#booking_member_price").val(parseInt($("#individual_member_month_price").val(),10))
            $("#booking_super_member_price").val(parseInt($("#individual_super_member_month_price").val(),10))

        }
        if (end_date>start_date_p1month)
        {
           
            
            
            
        }


      }
      function compute_total_price_from_status_without_options()
      {
        //recalcul du prix total selon le status
        if ($("#status2").val()=='not member')
        {   
            
            $("#total_price").val($("#booking_price").val())
            $("#price_individual_booking").val($("#booking_price").val())
        }
    if ($("#status2").val()=='member')
        {
     
            $("#total_price").val($("#booking_member_price").val())
            $("#price_individual_booking").val($("#booking_member_price").val())
        }
    if ($("#status2").val()=='super member')
        {   
          
            $("#total_price").val($("#booking_super_member_price").val())
            $("#price_individual_booking").val($("#booking_super_member_price").val())
        }

      }

      function compute_total_price_with_options()
      {
        $( ":checkbox" ).each(function( index ) {
            var l=this.name.length
            var i=this.name.substring(2,l-1)
            
            if (this.value==$("#membership_product").val())
            {
                var option_price_str='#o_standard'+i
            }
            else
            {   
                if ($("#status2").val()=='not member')
                {
                    var option_price_str='#o_standard'+i
                }
                else
                {
                    var option_price_str='#o_'+$("#status2").val()+i
                }
                
            }
            
            
            option_price=$(option_price_str).val()
            if  (!option_price) option_price=0
            console.log(option_price_str+'='+option_price)
            total_price=$("#total_price").val()

            if(this.checked) {
                
                $("#total_price").val(parseInt(total_price)+parseInt(option_price))
                console.log($("#total_price").val())
                
            }
            
          });

          $("#booking_total_price").text($("#total_price").val()+' €');
             console.log( 'total price final='+$("#booking_total_price").text())
             console.log('end debug price')

      }
    $( "#medical_concern" ).change(function() {
        
        if (this.value=='have medical concern')
        {
            
            $('#medical_info').show()
            $("medical_info_div").addClass("s_website_form_required");
            $("medical_information").prop('required',true);
        }
        else
        {
            $('#medical_info').hide()
            $("medical_info_div").removeClass("s_website_form_required");
            $("medical_information").prop('required',false);
        }


      });
    
      $( "#continue_button" ).click(function() {
        if ($('#email').val()=='')
        {   
            $('#validation_message').text('merci de saisir un email ou un mobile ou un téléphone')
            $('#validation_message').show()
        }
        else if ($('#mobile').val()=='' && $('#phone').val()=='')
        {   
            $('#validation_message').text('afin de pouvoir vous contacter merci de saisir un mobile ou un téléphone')
            $('#validation_message').show()
        }
        else if ($('#name').val()=='')
        {
             
                $('#validation_message').text('merci de saisir un nom')
                $('#validation_message').show()
                
                
        }
        else if ($('#firstname').val()=='')
        {   
                $('#validation_message').text('merci de saisir un prénom')
                $('#validation_message').show()
                
                
        }
        else if ($('#age').val()=='')
        {   
            $('#validation_message').text('merci de saisir l\'age du participant')
            $('#validation_message').show()
            
        }
        else if ($('#city').val()=='')
        {   
            $('#validation_message').text('merci de saisir la ville du participant')
            $('#validation_message').show()
            
        }
        else if ($('#gender').val()=='')
        {   
            $('#validation_message').text('merci de sélectionner le sexe du participant')
            $('#validation_message').show()
            
            
        }
           
        else
        {
            $('#form1').submit()
        }

            
            
        
      });

      $( "#continue_button2" ).click(function() {
        if ($('#image_permission').val()=='')
        {   
            
             $('#validation_message').text('merci de répondre à la demande d\'autorisation de l\'utilisation de votre image')
             $('#validation_message').show()
        }
        else
        {
                if ($('#medical_contact_name').val()=='')
                {   
                    $('#validation_message').text('merci de saisir le nom du contact')
                    $('#validation_message').show()
                    
                    
                }
            else if ($('#medical_contact_phone').val()=='')
            {   
                $('#validation_message').text('merci de saisir le téléphone du contact')
                $('#validation_message').show()
                
                
            }
            else
            {
                $('#form2').submit()
            }

        }
        

            
            
        
      });
    
      

    $( "#back_button2" ).click(function() {
        history.back()
      });

      $( "#back_button1" ).click(function() {
        history.back()
      });

    $(":checkbox").change(function() {
        //console.log(this.value)
        //console.log($("#id_opt_reduction1200").val())
        //console.log('status:'+$("#status").val())
        //si coche adhésion alors le statut devient membre

        
            // if ($('input[name=pre_registration_required]').is(':checked'))
            // {
               
            //     $("#continue_button").show()
            // }
            // else
            // {
            //     $("#continue_button").hide()
            // }
        

        if (this.value==$("#id_opt_individual_room").val() && this.checked)
        {
            //console.log('fff'+"#o["+$("#i_opt_reduction1200").val()+"]")
            //console.log('ggg'+$("#o1").val())
            if ($("#o"+$("#i_opt_reduction1200").val()).prop('checked')== true)
            {
                alert('Selection non autorisée')
                this.checked=false

            }
        }
        if (this.value==$("#id_opt_reduction1200").val() && this.checked)
        {
            if ($("#o"+$("#i_opt_individual_room").val()).prop('checked')== true)
            {
                alert('Selection non autorisée')
                this.checked=false
            }
        }
        if(this.value==$("#membership_product").val() && this.checked)
        {
           
            if ($("#status").val()=='not member') {$("#status2").val('member')}
         
            
        }
        //si décoche adhésion alors le statut devient non membre
        if(this.value==$("#membership_product").val() && this.checked==false)
        {
            if ($("#status").val()=='not member'){$("#status2").val('not member')}
        
        }

        console.log('status2:'+$("#status2").val())

            compute_total_price_from_status_without_options()
            compute_total_price_with_options()

            
              
             
        
    });
   

});