<?php
    // فیلد های اول فرم نقد و بررسی 
    add_filter( 'comment_form_defaults', 'render_pros_cons_fields_for_anonymous_users', 10, 1 );
    add_action( 'comment_form_top', 'render_pros_cons_fields_for_authorized_users' );
    function get_pros_cons_fields_html() {
      ob_start();
      ?>

    <?php
    $con = 0;
    $aid = get_the_ID() ;
    ?>
    <div class="row mt-2 <?php if(!is_user_logged_in()): ?>w-705 mx-auto <?php endif; ?>mb-3">
      <?php if ( class_exists( 'WCK_Page_Creator' ) ): ?>
      <?php foreach( get_cfc_meta( 'product-pr' ) as $key => $value ): ?>
        <?php $autho = get_cfc_field( 'product-pr','product_val_atrr', false, $key ); ?>
        <?php $dai = $aid . 'at' . $con ; ?>
        
        <div class="col-6">
            <div class="pcf-container position-relative <?php if(!is_user_logged_in()): ?>w-75 mx-auto <?php endif; ?>mb-3">
            <label for="<?php echo $dai ; ?>"><?php echo $autho; ?></label>
            <input type="range" id="<?php echo $dai ; ?>" name="<?php echo $dai ; ?>" min="1" max="100" value='' class="slider my-2 <?php if(!is_user_logged_in()): ?>mx-auto <?php endif; ?>mb-3">
            <p class="position-absolute top-0 lef30">مقدار امتیاز شما: <span id="<?php echo $dai ; ?><?php echo $con; ?>"></span></p>
            </div>
        </div>
    <script>
    var slider<?php echo $con; ?> = document.getElementById("<?php echo $dai ; ?>");
    var output<?php echo $con; ?> = document.getElementById("<?php echo $dai ; ?><?php echo $con; ?>");
    output<?php echo $con; ?>.innerHTML = slider<?php echo $con; ?>.value;

    slider<?php echo $con; ?>.addEventListener('input', function (evt) {
        output<?php echo $con; ?>.innerHTML = this.value;
    });

    </script>
    <?php $con += 1 ;  endforeach; ?>
    <?php endif;  ?>
    </div> 
      <div class="pcf-container <?php if(!is_user_logged_in()): ?>w-75 mx-auto <?php endif; ?>mb-3">
        <div class="pcf-field-container <?php if(!is_user_logged_in()): ?>w-80 mx-auto <?php endif; ?>mb-3">
          <label for="pros">نقاط قوت:</label>
          <input id="pros" name="pros" class="form-control rounded-pill govat ps-1" value='' placeholder='نقاط قوت را با فشردن اینتر از هم جدا کنید'>
        </div>
        <div class="pcf-field-container <?php if(!is_user_logged_in()): ?>w-80 mx-auto<?php endif; ?>">
          <label for="cons">نقاط ضعف:</label>
          <input id="cons" name="cons" class="form-control rounded-pill zaaf ps-1" value='' placeholder='نقاط ضعف را با فشردن اینتر از هم جدا کنید'>
        </div>
      </div>
      <?php   
      return ob_get_clean();
    }
    function render_pros_cons_fields_for_authorized_users() {
     if ( class_exists( 'woocommerce' ) ){
        if ( ! is_product() || ! is_user_logged_in() ) {
          return;
        }
     }
      
      echo get_pros_cons_fields_html();
    }

    function render_pros_cons_fields_for_anonymous_users() {
      if ( class_exists( 'woocommerce' ) ){
        if ( ! is_product() || is_user_logged_in() ) {
          return;
        }
      }
      
      echo get_pros_cons_fields_html();
    }
    // ارسال فیلد ها به سمت دیتابیس

    add_action( 'comment_post', 'save_review_pros_and_cons', 10, 3 );

    function save_review_pros_and_cons( $comment_id, $approved, $commentdata ) {
      // The pros and cons fields are not required, so we have to check if they're not empty
      $pros = isset( $_POST['pros'] ) ? $_POST['pros'] : '';
      $cons = isset( $_POST['cons'] ) ? $_POST['cons'] : '';

      $con = 0;
      $aid = $comment_id;
      $comment_id_cus = get_comment( $comment_id ); 
      $comment_post_id = $comment_id_cus->comment_post_ID ;
      $meta_valuess = get_cfc_meta( 'product-pr', $comment_post_id  );

      foreach($meta_valuess as $meta_values):
      $dai = $comment_post_id . 'at' . $con ;
      $daii = isset( $_POST[$dai] ) ? $_POST[$dai] : '';  
      update_comment_meta( $comment_id , $dai , esc_html( $daii ) );
      $con += 1 ; 
      endforeach;  
      
      // Spammers and hackers love to use comments to do XSS attacks.
      // Don't forget to escape the variables
      update_comment_meta( $comment_id, 'pros', esc_html( $pros ) );
      update_comment_meta( $comment_id, 'cons', esc_html( $cons ) );
    }

    // نمایش سمت ادمین فیلد های اختصصای کاوه برای نقد و بررسی
    
    add_action( 'add_meta_boxes_comment', 'extend_comment_add_meta_box', 10, 1 );
    function extend_comment_add_meta_box( $comment ) {
      // We don't need to show this metabox if a comment doesn't belong to a product
      $post_id = $comment->comment_post_ID;
      $product = wc_get_product( $post_id );
      
      if ( $product === null || $product === false ) {
        return;
      }
      
      add_meta_box( 'pcf_fields', 'نقد و بررسی اختصاصی کاوه', 'render_pcf_fields_metabox', 'comment', 'normal', 'high' );
      
    }

    function render_pcf_fields_metabox ( $comment ) {
        $pros = get_comment_meta( $comment->comment_ID, 'pros' , true );
        $cons = get_comment_meta( $comment->comment_ID, 'cons' , true );
        wp_nonce_field( 'pcf_metabox_update', 'pcf_metabox_nonce', false );
        ?>
        <p>
            <label for="phone">نقاط قوت:</label>
            <input type="text" name="pros" id="pros" value="<?php echo esc_attr( $pros ); ?>" class="widefat" />
        </p>
        <p>
            <label for="cons">نقاط ضعف:</label>
            <input type="text" name="cons" id="cons" value="<?php echo esc_attr( $cons ); ?>" class="widefat" />
        </p>
        
      <?php
        $con = 0;
        $aid = $comment_id;
        $comment_id_cus = get_comment( $comment_id ); 
        $comment_post_id = $comment_id_cus->comment_post_ID ;
        $meta_valuess = get_cfc_meta( 'product-pr', $comment_post_id  );

        foreach($meta_valuess as $meta_values):  
        $dai = $comment_post_id . 'at' . $con ; 
        $daii = get_comment_meta( $comment->comment_ID, $dai , true );
        ?>    
        <p>
            <label for="<?php echo $dai; ?>">امتیاز <?php echo $meta_values['product_val_atrr'] ?>:</label>
            <input type="number" name="<?php echo $dai; ?>" id="<?php echo $dai; ?>" value="<?php echo $daii; ?>" class="widefat" />
        </p>
        <?php
        $con += 1 ; 
        endforeach;  

     
    }
    add_action( 'edit_comment', 'save_pcf_changes', 10, 1 );
    function save_pcf_changes( $comment_id ) {
      // First of all, let's validate the nonce
      if ( ! isset( $_POST['pcf_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['pcf_metabox_nonce'], 'pcf_metabox_update') ) {
        wp_die( 'You can not do this action' );
      }
      
      if ( isset( $_POST['pros'] ) ) {
        $pros = $_POST['pros'];
        update_comment_meta( $comment_id, 'pros', esc_html( $pros ) );
      }
      
      if ( isset( $_POST['cons'] ) ) {
        $cons = $_POST['cons'];
        update_comment_meta( $comment_id, 'cons', esc_html( $cons ) );
      }

      $con = 0;
      $aid = $comment_id;
      $comment_id_cus = get_comment( $comment_id ); 
      $comment_post_id = $comment_id_cus->comment_post_ID ;
      $meta_valuess = get_cfc_meta( 'product-pr', $comment_post_id  );

      foreach($meta_valuess as $meta_values):  
      $dai = $comment_post_id . 'at' . $con ; 
      if ( isset( $_POST[$dai] ) ) {
        $sai = $_POST[$dai];
        update_comment_meta( $comment_id, $dai, esc_html( $sai ) );
      }
      $con += 1 ; 
      endforeach; 
        
    }

    // پایان بخش اختصاص نقد و بررسی محصولات کاوه