<?php

namespace app\controller\api\v1;

use app\controller\api\Base;
use app\model\Account;
use app\model\AccountWorksite;
use app\model\CheckLog;
use app\model\ClockLog;
use app\model\OvertimeLog;
use app\model\Position;
use app\model\Worksite;
use app\service\Math;
use Exception;
use Lcobucci\Clock\Clock;
use think\Collection;
use think\response\Json;

/**
 * 负责人相关
 *
 * Class Manager
 * @package app\controller\api\v1
 */
class Manager extends Base
{
    protected $noNeedLogin = [];

    /**
     * 查看审核详情
     *
     * @return Json
     */
    public function checkDetail(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $id        = input('id/d');//待审核记录ID

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能查看
                return $this->json(4003, '无查看权限');
            }

            if (!$item = CheckLog::findById($id)) {
                return $this->json(4004, '记录不存在');
            }

            $item = arrayNullToString($item->toArray());

            $fields = Account::needCheckFields();
            array_push($fields, 'id', 'role', 'work_at', 'worksite_id');

            $user = Account::findById($item['account_id'], $fields)->toArray();
            $user = arrayNullToString($user);

            $positionList          = Position::column('name', 'id');
            $item['position_name'] = $positionList[$item['position']] ?? '';
            $user['position_name'] = $positionList[$user['position']] ?? '';

            // user角色为工人时,需要判断当前人是否是该工地的负责人
            if (!empty($user) && $user['role'] == Account::COMMON_ON) {
                $managerIds = AccountWorksite::where('worksite_id', $user['worksite_id'])->column('account_id');
                if (!in_array($accountId, $managerIds)) {
                    return $this->json(4003, '无查看权限2');
                }
            }

            $item['account'] = $user;

