<?php
declare (strict_types = 1);
namespace app\system\controller;

use app\BaseController;
use app\model\api\ShowTemplate as ShowTemplateModel;
use app\model\api\ServiceType as ServiceTypeModel;
use app\model\api\ServiceTimeType as ServiceTimeTypeModel;
use app\Request;
use library\services\UtilService;

// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 皮肤模板管理 ]
// +----------------------------------------------------------------------
// | Date: 2020-09-06 21:53
// +----------------------------------------------------------------------

class ShowTemplate extends  BaseController{
    /**
     * 商品列表
     * @param Request $request
     * @return type
     */
    public function list(Request $request) {
        $post = UtilService::getMore([
            ['is_hot', ''],
            ['is_recommend', ''],
            ['status', ''],
            ['page', 1],
            ['pageSize', 50],
        ], $request);
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $data = (new ShowTemplateModel)->getDataList($post,"*",1);
        return app('json')->success(["list" =>$data["list"], "pageSize" => $data["pageSize"],"page"=>$data["page"],"pageCount"=>$data["totalCount"]]);
    }

    
    /**
     * 添加编辑商品
     * @param Request $request
     * @return type
     */
    public function add(Request $request){
        $post = UtilService::getMore([
            ['id',''],
            ['title','','empty','请填写模板标题'],
            ['price','0','is_numeric','请输入正确的模板售价'],
            ['old_price','0','is_numeric','请输入正确的模板原价'],
//            ['imgs',[],'empty','请上传商品主图'],
            ['imgs',[]],
            ['is_hot','0'],
            ['is_recommend','0'],
            ['is_init','0'],
            ['code','','empty','请输入CODE'],
            ['status','0'],
            ['unreal_sales','0'],
        ],$request);
        $save=[];
        $save["title"]        = $post["title"];
        $save["price"]        = floatval($post["price"]);
        $save["old_price"]    = floatval($post["old_price"]);
        $save["status"]       = (int)$post["status"]      == 1 ? 1 : 0;
        $save["imgs"]         = empty($post["imgs"]) ? "" : implode(",", $post["imgs"]);
        $save["is_hot"]       = (int)$post["is_hot"]      == 1 ? 1 : 0;
        $save["is_recommend"] = (int)$post["is_recommend"]== 1 ? 1 : 0;
        $save["is_init"]      = (int)$post["is_init"]     == 1 ? 1 : 0;
        $save["code"]         = $post["code"];
        $save["unreal_sales"] = (int)$post["unreal_sales"];
        $save["admin_time"]   = time();
        $save["admin_id"]     = $request->adminInfo['id'];
        if($save["price"]<0 || $save["old_price"]<0){
            return app('json')->fail("请输入正确的价格");
        }
        
        $id = empty($post["id"])?0:(int)$post["id"];
        $codeWhere[]=["code","=",$save["code"]];
        if($id>0){
            $codeWhere[]=["id","<>",$id];
        }
        $count = (new ShowTemplateModel)->where($codeWhere)->where("id","<>",$id)->count();
        if($count>0){
            return app('json')->fail("当前code已存在");
        }
        $r=0;
        if(empty($id) || $id=="0"){
            $save["time"] = time();
            $r = (new ShowTemplateModel)->insert($save);
        }else{
            $r = (new ShowTemplateModel)->where("id",$id)->update($save);
        }
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }
    
    
    
    
    
    
    
     /**
     * 服务类型列表
     * @param Request $request
     * @return type
     */
    public function serviceType(Request $request) {
        $post =  UtilService::getMore([
            ['page',1],
            ['pageSize', 50],
            ['title',''],
            ['status',''],
        ],$request);
        $data = (new ServiceTypeModel)->getList($post);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }

    /**
     * 添加服务内容
     */
    public function serviceTypeAdd(Request $request){
        $post = UtilService::getMore([
            ['id','0'],
            ['title','','empty','请填写名称'],
            ['seq','0'],
            ['content',''],
            ['img',''],
            ['status','0']
        ],$request);
        $id = $post["id"];
        unset($post["id"]);
        $r=0;
        if(empty($id) || $id=="0"){
            $r = (new ServiceTypeModel)->insert($post);
        }else{
            $r = (new ServiceTypeModel)->where("id",$id)->update($post);
        }
        
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }

    /**
     * 分类删除
     * @param Request $request
     */
    public function serviceTypeDel(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        return app('json')->success("暂不支持删除");
        $bool =   (new ServiceTypeModel)->where("id",$id)->delete();
        return app('json')->success("删除成功");
    }
    
    
    /**
     * 服务时长类型
     * @param Request $request
     */
    public function serviceTimeTypeList(Request $request){
        $post =  UtilService::getMore([
            ['page',1],
            ['pageSize', 50],
            ['title',''],
            ['status',''],
        ],$request);
        $data = (new ServiceTimeTypeModel)->getList($post);
        return app('json')->success([
            'list'      => $data["list"],
            'pageCount' => $data["totalCount"],
            'pageSize'  => $data["pageSize"],
            'page'      => $data["page"],
        ]);
    }
    
    /**
     * 添加服务时长类型
     */
    public function serviceTimeTypeAdd(Request $request){
        $post = UtilService::getMore([
            ['id','0'],
            ['seq','0'],
            ['code','','empty','请填写code'],
            ['title','','empty','请填写名称'],
            ['content',''],
            ['status','0'],
            ['img','']
            
        ],$request);
        $id = $post["id"];
        unset($post["id"]);
        $r=0;
        if(empty($id) || $id=="0"){
            $r = (new ServiceTimeTypeModel)->insert($post);
        }else{
            $r = (new ServiceTimeTypeModel)->where("id",$id)->update($post);
        }
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }

    /**
     * 分类删除
     * @param Request $request
     */
    public function serviceTimeTypeDel(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        return app('json')->success("暂不支持删除");
        $bool =   (new ServiceTimeTypeModel)->where("id",$id)->delete();
        return app('json')->success("删除成功");
    }
    
    
}