<?php
namespace app\common\listener\classes;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\school\classes\Teacher;
use app\common\model\school\classes\Visit;
use app\common\model\school\Message;
use app\common\model\school\MessageConfig;

class ClassesHook
{




    // 课程审核提交
    public function classesAuthNeedAfter(&$params)
    {
        ['classes' => $classes] = $params;
        $teacher_user = \app\common\model\User::get($classes["user_id"]);
        $teacher = Teacher::get($classes["teacher_id"]);
        $shop = ManystoreShop::get($classes["shop_id"]);
        //课程推送给老师
        $desc = "您的新课程{$classes['title']}已提交审核，审核时间为1-3日内,请耐心等待审核结果";

        $title = "新课程审核";
        $mini_type = "classes_apply";
        $to_type="user";
        $to_id = $classes["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_auth_need_after",
            "classes_lib_id"=>$classes["id"],
        ];

        $param = [
            "title" => $classes['title'],
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "address"=>$classes["address"]."(".$classes["address_detail"].")",
            "price" => $classes["price"],
            "start_time" => date("Y-m-d H:i",$classes["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$classes["end_time"]),
//            "reason" => $classes['reason'],
        ];

        //发给机构老师
        (new MessageConfig)
            ->setTemplate($params["event"]."_user")
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();
        //发给机构拥有者

        (new MessageConfig)
            ->setTemplate($params["event"]."_shop")
            ->setTemplateData($param)
            ->setToUid($shop["user_id"])
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

    }



    // 课程审核通过后
    public function classesAuthSuccessAfter(&$params)
    {
        ['classes' => $classes] = $params;
        $teacher_user = \app\common\model\User::get($classes["user_id"]);
        $teacher = Teacher::get($classes["teacher_id"]);
        $shop = ManystoreShop::get($classes["shop_id"]);

        //课程推送给老师
        $desc = "您的新课程{$classes['title']}已审核通过，可以在后台操作课程上架！";

        $title = "新课程审核成功";
        $mini_type = "classes_apply";
        $to_type="user";
        $to_id = $classes["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_auth_success_after",
            "classes_lib_id"=>$classes["id"],
        ];

        $param = [
            "title" => $classes['title'],
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "address"=>$classes["address"]."(".$classes["address_detail"].")",
            "price" => $classes["price"],
            "start_time" => date("Y-m-d H:i",$classes["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$classes["end_time"]),
//            "reason" => $classes['reason'],
        ];

        //发给机构老师
        (new MessageConfig)
            ->setTemplate($params["event"])
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();

    }

    // 课程审核失败后
    public function classesAuthFailAfter(&$params)
    {
        ['classes' => $classes] = $params;
        $teacher_user = \app\common\model\User::get($classes["user_id"]);
        $teacher = Teacher::get($classes["teacher_id"]);
        $shop = ManystoreShop::get($classes["shop_id"]);

       //课程推送给老师
        $desc = "您的新课程{$classes['title']}审核未通过，未通过原因为:{$classes['reason']},整改后,可以在后台重新提交！";

        $title = "新课程审核不通过";
        $mini_type = "classes_apply";
        $to_type="user";
        $to_id = $classes["user_id"];
        $status ="classes";
        $platform="user";
        $oper_id=0;
        $oper_type="system";
        $params=[
            "event"=>"classes_auth_fail_after",
            "classes_lib_id"=>$classes["id"],
        ];

        $param = [
            "title" => $classes['title'],
            "teacher_name"=> $teacher["name"] ?? "课程负责人",
            "teacher_realname"=> $teacher_user["realname"] ?? "课程负责人真实姓名",
            "teacher_nickname"=> $teacher_user["nickname"] ?? "课程负责人昵称",
            "teacher_mobile"=> $teacher_user["mobile"] ?? "课程负责人电话",
            "address"=>$classes["address"]."(".$classes["address_detail"].")",
            "price" => $classes["price"],
            "start_time" => date("Y-m-d H:i",$classes["start_time"]), //格式化日期格式 $order["start_time"], //格式化日期格式
            "end_time" =>   date("Y-m-d H:i",$classes["end_time"]),
            "reason" => $classes['reason'],
        ];

        //发给机构老师
        (new MessageConfig)
            ->setTemplate($params["event"])
            ->setTemplateData($param)
            ->setToUid($to_id)
            ->setMessageStatus($status)
            ->setMessageMiniType($mini_type)
            ->setMessageParams($params)
            ->sendMessage();


    }


        public function classesViewAfter(&$params)
        {
            ['classes' => $classes,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;
           //访问记录
           Visit::create([
               "classes_lib_id"=>$classes["id"],
               "shop_id"=>$classes["shop_id"],
               "classes_activity_id"=>0,
               "status"=>'1',
               "user_id"=>$user_id,
               ]);
        }

         public function classesCollectSuccessAfter(&$params)
        {
            ['classes' => $classes,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

        }

         public function classesCollectCancelAfter(&$params)
        {
            ['classes' => $classes,"user_id"=>$user_id,"oper_type"=>$oper_type,"oper_id"=>$oper_id] = $params;

        }


}