            return $this->json(0, 'success', $item);
        } catch (Exception $e) {
            return $this->json(4000, '没有相关的用户记录'.$e->getMessage());
        }
    }

    // 审核列表(员工资料提交的审核)
    public function checkList(): Json
    {
        $page    = input('page/d', 1);
        $size    = input('size/d', 20);
        $keyword = input('keyword/s');
        $status  = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无查看权限');
        }

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['cl.real_name|cl.mobile', 'like', '%'.$keyword.'%'];
        }

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = CheckLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('position p', 'p.id = cl.position')
            ->field('cl.id,cl.real_name,cl.status,cl.created_at,p.name as position_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->created_at = date('Y年m月d日 H:i:s', strtotime($item->created_at));
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核资料(工人信息注册或二次修改审核)
     *
     * @return Json
     */
    public function checkInfo(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $id        = input('id/d');//待审核记录ID
            $reason    = input('reason/s');//不通过原因

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            if (!$item = CheckLog::findById($id)) {
                return $this->json(4004, '记录不存在');
            }

            if ($item['status'] != CheckLog::COMMON_OFF) {
                return $this->json(4002, '状态错误');
            }

            $user = Account::getUser($item['account_id']);

            // user角色为工人时,需要判断当前人是否是该工地的负责人
            if (!empty($user) && $user['role'] == Account::COMMON_ON) {
                $managerIds = AccountWorksite::where('worksite_id', $user['worksite_id'])->column('account_id');
                if (!in_array($accountId, $managerIds)) {
                    return $this->json(4003, '无此权限2');
                }
            }

            // 审核不通过
            if ($type == 0) {
                Account::where('id', $item['account_id'])->save(['checking' => Account::COMMON_OFF]);
                $item->save(['status' => -1, 'refund_reason' => $reason]);
                return $this->json();
            }

            $now = date('Y-m-d H:i:s');

            $update = $item->toArray();
            if ($update['is_register'] == CheckLog::COMMON_ON) {
                $update['role']    = CheckLog::COMMON_ON;
                $update['work_at'] = $now;
            }
            $update['checking'] = Account::COMMON_OFF;
            unset($update['id']);
            unset($update['account_id']);
            unset($update['is_register']);
            unset($update['status']);
            unset($update['created_at']);
            unset($update['check_at']);
            unset($update['refund_reason']);

            Account::where('id', $item['account_id'])->save($update);
            $item->save(['status' => 1, 'check_at' => $now]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(4000, '没有相关的用户记录'.$e->getMessage());
        }
    }

    // 打卡记录
    public function clockList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无权限');
        }

        $where = [];

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = \app\model\ClockLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->field('cl.*,w.name as worksite_name,a.real_name as worker_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->type_text = $item->type == 'in' ? '上班' : '下班';
                switch ($item->status) {
                    case 0:
                        $item->status_text = '待确认';
                        break;
                    case 1:
                        $item->status_text = '已确认';
                        break;
                    case -1:
                        $item->status_text = '不通过';
                        break;
                }
                unset($item->check_by);
                unset($item->check_at);
                unset($item->account_id);
                unset($item->create_time);
                unset($item->type);
                unset($item->worksite_id);
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核打卡 支持批量
     *
     * @return Json
     */
    public function checkClock(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (ClockLog::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            ClockLog::whereIn('id', $ids)->where('status', ClockLog::COMMON_OFF)->update([
                'status'   => $type == 1 ? 1 : -1,
                'check_at' => date('Y-m-d H:i:s'),
                'check_by' => $accountId
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }

    // 加班记录
    public function overtimeList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', 0);//状态 0=待审核 1=已审核(包含1通过 -1不通过)

        $accountId = $this->request->user['user_id'] ?? 0;

        if (!$account = Account::findById($accountId, ['id, role'])) {
            return $this->json(6001, '请先登录');
        }

        if ($account['role'] <= Account::COMMON_ON) {
            return $this->json(4003, '无权限');
        }

        $where = [];

        if ($status == 0) {
            $where[] = ['cl.status', '=', 0];
        } else {
            $where[] = ['cl.status', 'in', [1, -1]];
        }

        // 负责工地
        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
        $where[]     = ['cl.worksite_id', 'in', $worksiteIds];

        $query = \app\model\OvertimeLog::alias('cl')
            ->leftJoin('account a', 'a.id = cl.account_id')
            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
            ->field('cl.*,w.name as worksite_name,a.real_name as worker_name')
            ->where($where);

        $total = $query->count();

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
            $res['list']->each(function ($item) {
                switch ($item->status) {
                    case 0:
                        $item->status_text = '待确认';
                        break;
                    case 1:
                        $item->status_text = '已确认';
                        break;
                    case -1:
                        $item->status_text = '不通过';
                        break;
                }
                $item->create_time = date('Y年m月d日 H:i:s', $item->create_time);
                $item->worktime    = date('Y年m月d日', strtotime($item->day));
                unset($item->check_by);
                unset($item->check_at);
                unset($item->account_id);
                unset($item->created_at);
                unset($item->type);
                unset($item->day);
                unset($item->day_text);
                unset($item->worksite_id);
                unset($item->is_statistic
                );
            });
            $res['list'] = arrayNullToString($res['list']->toArray());
        }

        return $this->json(0, 'success', $res);
    }

    /**
     * 审核加班 支持批量
     *
     * @return Json
     */
    public function checkOvertime(): Json
    {
        try {
            $accountId = $this->request->user['user_id'] ?? 0;
            $type      = input('type/d', 1);//类型 1=通过 0=不通过
            $ids       = input('id/s');//待审核记录ID 多个用逗号分割
            $ids       = explode(',', $ids);
            $ids       = array_filter($ids);

            if (!in_array($type, [0, 1])) {
                return $this->json(4001, '审核参数错误');
            }

            if (!$account = Account::findById($accountId, ['id, role'])) {
                return $this->json(6001, '请先登录');
            }

            if ($account['role'] <= Account::COMMON_ON) {
                // 工地负责人才能操作
                return $this->json(4003, '无此权限');
            }

            $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');
            if (OvertimeLog::whereIn('id', $ids)->whereNotIn('worksite_id', $worksiteIds)->count() > 0) {
                return $this->json(4003, '部分记录不在您权限操作的范围');
            }

            OvertimeLog::whereIn('id', $ids)->where('status', ClockLog::COMMON_OFF)->update([
                'status'   => $type == 1 ? 1 : -1,
                'check_at' => date('Y-m-d H:i:s'),
                'check_by' => $accountId
            ]);
            return $this->json();
        } catch (Exception $e) {
            return $this->json(5000, '审核失败'.$e->getMessage());
        }
    }

    // 工资记录
    public function payListMock(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', -1);
        $date   = input('data/s', '');

        $accountId = $this->request->user['user_id'] ?? 0;

        $where = [];

        if (!empty($keyword)) {
            $where[] = ['w.name', 'like', '%'.$keyword.'%'];
        }

        if ($status >= 0) {
            $where[] = ['w.status', '=', $status];
        }

        if (!empty($date)) {
            $date    = str_replace('_', '', $date);
            $where[] = ['cl.time', '=', $date];
        }

        $where[] = ['cl.account_id', '=', $accountId];
        //        $query   = \app\model\ClockLog::alias('cl')
        //            ->leftJoin('worksite w', 'w.id = cl.worksite_id')
        //            ->field('cl.*,w.name as worksite_name')
        //            ->where($where);
        //
        //        $total = $query->count();
        $total = 20;

        $res = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'info'    => [],
            'list'    => new Collection(),
        ];

        if ($total > 0) {
            $res['info'] = [
                'amount'          => 999999,//工资总金额
                'base_amount'     => 20000,//基本工资
                'overtime_amount' => 20000,//加班工资

                'not_amount'  => 20000,//未发放
                'done_amount' => 20000,//已发放
            ];
            $res['list'] = [
                [
                    'name'            => '张珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '李珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '王珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '张珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '刘珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '谢珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
                [
                    'name'            => '吴珊珊',
                    'status'          => 0,
                    'status_text'     => '待发放',
                    'base_amount'     => 7500,
                    'overtime_amount' => 0,
                    'amount'          => 7500,
                ],
            ];
        }

        return $this->json(0, 'success', $res);
    }

    // 工资记录
    public function payList(): Json
    {
        $page   = input('page/d', 1);
        $size   = input('size/d', 20);
        $status = input('status/d', -1);
        $date   = input('date/s', '');

        $accountId = $this->request->user['user_id'] ?? 0;

        $where = [];

        if (!empty($date)) {
            $date    = str_replace('_', '', $date);
            $where[] = ['pml.time', '=', $date];
        }

        $worksiteIds = AccountWorksite::where('account_id', $accountId)->column('worksite_id');

        $total = 0;
        $res   = [
            'total'   => $total,
            'current' => $page ?: 1,
            'size'    => $size ?: 20,
            'info'    => [],
            'list'    => new Collection(),
        ];

        if (empty($worksiteIds)) {
            return $this->json(0, 'success', $res);
        }

        // 当前明确说明 负责人和工地是一对一关系
        $where[] = ['pml.worksite_id', '=', $worksiteIds[0]];

        $query = \app\model\PayMonthLog::alias('pml')
            //            ->leftJoin('account a', 'a.id = pml.account_id')
            ->where($where);
        // 汇总信息
        $info = $query->fieldRaw('sum(pml.amount) as amount, sum(pml.base_amount) as base_amount,
            sum(pml.overtime_amount) as overtime_amount')->find()->toArray();
        // 已发放工资
        $doneAmount = $query->where('pml.status', 1)->fieldRaw('sum(pml.amount) as amount')->find()->toArray();

        $res['info'] = [
            'amount'          => $info['amount'] ?? 0,
            'base_amount'     => $info['base_amount'] ?? 0,
            'overtime_amount' => $info['overtime_amount'] ?? 0,
            'done_amount'     => $doneAmount['amount'] ?? 0,
            'not_amount'      => Math::sub($info['amount'] ?? 0, $doneAmount['amount'] ?? 0),
        ];

        if ($status >= 0) {
            $where[] = ['pml.status', '=', $status];
        }

        $query = \app\model\PayMonthLog::alias('pml')
            ->leftJoin('account a', 'a.id = pml.account_id')
            ->where($where);

        $total        = $query->count();
        $res['total'] = $total;

        if ($total > 0) {
            $res['list'] = $query->field('pml.id,pml.status,pml.amount,pml.base_amount,pml.overtime_amount,a.real_name as name')->page($page, $size)->order('pml.id', 'desc')->select();
            $res['list']->each(function ($item) {
                $item->status_text = $item->status == 1 ? '已发放' : '待发放';
            });
        }

        return $this->json(0, 'success', $res);
    }
}