<?php
session_start();
ob_start();
require_once(__DIR__ . "/../../../adbcon.php");
require_once(__DIR__ . "/../../../vendor/autoload.php");
$result = [
    'status' => 0
];
$url = trim($_REQUEST['online_url']);
$catdom_id = intval($_REQUEST['catdom_id']);

// Find info about this URL
$info = [
    'num_posts' => 0
];


$link = mysql_real_escape_string($url);
$q = "SELECT * FROM facebook_posts WHERE catdom_id='$catdom_id' AND (link='$link' OR link='$link/')";
$r = @mysql_query($q);
if($r && mysql_num_rows($r) > 0) {
    $info['num_posts'] = mysql_num_rows($r);
}



ob_start();
?>
<form method="post" action="/amodules/social-media-actions/ajax/facebook_actions.php" class="form-horizontal" id="facebook-form">
    <input type="hidden" name="post_link" value="<?=$url;?>">
    <input type="hidden" name="catdom_id" value="<?=$catdom_id;?>">
    <h3 class="header">
        <?=$url;?>
    </h3>
    <p class="alert alert-info"><span class="text-black">This URL has been posted <b><?=$info['num_posts'];?></b> times.</span></p>
    <div class="container-fluid">

        <div class="row no-margin">
            <div class="col-sm-12">
                <div class="form-group">
                    <label class="control-label">POST Message</label>
                    <textarea name="post_message" rows="4" class="form-control input-sm"></textarea>
                    <span class="help-block">If you do no type anything in the message field, Facebook will crawl the page and it will use the META DESCRIPTION.</span>
                </div>
            </div>
        </div>
    </div>
    <div class="box-footer">
        <button type="submit" class="btn btn-primary btn-sm" id="facebook-submit">
            <?=($info['num_posts'] > 0) ? 'Post Again' : 'Post';?>
        </button>
    </div>
    <div id="facebook-result" class="alert hidden"></div>
</form>
<?php

$form = ob_get_clean();
$result['form'] = $form;

ob_end_clean();

$result['status'] = 1;
$result['info'] = $info;
echo json_encode($result);
exit;