<?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\bestbao\controller;
use app\bestbao\model\BestbaoConfig;
use app\bestbao\model\BestbaoEngineer;
use app\common\model\SystemMemberBank;
use app\common\model\SystemMemberBankBill;

class Engineer extends Common{


    public function initialize(){
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'售后工程师','url'=>'javascript:;']]);
    }

    /**
     * 列表
     */
    public function index(){
        $view['lists'] = BestbaoEngineer::where(['member_miniapp_id' => $this->member_miniapp_id])->order('sort desc,id desc')->paginate(20);
        return view()->assign($view);
    }

    /**
     * 编辑或添加
     * @return void
     */
    public function edit(){
        if(request()->isAjax()){
            $id = $this->request->param('id/d');
            $data = [
                'uid'               => $this->request->param('uid/d'),
                'title'             => $this->request->param('title/s'),
                'occupation'        => $this->request->param('occupation/s'),
                'about'             => $this->request->param('about/s'),
                'level'             => $this->request->param('level/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'update_time'       => time(),
            ];
            $validate = $this->validate($data,'Engineer.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            if($id > 0){
                $data['id'] = $id;
                $result = BestbaoEngineer::update($data);
            }else{
                $info  = BestbaoConfig::where(['member_miniapp_id' => $this->member_miniapp_id])->find();
                if($this->member_miniapp->member_id > 0 && $info['type'] == 0 && !empty($info['price'])){
                    $rel = SystemMemberBank::moneyJudge($this->member_miniapp->member_id,$info['price']); //判断余额
                    if($rel){
                        return enjson(0,'帐号余额不足,请联系应用服务商');
                    }
                    SystemMemberBankBill::create(['state' => 1,'money' => $info['price'],'member_id' => $this->member_miniapp->member_id,'message'=> '添加工程师费用','update_time' => time()]);
                    SystemMemberBank::moneyUpdate($this->member_miniapp->member_id,-$info['price']);
                }
                $data['create_time'] = time();
                $result = BestbaoEngineer::create($data);
            }
            if($result){
                return enjson(200,'操作成功');
            }else{
                return enjson(0);
            }
        }else{
            $view['info']  = BestbaoEngineer::where(['id' => $this->request->param('id/d',0),'member_miniapp_id' => $this->member_miniapp_id])->find();
            return view()->assign($view);
        }
    }

    /**
     * 排序
     */
    public function sort(){
        if(request()->isAjax()){
            $data = [
                'sort' => $this->request->param('sort/d'),
                'id'   => $this->request->param('id/d'),
            ];
            $validate = $this->validate($data,'Engineer.sort');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = BestbaoEngineer::update(['sort'=>$data['sort']],['id' => $data['id']]);
            if($result){
                return enjson(200);
            }else{
                return enjson(0);
            }
        }
    }

    //删除
    public function delete(int $id){
        $result = BestbaoEngineer::destroy($id);
        if($result){
            return enjson(200);
        }else{
            return enjson(403,'删除失败');
        }
    }
}