<?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\citys\controller;
use app\citys\model\CitysAdwords;
use think\facade\Request;

class Adwords extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps', [['name'=>'广告管理','url'=>'javascript:;']]);
    }

    /**
     * 列表
     */
    public function index($group){
        $condition          = [];
        $condition['group'] = $group;
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $view['lists'] = CitysAdwords::where($condition)->order('sort desc,id desc')->paginate(20);
        $view['pathMaps']   = [['name'=>'广告管理','url'=>url("adwords/index",['group' => $group])]];
        $view['group'] = $group;
        return view('index',$view);
    }
  
    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'                => Request::param('id/d',0),
                'group'             => Request::param('group/s'),
                'open_type'         => Request::param('open_type/s','','htmlspecialchars'),
                'title'             => Request::param('title/s'),
                'link'              => Request::param('link/s'),
                'picture'           => Request::param('picture/s'),
                'member_miniapp_id' => $this->member_miniapp_id,
            ];
            $validate = $this->validate($data,'adwords.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  CitysAdwords::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('adwords/index',['group' => $data['group']]),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['info']  = CitysAdwords::where($this->mini_program)->where(['id' => Request::param('id/d',0)])->find();
            $view['group'] = Request::param('group');
            return view()->assign($view);
        }
    }

    //删除
    public function delete(int $id){
        $result = CitysAdwords::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            return enjson(200);
        }else{
            return enjson(0,'删除失败');
        } 
    }
    
    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => Request::param('sort/d'),
                'id'   => Request::param('id/d'),
            ];
            $validate = $this->validate($data,'adwords.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = CitysAdwords::update(['sort'=>$data['sort']],['member_miniapp_id' => $this->member_miniapp_id,'id' =>$data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            } 
        }
    }
}