<?php

namespace app\adminapi\controller;

use think\facade\Db;
use app\common\model\user\Task;

class TaskController extends BaseAdminController
{

    public function index()
    {
        $param = get_params();
        $param['uid'] = $this->adminId;
        [$param['page'], $param['limit']] = $this->getPage();
        [$count, $list] = (new Task())->list($param);
        return $this->success('success', ['count' => $count, 'lists' => $list]);
    }

    public function create()
    {
        $param = get_params();
        //markdown数据处理
        if (isset($param['table-align'])) {
            unset($param['table-align']);
        }
        if (isset($param['docContent_html_code'])) {
            $param['content'] = $param['docContent_html_code'];
            $param['md_content'] = $param['docContent_markdown_doc'];
            unset($param['docContent_html_code']);
            unset($param['docContent_markdown_doc']);
        }
        if (isset($param['ueditorcontent'])) {
            $param['content'] = $param['ueditorcontent'];
            $param['md_content'] = '';
        }
        $param['start_time'] = strtotime(urldecode($param['start_time']));
        if (isset($param['end_time'])) {
            $param['end_time'] = strtotime(urldecode($param['end_time']));
            $a = $param['end_time'];
            $b = date('d', $a) - date('d');
            $b = $b + 1;//验收时间加1
            $date = Strtotime(date("Y-m-d", strtotime("+1 day", $a)));//验收当天时间戳
            $c = $b * 1;//验收小时
            if ($c > 9) {
                $d = (string)round($c / 9, 1);//转换成几天
                $times = explode('.', $d);
                $date = Strtotime(date("Y-m-d", strtotime("+$times[0] day", $a)));//验收当天时间戳
                if (!empty($times[1]) && $times[1] == 9) {
                    $times = $times[0] + 1;
                    $date = Strtotime(date("Y-m-d", strtotime("+$times day", $a)));//验收当天时间戳
                    $check_time2 = $date + 32400;//早上9点
                } else {
                    $check_time2 = ($date + 32400) + (3600 * $times[0]);//准确时间
                }
            } else {
                $check_time2 = ($date + 32400) + (3600 * $c);//准确时间
            }
            $times3 = Strtotime(date('Y-m-t', time())) + 64800;
            if ($check_time2 > $times3) {
                return $this->fail('验收时间不能大于本月月底18点 跨月时间:' . date('Y-m-d H:i:s', $check_time2));
            }
            $param['check_time'] = $check_time2;
            $param['end_time'] = $param['end_time'] + 86399;
        }
        if (isset($param['flow_status'])) {
            if ($param['flow_status'] == 6) {
                $param['over_time'] = time();
            } else {
                $param['over_time'] = 0;
            }
        }
        if (!empty($param['id']) && $param['id'] > 0) {
            $task = (new Task())->detail($param['id']);
            $param['update_time'] = time();
            $param['did'] = 0;
            $res = Task::where('id', $param['id'])->strict(false)->field(true)->update($param);
            if ($res) {
                if (isset($param['name']) && $param['name'] == 'end_time') {
                    $param['real_val'] = $param['end_time'];
                }
                unset($param['name'], $param['real_val']);
            }
            return $this->success('发布成功', [], 1, 1);
        } else {
            $param['create_time'] = time();
            $param['admin_id'] = $this->adminId;
            $param['type'] = 1;
            $param['cate'] = 1;
            $param['plan_hours'] = 1;
            $param['accept_overtime'] = time() + (86400 * 3);
            $param['did'] = 0;
            if (isset($param['flow_status']) && $param['assist_check_ids'] == "") {
                $param['assist_check_ids'] = $param['admin_id'];
            }
            if ($param['end_time'] == Strtotime(date('Y-m-d'))) {
                $param['end_time'] = $param['end_time'] + 64800;
            }
            $param['initial_end_time'] = $param['end_time'];
            $sid = Task::strict(false)->field(true)->insertGetId($param);
            return $this->success('发布成功', [], 1, 1);
        }
    }

