<?php
    if(isset($this->agency)){
        $update = true;
        $a = $this->agency;
    }else{
        $update = false;
        $a = null;
    }
?>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

<form action="<?= ($update)?'/plugin/RealAdvice/Agency/edit':'/plugin/RealAdvice/Agency/create' ?>" method="post" enctype="multipart/form-data" >
    <fieldset>
    <legend>Informations</legend>
    <div>
            <label for="uid">uid</label>
            <input type="text" readonly="" value="<?= ($update)?$a->getUid():\Website\Tool\RealAdvice::generateUid() ?>" id="uid" name="uid">
    </div>
    <div>
        <input type="hidden" readonly="" value="<?= ($update)?$a->getId():'' ?>" id="id" name="id">
    </div>
    <div>
        <label for="name">nom</label>
        <input type="text" value="<?= ($update)?$a->getName():'' ?>" id="name" name="name">
    </div>
    <div>
        <label for="email">email</label>
        <input type="text" value="<?= ($update)?$a->getEmail():'' ?>" id="email" name="email">
    </div>
    <div>
        <label for="phone">téléphone</label>
        <input type="text" value="<?= ($update)?$a->getPhone():'' ?>" id="phone" name="phone">
    </div>
    <div>
        <label for="address">adresse</label>
        <input type="text" value="<?= ($update)?$a->getAddress():'' ?>" id="address" name="address">
    </div>
    <div>
        <label for="cp">code postal</label>
        <input type="text" value="<?= ($update)?$a->getCp():'' ?>" id="cp" name="cp">
    </div>
        <div>
            <label for="short_description">description fr</label>
            <input type="text" value="<?= ($update)?$a->getShort_description_fr():'' ?>" id="short_description_fr" name="short_description_fr">
        </div>
        <div>
            <label for="long_description_fr">longue description fr</label>
            <textarea id="long_description_fr" name="long_description_fr" style="display: block;height: 300px;width: 100%;"><?= ($update)?$a->getLong_description_fr():'' ?></textarea>
        </div>

        <div>
            <label for="short_description_nl">description NL</label>
            <input type="text" value="<?= ($update)?$a->getShort_description_nl():'' ?>" id="short_description_nl" name="short_description_nl">
        </div>
        <div>
            <label for="long_description_nl">longue description NL</label>
            <textarea id="long_description_nl" name="long_description_nl" style="display: block;height: 300px;width: 100%;"><?= ($update)?$a->getLong_description_nl():'' ?></textarea>
        </div>
    <div>
        <label for="website">Site internet</label>
        <input type="text" value="<?= ($update)?$a->getWebsite():'' ?>" id="website" name="website">
    </div>
    <div>
        <label for="facebook">Facebook</label>
        <input type="text" value="<?= ($update)?$a->getFacebook():'' ?>" id="facebook" name="facebook">
    </div>
    <div>
        <label for="twitter">Twitter</label>
        <input type="text" value="<?= ($update)?$a->getTwitter():'' ?>" id="twitter" name="twitter">
    </div>
    <div>
        <label for="linkedin">LinkedIn</label>
        <input type="text" value="<?= ($update)?$a->getLinkedin():'' ?>" id="linkedin" name="linkedin">
    </div>
    <div>
        <label for="seerus_key">Clé seerus</label>
        <input type="text" value="<?= ($update)?$a->getSeerus_key():'' ?>" id="seerus_key" name="seerus_key">
    </div>
    <div>
        <label for="seerus_custom_url">custom url seerus</label>
        <input type="text" value="<?= ($update)?$a->getSeerus_custom_url():'' ?>" id="seerus_custom_url" name="seerus_custom_url">
    </div>
    <div>
        <label for="seerus_secret">Clé secrète seerus</label>
        <input type="text" value="<?= ($update)?$a->getSeerus_secret():'' ?>" id="seerus_secret" name="seerus_secret">
    </div>
</fieldset>

    <fieldset>
        <legend>Heures</legend>
        <?= $this->inc('/snippets/edit-opening-hours', array("agency" => $a)) ?>
    </fieldset>

    <?php
    if ($update && file_exists($_SERVER['DOCUMENT_ROOT'] . '/upload/' . $a->getUid() . '/logo.png'))
        echo '<img id="logo" style="display:block" height=150 width=275 src="http://' . $_SERVER['SERVER_NAME'] . '/upload/' . $a->getUid() . '/logo.png?' . time() . '" />';
    ?>

    <input style="display:block" type="file" name="logo" id="logo">


    <input type="submit" value="<?= ($update)?'update':'create' ?>">
    <div class="result"></div>

</form>



<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script>
    $("form").submit(function(e){
        e.preventDefault();

        for (instance in CKEDITOR.instances) {
            CKEDITOR.instances[instance].updateElement();
        }

        var form = e.target,
            fd = new FormData( form );

        if ($('#logo')[0].length){
            $.each( $('#logo')[0].files, function(key, value)
            {
                fd.append(key, value);
            });
        }



        $.ajax({
            url : form.action,
            type : 'POST',
            processData: false,
            contentType: false,
            data : fd,
            success : function(response){
                var r = JSON.parse(JSON.stringify(response));

                if (r.success) $('.result').text('Done !');
                else $('.result').text(r.message);
            }
        });

        return false;
    })
</script>

<script>
    CKEDITOR.replace( 'long_description_fr' );
    CKEDITOR.replace( 'long_description_nl' );
</script>