<?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\smartbc\controller;
use app\smartbc\model\SmartbcInfoConfig;
use app\smartbc\model\SmartbcConfig;

class Setting extends Common{

    /**
     *  应用配置
     * @return void
     */
    public function config(){
        if(request()->isAjax()){
            $data = [
                'service_telephone'  => $this->request->param('service_telephone/s'),
                'end_time'           => $this->request->param('end_time/d', 0),
                'is_pay_open_coupon' => $this->request->param('is_pay_open_coupon/d', 0),
                'group_join'         => $this->request->param('group_join/d', 0),
                'group_member'       => $this->request->param('group_member/d', 0),
                'profit'             => $this->request->param('profit/d', 0),
            ];
            $validate = $this->validate($data,'Config.save');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result =  SmartbcConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/config')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['info']     = SmartbcConfig::getConfig($this->member_miniapp_id);
            $view['pathMaps'] = [['name' =>' 系统配置','url' => url("setting/config")]];
            return view()->assign($view);

        }
    }

    //文案配置
    public function article(){
        $info =  SmartbcConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
        if(request()->isAjax()){
            $data = [
                'shore_img'  => $this->request->param('shore_img/s'),
                'shore_text' => $this->request->param('shore_text/s'),
                'mch_text'   => $this->request->param('mch_text/s'),
                'mch_qrcode' => $this->request->param('mch_qrcode/s'),
            ];
            $validate = $this->validate($data,'Config.article');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $config['article'] = json_encode($data);
            $result = SmartbcConfig::configs($config,$this->member_miniapp_id);
            if($result){
                return enjson(200,'操作成功',['url' => url('setting/article')]);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
                return enjson(0,'操作失败');
            }
        }else{
            $view['article'] = empty($info->article) ? '' : json_decode($info->article,true);
            $view['pathMaps'] = [['name' =>' 文案配置','url' => url("setting/article")]];
            return view()->assign($view);
        }
    }
    /**
     * 模板消息配置
     */
    public function tplmsg(){
        if(request()->isAjax()){
            $data = [
                'tplmsg_order' => $this->request->param('tplmsg_order/s'),
                'tplmsg_point' => $this->request->param('tplmsg_point/s'),
            ];
            $result =  SmartbcConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'data' => ['url' => url('setting/tplmsg')],'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['config'] = SmartbcConfig::getConfig($this->member_miniapp_id);
            $view['pathMaps'] = [['name' =>' 模板消息','url' => url("setting/tplmsg")]];
            return view()->assign($view);
        }
    }

    /**
     * 增加会员制度
     * @return void
     */
    public function sla(){
        if(request()->isAjax()){
            $data['sla'] = $this->request->param('sla/s');
            $result =  SmartbcConfig::configs($data,$this->member_miniapp_id);
            if($result){
                return json(['code'=>200,'url' => url('setting/sla'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $view['config']  = SmartbcConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
            $view['pathMaps'] = [['name' =>' 服务协议','url' => url("setting/sla")]];
            return view()->assign($view);
        }
    }
}