    public function view($id)
    {
        $detail = (new Task())->detail($id);
        if (empty($detail)) {
            return $this->fail('任务不存在');
        }
        $role_uid = [$detail['admin_id'], $detail['director_uid']];
        $role_edit = 'view';
        if (in_array($this->adminId, $role_uid)) {
            $role_edit = 'edit';
        }
        $file_array = Db::name('FileInterfix')
            ->field('mf.id,mf.topic_id,mf.admin_id,f.name,a.name as admin_name')
            ->alias('mf')
            ->join('File f', 'mf.file_id = f.id', 'LEFT')
            ->join('Admin a', 'mf.admin_id = a.id', 'LEFT')
            ->order('mf.create_time desc')
            ->where(array('mf.topic_id' => $id, 'mf.module' => 'task'))
            ->select()->toArray();

        $link_array = Db::name('LinkInterfix')
            ->field('i.id,i.topic_id,i.admin_id,i.desc,i.url,a.name as admin_name')
            ->alias('i')
            ->join('Admin a', 'i.admin_id = a.id', 'LEFT')
            ->order('i.create_time desc')
            ->where(array('i.topic_id' => $id, 'i.module' => 'task', 'a.delete_time' => 0))
            ->select()->toArray();
        if ($detail['is_bug'] == 0) {
            $detail['is_bug'] = '部门工作';
        } elseif ($detail['is_bug'] == 1) {
            $detail['is_bug'] = '部门协助';
        } elseif ($detail['is_bug'] == 2) {
            $detail['is_bug'] = '临时任务';
        }
        $return['bohui'] = [];
        $return['count_bohui'] = [];
        $return['detail'] = $detail;
        $return['file_array'] = $file_array;
        $return['link_array'] = $link_array;
        $return['role_edit'] = $role_edit;
        $approve_id = Db::name('flow_task')->where('task_id', $id)->value('approve_id');
        $return['flow_nodes'] = $this->get_flow_nodes($approve_id);
        return $this->success('success', $return);
    }

    //获取审核流程节点
    public function get_flow_nodes($id = 0, $type = 1)
    {
        $flows = Db::name('FlowStep')->where(['action_id' => $id, 'type' => $type, 'delete_time' => 0])->order('sort asc')->select()->toArray();
        foreach ($flows as $key => &$val) {
            $user_id_info = Db::name('Admin')->field('id,name,thumb')->where('id', 'in', $val['flow_uids'])->select()->toArray();
            foreach ($user_id_info as $k => &$v) {
                $v['check_time'] = 0;
                $v['content'] = '';
                $v['status'] = 0;
                $check_array = Db::name('FlowRecord')->where(['check_user_id' => $v['id'], 'step_id' => $val['id']])->order('check_time desc')->select()->toArray();
                if (!empty($check_array)) {
                    $checked = $check_array[0];
                    $v['check_time'] = date('Y-m-d :H:i', $checked['check_time']);
                    $v['content'] = $checked['content'];
                    $v['status'] = $checked['status'];
                }
            }

            $check_list = Db::name('FlowRecord')
                ->field('f.*,a.name,a.thumb')
                ->alias('f')
                ->join('Admin a', 'a.id = f.check_user_id', 'left')
                ->where(['f.step_id' => $val['id']])->select()->toArray();
            foreach ($check_list as $kk => &$vv) {
                $vv['check_time_str'] = date('Y-m-d :H:i', $vv['check_time']);
            }

            $val['user_id_info'] = $user_id_info;
            $val['check_list'] = $check_list;
        }
        return $flows;
    }

    //驳回($id:审批id)
    public function bohui($id)
    {
        $find = Db::name('flow_task')->where('approve_id', $id)->find();
        if ($find) {
            $param['create_time'] = time();
            $param['admin_id'] = $this->adminId;
            $param['id'] = $find['task_id'];
            $res = Task::where('id', $param['id'])->strict(false)->field(true)->update(['flow_status' => 2, 'over_time' => 0]);
            $comm = $param;
            unset($comm['id']);
            if ($res) {
                $log_data = array(
                    'module' => 'task',
                    'task_id' => $param['id'],
                    'old_content' => 3,
                    'new_content' => 2,
                    'field' => 'flow_status',
                    'action' => 'edit',
                    'admin_id' => $this->adminId,
                    'create_time' => time(),
                );
                Db::name('Log')->strict(false)->field(true)->insert($log_data);
            }
            return $this->success('驳回成功', [], 1, 1);
        } else {
            return $this->fail('驳回失败');
        }
    }


}