<?php

class HelperBBus
{
    public $db;

    public function __construct(DoliDB $db)
    {
        $this->db = $db;
    }

    /**
     * Output the buttons to submit a creation/edit form
     *
     * @param   string  $save_label     	Alternative label for save button
     * @param   string  $cancel_label   	Alternative label for cancel button
     * @param   array   $morebuttons    	Add additional buttons between save and cancel
     * @param   bool    $withoutdiv     	Option to remove enclosing centered div
     * @param	string	$morecss			More CSS
     * @param	string	$dol_openinpopup	If the button are shown in a context of a page shown inside a popup, we put here the string name of popup.
     * @return 	string						Html code with the buttons
     */
    public function buttonsSaveCancel($save_label = 'Save', $cancel_label = 'Cancel', $cancelURL = '', $morebuttons = array(), $withoutdiv = 0, $morecss = '', $dol_openinpopup = '')
    {
        global $langs;

        $buttons = array();

        $save = array(
            'name' => 'save',
            'label_key' => $save_label,
        );

        if ($save_label == 'Create' || $save_label == 'Add') {
            $save['name'] = 'add';
        } elseif ($save_label == 'Modify') {
            $save['name'] = 'edit';
        }

        $cancel = array(
            'name' => 'cancel',
            'label_key' => 'Cancel',
        );

        !empty($save_label) ? $buttons[] = $save : '';

        if (!empty($morebuttons)) {
            $buttons[] = $morebuttons;
        }

        //!empty($cancel_label) ? $buttons[] = $cancel : '';

        $retstring = $withoutdiv ? '' : '<div class="center">';

        foreach ($buttons as $button) {
            $addclass = empty($button['addclass']) ? '' : $button['addclass'];
            $retstring .= '<input type="submit" class="button button-' . $button['name'] . ($morecss ? ' ' . $morecss : '') . ' ' . $addclass . '" name="' . $button['name'] . '" value="' . dol_escape_htmltag($langs->trans($button['label_key'])) . '">';
        }

        if ($cancel_label == "Cancel") {
            $retstring .= '<a class="button button-' . $cancel['name'] . '" href="' . dol_buildpath($cancelURL, 1) . '?restore_lastsearch_values=1' . (!empty($socid) ? '&socid=' . $socid : '') . '">' . $langs->trans($cancel['label_key']) . '</a>';
        }

        $retstring .= $withoutdiv ? '' : '</div>';

        if ($dol_openinpopup) {
            $retstring .= '<!-- buttons are shown into a $dol_openinpopup=' . $dol_openinpopup . ' context, so we enable the close of dialog on cancel -->' . "\n";
            $retstring .= '<script>';
            $retstring .= 'jQuery(".button-cancel").click(function(e) {
				e.preventDefault(); console.log(\'We click on cancel in iframe popup ' . $dol_openinpopup . '\');
				window.parent.jQuery(\'#idfordialog' . $dol_openinpopup . '\').dialog(\'close\');
				 });';
            $retstring .= '</script>';
        }

        return $retstring;
    }
}