<?php

namespace app\admin\service\setting;

use app\admin\service\AdminBaseService;
use app\model\ServiceSettingModel;

class ServiceService extends AdminBaseService
{

    public function index()
    {
        try {
            $rows = [];
            $list = ServiceSettingModel::order('id', 'desc')->select();
            if (!$list->isEmpty()) {
                $rows = $list->toArray();
            }
            return $this->toData('0', 'SUCCESS', ['list' => $rows, 'total' => count($rows), 'extend' => ['type_list' => ServiceSettingModel::$typeList]]);
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }
    }

    public function add($param)
    {
        try {
            if (empty($param['type']) || !in_array($param['type'], array_keys(ServiceSettingModel::$typeList))) {
                return $this->toData('1', '类型 无效', []);
            }

            if (empty($param['header_photo']) || !is_string($param['header_photo'])) {
                return $this->toData('1', '头像图片地址无效', []);
            }

            if (empty($param['server_name']) || !is_string($param['server_name'])) {
                return $this->toData('1', '客服名称无效', []);
            }

            if (!empty($param['server_link']) && !is_string($param['server_link'])) {
                return $this->toData('1', '类型内容 无效', []);
            }

            $service = new ServiceSettingModel;
            $service->header_photo = $param['header_photo'];
            $service->type = $param['type'];
            $service->server_link = $param['server_link'] ?? '';
            $service->server_name = $param['server_name'];
            $service->save();
            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }
    }

    public function edit($param)
    {
        try {
            if (empty($param['id']) || !is_numeric($param['id'])) {
                return $this->toData('1', '目标不存在', []);
            }
            $service = ServiceSettingModel::where('id', $param['id'])->find();
            if (empty($service)) {
                return $this->toData('1', '目标不存在', []);
            }

            if (empty($param['type']) || !in_array($param['type'], array_keys(ServiceSettingModel::$typeList))) {
                return $this->toData('1', '类型 无效', []);
            }

            if (empty($param['header_photo']) || !is_string($param['header_photo'])) {
                return $this->toData('1', '头像图片地址无效', []);
            }

            if (empty($param['server_name']) || !is_string($param['server_name'])) {
                return $this->toData('1', '客服名称无效', []);
            }

            if (!empty($param['server_link']) && !is_string($param['server_link'])) {
                return $this->toData('1', '类型内容 无效', []);
            }
            $service->type = $param['type'];
            $service->header_photo = $param['header_photo'];
            $service->server_link = $param['server_link'] ?? '';
            $service->server_name = $param['server_name'];
            $service->save();
            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }
    }


    public function del($param)
    {
        try {
            if (empty($param['id']) || !is_numeric($param['id'])) {
                return $this->toData('1', '参错错误');
            }

            $lang = ServiceSettingModel::where('id', $param['id'])->find();
            if (empty($lang)) {
                return $this->toData('1', '目标不存在');
            }

            $lang->delete();
            return $this->toData('0', 'SUCCESS');
        } catch (\Exception $exception) {
            return $this->toData('1', '系统繁忙', []);
        }
    }
}