<?php

namespace app\admin\controller\ContactUs;

use app\BaseController;
use app\common\model\ContactUs\ContactScancode as ModelContactScancode;
use app\Request;
use think\Validate;
use think\exception\ValidateException;
use think\facade\Filesystem;
use app\common\arw\adjfut\src\Excel;
use app\common\arw\adjfut\src\UploadFile;
use app\common\exception\Tool;
use think\facade\Db;
use think\facade\Env;


class ContactScancode extends BaseController
{
    /**
     * 获取联系扫码列表
     */
    public function getContactScancodeList(Request $request): array
    {
        $params = $request->param();
        $con = [];

        $con = Tool::getOptionalQuery();

        $query = ModelContactScancode::where($con)
            ->field([
                'contact_scancode_id',
                'contact_scancode_guid',
                'contact_scancode_title',
                'contact_scancode_status',
                'contact_scancode_icon',
                'contact_scancode_img',
                'contact_scancode_order'
            ])
            ->order('contact_scancode_order', 'asc');

        return msg("获取联系扫码列表成功!", $query);
    }

    /**
     * 编辑联系扫码
     */
    public function editContactScancode(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'contact_scancode_title|联系扫码标题' => 'require'

        ]);
        $model = ModelContactScancode::where('contact_scancode_guid', $params['contact_scancode_guid'])->find();
        if (!$model) throwErrorMsg("该联系扫码不存在", 1);
        $model->allowField([
            'contact_scancode_update_user_guid',
            'contact_scancode_title',
            'contact_scancode_status',
            'contact_scancode_icon',
            'contact_scancode_img',
            'contact_scancode_order'
        ])->save($params);
        return msg('编辑成功!');
    }

    /**
     * 添加联系扫码
     */
    public function addContactScancode(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'contact_scancode_title|联系扫码标题' => 'require',
            'contact_scancode_order|联系扫码排序' => 'require'
        ]);
        $model = ModelContactScancode::create($params, [
            'contact_scancode_guid',
            'contact_scancode_create_user_guid',
            'contact_scancode_update_user_guid',
            'contact_scancode_title',
            'contact_scancode_img',
            'contact_scancode_status',
            'contact_scancode_icon',
            'contact_scancode_order'
        ]);
        return msg('添加成功!');
    }

    /**
     * 删除联系扫码
     */
    public function deleteContactScancode(Request $request): array
    {
        $params = $request->param();
        $this->validate($params, [
            'contact_scancode_guid' => 'require',
        ]);
        $contact_scancode = ModelContactScancode::where([
            'contact_scancode_guid' => explode(',', $params['contact_scancode_guid'])
        ])->select();
        $contact_scancode->delete();
        return msg('删除成功!');
    }
}
