<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | [ 系统配置 ]
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller;


use app\BaseController;
use app\Request;
use library\services\UtilService;
use app\model\api\Sys as SysModel;
use app\model\api\Bank as BankModel;


class Sys extends BaseController
{

    /**
     * 基本设置
     */
    public function index(){
        $sys = (new SysModel)->getDataInfo();
        return app('json')->success($sys);
    }
    /**
     * 编辑用户条款
     * @param Request $request
     */
    public function agreeSave(Request $request){
        $post = UtilService::getMore([
            ['user_agreement', '','empty','请输入用户协议'],
            ['privacy_policy', '','empty','请输入用户隐私声明'],
        ], $request);
        (new SysModel())->saveSys($post);
        return app('json')->success("数据保存成功");
    }
    /**
     * 保存数据
     */
    public function save(Request $request){
        $saveType = $request->param("saveType", "sys");
        if($saveType=="sys"){
            $post = UtilService::getMore([
                ['title', ''],
                ['system_url', ''],
                ['tag', ''],
                ['custom_tel', ''],
//            ['ip_income_per',0,'empty','IP商品收益百分比大于0'],
//            ['tree_income_per',0],
//            ['tree_income_per_two',0],
//            ['money_to_score_per',0,'empty','消费转积分百分比大于0'],
//            ['tx_limit_money',0],
//            ['tx_process_per',0],
//            ['tx_process_max',0],
            ], $request);
            if(!empty($post["custom_tel"])){
                $post["custom_tel"] = str_replace("，", ",", $post["custom_tel"]);
            }
            (new SysModel())->saveSys($post);
        }
        if($saveType=="weixin"){
            $post = UtilService::getMore([
                ['appid', ''],
                ['secret', ''],
                ['mchid', ''],
                ['apiv2key', ''],
                ['apiclient_key', ''],
                ['apiclient_cert', ''],
                ['notify_url', ''],
            ], $request);
            (new SysModel())->saveWxConfig($post);
        }
        if($saveType=="share"){
            $post = UtilService::getMore([
                ['title', ''],
                ['img', ''],
                ['content', ''],
                ['query', ''],
            ], $request);
            (new SysModel())->saveShareConfig($post);
        }
        if($saveType=="invite"){
            $post = UtilService::getMore([
                ['img', ''],
            ], $request);
            (new SysModel())->saveInviteConfig($post);
        }
        return app('json')->success("数据保存成功");
    }
    
    /**
     * 提现方式列表
     * @param Request $request
     * @return type
     */
    public function bankList(Request $request) {
        $pageSize = 50;
        $post =  UtilService::getMore([
            ['page',1],
            ['status',''],
        ],$request);
        $where=[];
        if((string)$post["status"]!=""){
            $where[]=["status","=",(int)$post["status"] == 1 ? 1 : 0];
        }
        $data = (new BankModel)
                ->where($where)
                ->page((int)$post["page"], (int)$pageSize)
                ->order("seq","desc")
                ->order("id","desc")
                ->select()
                ->toArray();
        $pageCount = (new BankModel)->where($where)->count();
        $data = empty($data)?[]:$data;
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post["page"]
        ]);
    }
    /**
     * 添加编辑提现方式
     * @param Request $request
     */
    public function bankSave(Request $request){
        $post = UtilService::getMore([
            ['id','0'],
            ['name','','empty','请填写银行名称'],
            ['code','','empty','请填写银行编码'],
            ['seq','0'],
            ['status','0']
        ],$request);
        $id = (int)$post["id"];
        unset($post["id"]);
        $post["seq"] = intval($post["seq"]);
        $post["status"] = intval($post["status"])==1?1:0;
        $r=0;
        $count = (new BankModel)->where("id","<>",$id)->where("code",$post["code"])->count();
        if($count>0){
            return app('json')->fail("银行编码不能重复");
        }
        
        if(empty($id)){
            $r = (new BankModel)->insert($post);
        }else{
            $r = (new BankModel)->where("id",$id)->update($post);
        }
        if($r){
            return app('json')->success("数据保存成功");
        }else{
            return app('json')->fail("数据保存失败");
        }
    }
    /**
     * 删除提现方式
     * @param Request $request
     */
    public function bankDel(Request $request) {
        [$id] = UtilService::getMore([
            ['id',0,'empty','参数错误']
        ],$request,true);
        $bool =   (new BankModel)->where("id",$id)->delete();
        return app('json')->success("删除成功");
    }
    

}