<?php
declare (strict_types = 1);

namespace app\admin\controller;


use app\common\controller\Backend;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;

class Contents extends Backend
{
    // todo 内容管理

    //轮播图
    public function index()
    {
        if (Request::isPost()){

            $page=input("page")?:1;
            $limit=input("limit")?:10;


            $url="http://".$_SERVER['HTTP_HOST'];
            $list=Db::name("banner")->order("id desc")->paginate(['list_rows' => $limit, 'page' => $page])->each(function ($item) use (&$url){

                $item["url"]=$url;
                return $item;
            })->toArray();
            return  $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data']?:[], 'count' => $list['total']?:0];
        }

        return View::fetch();
    }

// 添加轮播图
    public function addbanner()
    {
        $name=input("text");
        $img=input("img")?:"";
        $id=input("id");

        $add["text"]=$name;
        $add["img"]=$img;
//        $add["centos"]=$centos;

        if ($id){
            $where["id"]=$id;
            $res=Db::name("banner")->where($where)->update($add);
        }else{
            $res=Db::name("banner")->insert($add);
        }


        if ($res){
            json_result(1,"添加成功");
        }
        json_result(2,"添加失败");


    }

    public function add()
    {

        $id=input("id");
        $info=[];
        if ($id){
            $where["id"]=$id;
            $info=Db::name("banner")->where($where)->find();
        }else{
            $info["text"]='';
            $info["img"]='';
        }

        $view = [
            'info' => $info,
            'title' => lang('add'),
        ];
        View::assign($view);
        return View::fetch();
    }
// 删除轮播图
    public function del_banner()
    {
        $id=input("id");
        $where["id"]=$id;
        Db::name("banner")->where($where)->delete();
        json_result(1,"删除成功");

    }
//
//关于我们
    public function about()
    {
        if (Request::isPost()){
            $centos=input("text");
            $add["text"]=$centos;
                $ehere["id"]=1;
                Db::name("about")->where($ehere)->update($add);

            json_result(1,"修改成功");
        }
        $data=Db::name("about")->where(["id"=>1])->value("text");
        $view = [
            'info' => $data,
            'title' => lang('add'),
        ];
        View::assign($view);
       return View::fetch();
    }

    //
    //意见反馈
    public function feedback()
    {

        if (Request::isPost()){
            $name=input("name");
            $page=input("page")?:1;
            $limit=input("limit")?:10;
            $where=[];
//            if ($name){
//                $where["name"]=["like","% $name %"];
//            }
            $list=Db::name("feedback")->order("id desc")->where($where)->paginate(["list_row"=>$limit,"page"=>$page])->each(function ($item){
                return $item;

            })->toArray();
            return  $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['data']?:[], 'count' => $list['total']?:0];

        }


        return View::fetch();


    }
}