<?php

namespace Audit\Lib;

use Dever;

class Core
{
    public function audit($id, $name, $data)
    {
        $audit = Dever::param('audit', $data);
        $info = Dever::db('audit/data')->one($id);
        $table = Dever::config('base')->type_table[$info['type']];

        $source = Dever::db($table)->one($info['data_id']);

        if ($source && $source['audit'] != $audit) {
            Dever::config('base')->after = false;
            Dever::db($table)->update(array('where_id' => $info['data_id'], 'audit' => $audit));
        }
    }

    public function status($id, $name, $data)
    {
        $status = Dever::param('status', $data);
        $info = Dever::db('audit/data')->one($id);
        $table = Dever::config('base')->type_table[$info['type']];

        $source = Dever::db($table)->one($info['data_id']);

        if ($source && $source['status'] != $status) {
            Dever::config('base')->after = false;
            Dever::db($table)->update(array('where_id' => $info['data_id'], 'status' => $status));
        }
    }

    public function reorder($id, $name, $data)
    {
        $reorder = Dever::param('reorder', $data);
        $info = Dever::db('audit/data')->one($id);
        $table = Dever::config('base')->type_table[$info['type']];

        $source = Dever::db($table)->one($info['data_id']);

        if ($source && $source['reorder'] != $reorder) {
            Dever::config('base')->after = false;
            Dever::db($table)->update(array('where_id' => $info['data_id'], 'reorder' => $reorder));
        }
    }
}