<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 文章管理
 */
namespace app\allwin\controller;
use app\allwin\model\News as newsModel;

class News extends Common{

    public function initialize()
    {
        parent::initialize();
        $this->assign('pathMaps', [['name'=>'新闻管理','url'=>url("news/index")]]);
    }

    /**
     * 列表
     */
    public function index(){
        $condition = [];
        $view['lists'] = newsModel::where($this->mini_program)->where($condition)->order('id desc')->paginate(20);
        return view()->assign($view);
    }

    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'cate_id' => input('post.cate_id/d'),
                'types'   => input('post.types/d'),
                'title'   => input('post.title/s','','htmlspecialchars'),
                'img'     => input('post.img/s','','htmlspecialchars'),
                'desc'    => input('post.desc/s','','htmlspecialchars'),
                'content' => input('post.content/s','','htmlspecialchars'),
            ];
            $validate = $this->validate($data,'news.save');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $cate = model('NewsCate')->field('name')->where(['id' => $data['cate_id']])->find();
            $data['cate_name'] = empty($cate['name']) ? '' : $cate['name'];
            $result =  model('News')->edit($this->member_miniapp_id,$data);
            if($result){
                return json(['code'=>200,'url' => url('news/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['cate'] = model('NewsCate')->where($this->mini_program)->order('sort desc,id desc')->select();
            return view()->assign($view);
        }
    }

    /**
     * 编辑
     */
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'      => input('post.id/d'),
                'cate_id' => input('post.cate_id/d'),
                'types'   => input('post.types/d'),
                'title'   => input('post.title/s','','htmlspecialchars'),
                'img'     => input('post.img/s','','htmlspecialchars'),
                'desc'    => input('post.desc/s','','htmlspecialchars'),
                'content' => input('post.content/s','','htmlspecialchars'),
            ];
            $validate = $this->validate($data,'news.save');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $cate = model('NewsCate')->field('name')->where(['id' => $data['cate_id']])->find();
            $data['cate_name'] = empty($cate['name']) ? '' : $cate['name'];
            $result =  model('News')->edit($this->member_miniapp_id,$data);
            if($result){
                return json(['code'=>200,'url' => url('news/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $id  = input('get.id/d');
            $view['info'] = model('News')->where(['id' => $id,'member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['cate'] = model('NewsCate')->where($this->mini_program)->order('sort desc,id desc')->select();
            return view()->assign($view);
        }
    }

     /**
     * 删除
     */
    public function delete(int $id){
        $condition = [];
        $condition['id'] = $id;
        $result = model('News')->where($condition)->where($this->mini_program)->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功','data'=>[]]);
        }else{
            return json(['code'=>403,'msg'=>'操作失败']);
        } 
    }
}