<?php
namespace app\admin\controller;

use think\App;
use think\Db;
use think\Exception;

class Settlement extends Auth
{

    protected $status = [
        0=>'待分配',
        1=>'进行中',
        2=>'已完成',
        3=>'已作废',
        4=>'已结算'
    ];
    public function index(){
        if(request()->isAjax()){
            //分页参数
            $length = input('rows',10,'intval');   //每页条数
            $page = input('page',1,'intval');      //第几页
            $start = ($page - 1) * $length;     //分页开始位置
            //排序
            $sortRow = input('sidx','u.id','trim');      //排序列
            $sort = input('sord','desc','trim');        //排序方式
            $order = 'a.id desc';
            $title = input('title','','trim');//用户
            if($title){
                $user = Db::name('user')
                    ->alias('u')
                    ->join('user_org uo','uo.user_id = u.id')
                    ->where('u.real_name','like','%'.$title.'%')
                    ->where('uo.org_id',$this->orgId)
                    ->column('u.id');
                if(!empty($user)){
                    $map[] = ['a.user_id','in',$user];
                }else{
                    $map[] = ['a.user_id','=',0];
                }
            }
            $title1 = input('title1','','trim');//护工
            if($title1){
                $user = Db::name('worker')
                    ->where('name','like','%'.$title1.'%')
                    ->where('org_id',$this->orgId)
                    ->column('id');
                if(!empty($user)){
                    $ids = Db::name('todo')
                        ->where('worker_id','in',$user)
                        ->column('order_id');
                    if(empty($ids)){
                        $map[] = ['a.id','=',0];

                    }else{
                        $map[] = ['a.id','in',$ids];
                    }
                }else{
                    $map[] = ['a.id','=',0];
                }
            }

            $sn = input('sn','','trim');
            if($sn){
                $map[] = ['a.sn','=',$sn];
            }

            $cateId = input('cateId','','trim');
            if($cateId){
                $map[] = ['a.cate_id','=',$cateId];
            }

            $depId = input('depId','','trim');
            if($depId){
                $map[] = ['a.dep_id','=',$depId];
            }

            $status = input('status','','trim');
            if($status != ''){
                $map[] = ['a.status','=',$status];
            }

            $b = input('begin','','trim');
            $e = input('end','','trim');
            if($b){
                $b = date('Y-m-d 00:00:00',strtotime($b));
                $map[] = ['a.create_time','>=',$b];
            }

            if($e){
                $e = date('Y-m-d 23:59:59',strtotime($e));
                $map[] = ['a.create_time','<=',$e];
            }
            $map[] = ['a.status','=',2];
            $map[] = ['org_id','=',$this->orgId];

            $map= empty($map) ? true: $map;
            //数据查询
            $lists = Db::name('ph_orders')->alias('a')
                ->where($map)
                ->field('a.*')
                ->limit($start,$length)
                ->order($order)
                ->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['userName'] = Db::name('user')
                    ->where('id',$v['user_id'])
                    ->value('real_name');
                $lists[$k]['depName'] = '';
                $lists[$k]['cateName'] = '';
                if($v['cate_id'] > 0){
                    $lists[$k]['cateName'] = Db::name('cate')->where('id',$v['cate_id'])->value('title');
                }
                if($v['dep_id'] > 0){
                    $lists[$k]['depName'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
                }
            }
            int_to_string($lists,['status' => $this->status]);
            //数据返回
            $totalCount = Db::name('ph_orders')->alias('a')
                ->where($map)->count();
            $totalPage = ceil($totalCount/$length);
            $totalMoney = Db::name('ph_orders')->alias('a')
                ->where($map)->sum('a.amount');
            $result['totalMoney'] = $totalMoney;
            $result['page'] = $page;
            $result['total'] = $totalPage;
            $result['records'] = $totalCount;
            $result['rows'] = $lists;
            return json($result);
        }else{
            $cate =(new \app\common\model\Cate())->getAllByOrg($this->orgId);
            $this->assign('cate',$cate);

            $dep =(new \app\common\model\Dep())->getList($this->orgId);
            $this->assign('dep',$dep);

            $this->assign('status',$this->status);
            return $this->fetch();
        }
    }

    //订单结算
    public function js(){
        if(request()->isAjax()){
            $data = request()->post();
            $ids = $data['ids'];

            $web_bl = Db::name('config')
                ->alias('a')
                ->join('config_org b','a.id=b.config_id')
                ->where('b.org_id',$this->orgId)
                ->where('a.name','web_bl')
                ->value('b.value');
            $web_bl = floatval($web_bl)?floatval($web_bl):0;
//            if(empty($web_bl)){
//                $this->error('请先设置扣除比例');
//            }
            $web_deduct = Db::name('config')
                ->alias('a')
                ->join('config_org b','a.id=b.config_id')
                ->where('b.org_id',$this->orgId)
                ->where('a.name','web_deduct')
                ->value('b.value');
            $web_deduct = $web_deduct?$web_deduct:0;
//            if(empty($web_deduct)){
//                $this->error('请先设置结算前扣除金额');
//            }
            $res = (new \app\common\model\PhOrders())->js($ids,$web_bl,$web_deduct,$this->orgId);
            $this->success('','',$res);
        }
    }

    public function detail($id=0){
        $model = new \app\common\model\PhOrders();
        $info = $model->getInfo($id);
        if(!$info){
            $this->error('订单不存在');
        }
        $this->assign('info',$info);
        return $this->fetch();
    }
}