<?php
namespace myresto\controllers;

use Yii;
use yii\web\Controller;
use myresto\models\ImportModifier;
use myresto\models\SyncConfig;

class ImportModifierController extends Controller {
    public function actionIndex() {
        $model = new ImportModifier();
        $tipe_tabel = $model::$tipe_tabel;
        $conn = SyncConfig::getConnection();
        $config = SyncConfig::find()->where(['type_con' => 'resto'])->one();
        $access_token = $config->api_key;

        $idHeaderLog = $_POST['idHeaderLog'];

        $stmt = mysqli_query($conn, "SELECT id_header FROM sync_to_jurnal WHERE id_header < '$idHeaderLog' ORDER BY id_header DESC LIMIT 1");
        $row = mysqli_fetch_assoc($stmt);
        $lastheader = $row['id_header'];

        $result = $model->ambilData("import", $tipe_tabel);

        if ($result != NULL) {
            $response = $model->kirimData($result, $access_token);
            $simpan = $model->simpanStatus($response, $tipe_tabel, $idHeaderLog, $lastheader);
            $echo = $simpan;
        } else {
            $echo = "mod";
        }

        return $echo;
    }
}