<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/

namespace app\admin\controller\bbfxshop\news;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class Article extends Base
{
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\News;
    }
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        
        $where = array("uniacid"=>UNIACID);
        
        $list = Db::name("bbfx_news_article")->where($where)->order('id desc')->limit(($pindex - 1) * $psize,$psize)->select();
        $total = Db::name("bbfx_news_article")->where($where)->count();
        
        $cates = Db::name("bbfx_news_category")->where(['uniacid'=>UNIACID,'type'=>1])->select();
        $cates = array_skey($cates,"id");
        foreach ($list as $key => &$val){
            $val['thumb'] = cdnurl($val['thumb']);
            $val['author_avatar'] = cdnurl($val['author_avatar']);
            if(!empty($cates[$val['category_id']])){
                $val['category_name'] = $cates[$val['category_id']]['title'];
            }
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
        }
        unset($val);
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function status(){
        $params = $this->param();
        
        $res = Db::name("bbfx_news_article")->where("id",$params['id'])->update(['status'=>$params['status']]);
        if($res){
            \app\admin\model\AdminLog::record("","",["id"=>$params['id'],'status'=>$params['status']],"bbfx_news_article");
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function detail(){
        $params = $this->param();
        
        $info = Db::name("bbfx_news_article")->where("id",$params['id'])->find();
        
        if(empty($info)){
            __error("文章不存在");
        }
        $info['thumb'] = cdnurl($info['thumb']);
        $info['author_avatar'] = cdnurl($info['author_avatar']);
        
        __success('操作成功',$info);
    }
    
    public function post(){
        $params = $this->param();
        $data = array(
            "uniacid"=>UNIACID,
            "title"=>$params['title'],
            "introduction"=>$params['introduction'],
            "thumb"=>trim($params['thumb']),
            "author"=>$params['author'],
            "author_avatar"=>trim($params['author_avatar']),
            "content"=>htmlspecialchars_decode($this->param('content/h')),
            "link"=>$params['link'],
            "type"=>intval($params['type']),
            "category_id"=>intval($params['category_id']),
            "status"=>intval($params['status']),
            "browse"=>intval($params['browse'])
        );
        $data['thumb'] = replaceSiteroot($data['thumb']);
        $data['author_avatar'] = replaceSiteroot($data['author_avatar']);
        if(!empty($params['id'])){
            $res = Db::name("bbfx_news_article")->where("id",$params['id'])->update($data);
            $data['id'] = $params['id'];
        }
        else{
            $data['createtime'] = time();
            $res = Db::name("bbfx_news_article")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_news_article");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete("bbfx_news_article");
    }
}