<?php
namespace SabaiApps\Directories\Component\Voting\Controller\Admin;

use SabaiApps\Directories\Component\Form\Controller;
use SabaiApps\Directories\Component\Form\Form;
use SabaiApps\Directories\Context;

class ViewVotes extends Controller {

	protected function _doGetFormSettings( Context $context, array &$storage ) {
		$type_impl              = $this->Voting_Types_impl( $context->voting_type );
		$this->_submitButtons[] = array(
			'#btn_label' => ( $submit_label = $type_impl->votingTypeInfo( 'vote_table_submit_label' ) )
				? $submit_label
				: __( 'Delete', 'directories-pro' ),
			'#btn_color' => 'danger',
			'#btn_size'  => 'lg',
		);
		$this->_ajaxOnSuccess         = 'function (result, target, trigger) {
    var $table = $(".drts-voting-votes"),
        $row = $table.find(`tr[data-row-id="${result.entity_id}"]`);
    if (!$row.length) return;
    if (!result.current) {
        if (result.updated) DRTS.Form.deleteTableRow($row);
        return;
    }
    var current = result.current;
    $row.find(".drts-voting-vote-num").text(current.count);
    $row.find(".drts-voting-vote-sum").text(current.sum);
    $row.find(".drts-voting-vote-average").text(parseFloat(current.average).toFixed(2));
    $row.find(".drts-voting-vote-level").text(current.level);
    if ($.effects && $.effects.effect.highlight) {
        $row.find("> td").effect("highlight", {}, 1000);
    }
}';
		$this->_ajaxOnSuccessRedirect = false;

		$table_headers = $type_impl->votingTypeInfo( 'vote_table_headers' );
		unset( $table_headers['entity'] );
		$votes   = $this->_application->getModel( 'Vote', 'Voting' )
			->fieldName_is( 'voting_' . $context->voting_type )
			->entityId_is( $context->entity->getId() )
			->marked_is( 0 )
			->fetch();
		$options = array();
		foreach ( $votes as $vote ) {
			$options[ $vote->id ] = $type_impl->votingTypeTableRow( $vote, $table_headers );
		}

		return array(
			'votes'       => array(
				'#type'          => 'tableselect',
				'#header'        => $table_headers,
				'#options'       => $options,
				'#default_value' => array_keys( $options ),
				'#class'         => 'drts-data-table',
				'#size'          => 'sm',
				'#multiple'      => true,
				// '#disabled' => true,
			),
			'entity_id'   => array(
				'#type'          => 'hidden',
				'#default_value' => $context->entity->getId(),
			),
			'entity_type' => array(
				'#type'          => 'hidden',
				'#default_value' => $context->entity->getType(),
			),
			'voting_type' => array(
				'#type'          => 'hidden',
				'#default_value' => $context->voting_type,
			),
		);
	}

	public function submitForm( Form $form, Context $context ) {
		$updated = false;
		if ( ! empty( $form->values['votes'] ) ) {
			$field_name = 'voting_' . $context->voting_type;
			if ( $this->Voting_Types_impl( $context->voting_type )->votingTypeInfo( 'vote_deletable' ) === false ) {
				$criteria = $this->getModel( null, 'Voting' )->createCriteria( 'Vote' )
					->fieldName_is( $field_name )
					->entityId_is( $context->entity->getId() )
					->id_in( $form->values['votes'] );
				$this->getModel( null, 'Voting' )->getGateway( 'Vote' )->updateByCriteria( $criteria, array( 'vote_marked' => 1 ) );
			} else {
				$this->getModel( 'Vote', 'Voting' )->fieldName_is( $field_name )
					->entityId_is( $context->entity->getId() )
					->id_in( $form->values['votes'] )
					->delete();
			}
			$vote_stats = $this->Voting_Votes_recalculate( $context->entity, $field_name );
			$updated    = true;
		}
		$context->setSuccess(
			null,
			array(
				'entity_id' => $context->entity->getId(),
				'current'   => $vote_stats[''] ?? null,
				'updated'   => $updated,
			)
		);
	}
}
