<?php

namespace Audit\Lib;

use Dever;

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

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

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

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

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

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

    public function updatemul($id, $name, $data)
    {
        //$this->audit($id, $name, $data);
        //$this->status($id, $name, $data);
        $this->push($id, $name, $data);
    }

    public function push($id, $name, $data)
    {
        $push = Dever::param('push', $data);
        if ($push) {
            $info = Dever::db('audit/data')->getIn($id);
            if ($info) {
                foreach ($info as $k => $v) {
                    if ($v['type'] > 2) {
                        continue;
                    }
                    $table = Dever::config('base')->type_table[$v['type']];

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

                    if ($source && $push == 2) {
                        $content = Dever::load('content/lib/article')->getContent($source);
                        $send['source_id'] = $v['id'];
                        $send['name'] = $source['name'];
                        $send['cate_id'] = $source['cate_id'];
                        $send['pic_cover'] = Dever::pic($source['share_pic']);
                        $send['pic_index'] = Dever::pic($source['pic_cover_169']);
                        $send['pdate'] = $source['pdate'];
                        $send['desc'] = $source['share_title'];
                        $send['template'] = $v['type'];
                        if (isset($source['video'])) {
                            $send['video'] = $source['video'];
                        }
                        
                        $send['content'] = Dever::json_encode($content['content_array']);

                        $data = Dever::load('jingpin/lib/article.up', $send);
                    }
                }
            }
        }
    }

    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));
        }
    }
}