<?php
declare (strict_types = 1);

namespace app\model\api;
use app\model\api\ServiceType as ServiceTypeModel;

use think\Model;

/**
 * @mixin \think\Model
 */
class TypeAudit extends Model
{
    /**
     * 获取列表
     * @param type $post
     * @param type $is_admin
     */
    public function getList($post,$is_admin=0){
        $post["pageSize"] = $post["pageSize"]>50 ? 50 : (int)$post["pageSize"];
        $post["page"]     = $post["page"]<=0     ? 1  : (int)$post["page"];
        $where=[];
        if(is_numeric($post['status'])) {
            $where[]=["au.status","=",(int)$post['status']];
        }
        if(is_numeric($post['uid']) && $post['uid']>0) {
            $where[]=["au.uid","=",(int)$post['uid']];
        }else if(is_mobile($post['user_mobile'])){
            $udata = (new UserModel)->where("mobile",$post['user_mobile'])->find();
            if(!empty($udata)){
                $where[]=["au.uid","=",(int)$udata['uid']];
            }
        }
//        if(is_mobile($post['mobile'])) {
//            $where[]=["au.mobile","=",$post['mobile']];
//        }
        $startTime = "";
        $endTime = "";
        if(!empty($post['time'][0]) && !empty($post['time'][1])) {
           $startTime =  strtotime($post['time'][0]);
           $endTime = strtotime($post['time'][1]);
           $where[]=["au.time","between","{$startTime},{$endTime}"];
        }
//        if(!empty($post['keyword'])){
//            $where[]=["au.name","=",$post['keyword']];
//        }
        $pageCount = $this->alias("au")->where($where)->count();
        $data = null;
        if($pageCount>0){
            $userField = $is_admin==1 ? ",u.mobile as user_mobile,u.nickname as user_nickname,u.avatar as user_avatar,a.name as admin_name" : "";
            $data = $this
                ->alias("au")
                ->field("au.*{$userField}")
                ->leftJoin("user u"          , "u.uid  = au.uid")//用户信息
                ->leftJoin("admin a"         , "a.id   = au.admin_id")//管理员
                ->where($where)
                ->order("au.id","desc")
                ->page((int)$post["page"], $post["pageSize"])
                ->select()
                ->toArray();
        }
        $data = empty($data) ? [] : $data;
//        $serviceTypeModel = new ServiceTypeModel();
        foreach($data as $k=>$v){
            $data[$k]["time"]               = date("Y-m-d H:i:s",$data[$k]["time"]);//添加时间
            $data[$k]["service_audit_imgs"] = getImageAr($data[$k]["service_audit_imgs"]);//认证图片
            $data[$k]["admin_time"] = empty($data[$k]["admin_time"])?"":date("Y-m-d H:i:s",$data[$k]["admin_time"]);
        }
        return ["list" => $data, "pageSize" => $post["pageSize"],"page"=>$post["page"],"totalCount"=>$pageCount];
    }
    /**
     * 
     * @param type $id
     * @param type $type
     */
    public function getItem($post,$is_admin=0){
        if(empty($post)){
            return [];
        }
        $where=[];
        if(isset($post["status"]) && in_array((string)$post["status"], ["0","1","-1"])){
            $where[]=["au.status","=",(int)$post["status"]];
        }
        if(!empty($post["id"])){
            $where[]=["au.id","=",$post["id"]];
        }
        if(!empty($post["uid"])){
            $where[]=["au.uid","=",$post["uid"]];
        }
        //后台
        $userField = $is_admin==1 ? ",u.mobile as user_mobile,u.nickname as user_nickname,u.avatar as user_avatar,a.name as admin_name" : "";
        $data = $this
                ->alias("au")
                ->field("au.*{$userField}")
                ->leftJoin("user u"              ,"u.uid = au.uid")//用户信息
                ->leftJoin("admin a"             , "a.id = au.admin_id")//管理员
                ->where($where)
                ->order("au.id","desc")
                ->find();
        if(empty($data)){
            return [];
        }
        $data = $data->toArray();
        $data["time"]               = date("Y-m-d H:i:s",$data["time"]);//添加时间
        $data["service_audit_imgs"] = getImageAr($data["service_audit_imgs"]);//我的证书
        if($is_admin==0){
            unset($data["admin_id"]);
            unset($data["admin_mono"]);
            unset($data["admin_time"]);
            unset($data["uid"]);
        }else{
            $data["admin_time"] = empty($data["admin_time"])?"":date("Y-m-d H:i:s",$data["admin_time"]);
        }
        return $data;
        
    }
}
