<?php
/**
 * 在线客服系统 - 图片上传接口
 */
// 开启输出缓冲
ob_start();
include(dirname(__DIR__) . '/includes/common.php');
ob_end_clean();

@header('Content-Type: application/json; charset=UTF-8');

// 登录验证
if($islogin2 != 1) {
    exit(json_encode(array('code' => -1, 'msg' => '请先登录')));
}

$act = isset($_GET['act']) ? daddslashes($_GET['act']) : null;

switch($act) {

// ==================== 留言图片上传 ====================
case 'upload_feedback_image':
    if(!isset($_FILES['file']) || $_FILES['file']['error'] != 0){
        exit(json_encode(array('code' => -1, 'msg' => '请选择图片文件')));
    }
    $allowedTypes = array('image/jpeg','image/png','image/gif','image/webp');
    if(!in_array($_FILES['file']['type'], $allowedTypes)){
        exit(json_encode(array('code' => -1, 'msg' => '只支持 JPG、PNG、GIF、WebP 格式')));
    }
    if($_FILES['file']['size'] > 5 * 1024 * 1024){
        exit(json_encode(array('code' => -1, 'msg' => '图片大小不能超过5MB')));
    }

    $uploadDir = dirname(__DIR__) . '/assets/upload/cs/feedback/';
    if(!is_dir($uploadDir)){
        mkdir($uploadDir, 0755, true);
    }

    $ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $filename = 'fb_' . date('YmdHis') . '_' . uniqid() . '.' . $ext;
    $filepath = $uploadDir . $filename;

    if(move_uploaded_file($_FILES['file']['tmp_name'], $filepath)){
        $url = '/assets/upload/cs/feedback/' . $filename;
        exit(json_encode(array('code' => 1, 'msg' => '上传成功', 'url' => $url)));
    }else{
        exit(json_encode(array('code' => -1, 'msg' => '上传失败')));
    }
break;

default:
    exit(json_encode(array('code' => -1, 'msg' => '无效操作')));
break;
}
