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

namespace app\admin\controller\bbfxshop\apopup;

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


class Index extends Base
{

    protected $model = null;

    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_apopup")->where($where)->order('id desc')->limit(($pindex - 1) * $psize,$psize)->select();
        $total = Db::name("bbfx_apopup")->where($where)->count();
        foreach($list as $key => &$val){
            $val['statusText'] = "禁用";
            if($val['status'] == 1){
                $val['statusText'] = "正常";
            }
            $val['createtime'] = date('Y-m-d H:i:s',$val['createtime']);
        }
        unset($val);
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function status(){
        $params = $this->param(['id/d','status/d']);
        
        if($params['status'] == 1){
            $status = 1;
        }
        else{
            $status = 0;
        }
        
        $res = Db::name("bbfx_apopup")->where("id",$params['id'])->update(['status'=>$status]);
        if($res){
            
            \app\admin\model\AdminLog::record("","",["id"=>$params['id'],'status'=>$status],"bbfx_apopup");
            
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function detail(){
        $params = $this->param();
        
        $info = Db::name("bbfx_apopup")->where("id",$params['id'])->find();
        
        
        if(empty($info)){
            __error("数据不存在");
        }
        $info['link'] = iunserializer($info['link']);
        $info['thumb'] = cdnurl($info['thumb']);
        __success('操作成功',$info);
    }
    
    public function post(){
        $params = $this->param();
        $data = array(
            "uniacid"=>UNIACID,
            "name"=>$params['name'],
            "thumb"=>replaceSiteroot($params['thumb']),
            "link"=>iserializer($params['link']),
            "status"=>intval($params['status']),
            "width"=>intval($params['width']),
            "tanchu"=>intval($params['tanchu']),
            "is_gzh_show"=>intval($params['is_gzh_show']),
        );
        
        if($data['width'] > 100){
            $data['width'] = 100;
        }
        
        if(empty($data['name'])){
            __error("请输入活动单页名称");
        }
        if($data['width'] <= 0){
            __error("请输入活动单页名称");
        }
        if(empty($data['thumb'])){
            __error("请选择单页图片");
        }
        if(!empty($params['id'])){
            $res = Db::name("bbfx_apopup")->where("id",$params['id'])->update($data);
            $data['id'] = $params['id'];
        }
        else{
            $data['createtime'] = time();
            $res = Db::name("bbfx_apopup")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_apopup");
            
            __success("操作成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete("bbfx_apopup");
    }
}