<?php
namespace app\index\controller;

use think\Db;

class Statistics extends Base
{

    public function apply(){
        $map['org_id'] = $this->orgId;
        $map['del'] = 0;

        $cur = date('Y-m');
        $curTime = strtotime($cur.'-01');
        $month = [];
        for ($i=5;$i>0;$i--){
            $date = strtotime("-".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
        $month[] = $cur;

        $counts = [];
        foreach ($month as $k=>$v){
            $nextmonth = date('Y-m',strtotime(" +1 month",strtotime($v.'-01'))).'-01 00:00:00';
            $c = Db::name('apply')
                ->where($map)
                ->where('create_time','>=',$v.'-01 00:00:00')
                ->where('create_time','<',$nextmonth)
                ->count();
            $counts[] = $c;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }

    public function approval(){
        $map['org_id'] = $this->orgId;
        $map['del'] = 0;

        $lists = Db::name('approval')->where($map)->field('id,title')->select();
        $data = [];
        $legendData = [];

        foreach ($lists as $k=>$v){
            $legendData[] = $v['title'];
            $map['approval_id'] = $v['id'];
            $val = [
                'name' => $v['title'],
                'value' => Db::name('apply')->where($map)->count()
            ];
            $data[] = $val;
        }

        $json = [
            'legend' => $legendData,
            'data' => $data
        ];
        ajax_return_ok($json);
    }

    public function contract(){
        $map['org_id'] = $this->orgId;
        $map['del'] = 0;
        $map['status'] = 2;

        $cur = date('Y-m');
        $curTime = strtotime($cur.'-01');
        $month = [];
        for ($i=5;$i>0;$i--){
            $date = strtotime("-".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
        $month[] = $cur;

        $counts = [];
        foreach ($month as $k=>$v){
            $nextmonth = date('Y-m',strtotime(" +1 month",strtotime($v.'-01'))).'-01 00:00:00';
            $c = Db::name('apply')
                ->where($map)
                ->where('advanced','in','3,4')
                ->where('create_time','>=',$v.'-01 00:00:00')
                ->where('create_time','<',$nextmonth)
                ->count();
            $counts[] = $c;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }

    public function contractmoney(){
        $map['org_id'] = $this->orgId;
        $map['del'] = 0;
        $map['status'] = 2;

        $cur = date('Y-m');
        $curTime = strtotime($cur.'-01');
        $month = [];
        for ($i=5;$i>0;$i--){
            $date = strtotime("-".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
        $month[] = $cur;

        $advanced = '3,4';

        $counts = [];
        foreach ($month as $k=>$v){
            $money = $this->getApplyMoney($map,$v,$advanced);
            $counts[] = $money;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }

    public function contractpay(){
        $map['org_id'] = $this->orgId;
        $map['del'] = 0;
        $map['status'] = 2;

        $cur = date('Y-m');
        $curTime = strtotime($cur.'-01');
        $month = [];
        for ($i=5;$i>0;$i--){
            $date = strtotime("-".$i." month",$curTime);
            $month[] = date('Y-m',$date);
        }
        $month[] = $cur;
        $advanced = '5';
        $counts = [];
        foreach ($month as $k=>$v){
            $money = $this->getApplyMoney($map,$v,$advanced);
            $counts[] = $money;
        }
        $data = [
            "month" => $month,
            "counts" => $counts
        ];
        ajax_return_ok($data);
    }

    private function getApplyMoney($map,$v,$advanced){
        $nextmonth = date('Y-m',strtotime(" +1 month",strtotime($v.'-01'))).'-01 00:00:00';
        $lists = Db::name('apply')
            ->where($map)
            ->where('advanced','in',$advanced)
            ->where('create_time','>=',$v.'-01 00:00:00')
            ->where('create_time','<',$nextmonth)
            ->field('id,form_json')
            ->select();
        $money = 0;
        foreach ($lists as $v){
            $formJson = json_decode($v['form_json'],true);
            foreach ($formJson as $kk=>$vv){
                if($vv['componentName'] == 'ddjjcontractfield'||$vv['componentName'] == 'ddaddcontractfield'||$vv['componentName'] == 'ddpaycontractfield'){
                    foreach ($vv['components'] as $kkk=>$vvv){
                        if($vvv['idx'] === 3){
                            $money += $vvv['values'];
                        }
                    }
                    break;
                }
            }
        }
        return $money;
    }

}