<?php

namespace app\adminapi\controller\v1\setting;

use app\adminapi\controller\AuthController;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;
use crmeb\services\UtilService as Util;
use crmeb\services\QrcodeService;
use app\models\user\User;
use app\models\user\BindUser;
use app\models\system\MerchantDailyReport;

class PushBinding extends AuthController
{
	/**
	 * 生成h5页面二维码
	 */
	public function get_qrcode()
	{
		try {
			$token = time();
			// $urlCode = QrcodeService::getQrcodePath('qrcode_' . $this->merId . '_' . time() . '.jpg', '/api/wechat/wxAuth?mer_id=' . $this->merId, false, $this->merId);
            $urlCode = QrcodeService::getQrcodePath('qrcode_' . $this->merId . '_' . time() . '.jpg', '/bind/auth.html?token=' . $token . '&mer_id=' . $this->merId, false, $this->merId);
            return $this->success(['code_src' => $urlCode]);
        } catch (Exception $e) {
            return $this->fail('查看推广二维码失败！', ['line' => $e->getLine(), 'meassge' => $e->getMessage()]);
        }
	}

	/**
	 * 获取绑定推送用户列表
	 */
	public function getBindUserList()
	{
		$where = Util::getMore([
            ['page', 1],
            ['limit', 20],
            ['nickname', ''],
        ]);
        $where['mer_id'] = $this->merId ?: '';
        return $this->success(BindUser::systemPage($where));
	}

	/**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        if ($status == '' || $id == 0) return $this->fail('参数错误');
        BindUser::where(['id' => $id])->update(['status' => $status]);
        return $this->success($status == 0 ? '关闭成功' : '开启成功');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        if (!$id) return $this->fail('数据不存在');
        $bindUser = BindUser::get($id);
        if (!$bindUser) return $this->fail('数据不存在!');
        if ($bindUser['is_del']) return $this->fail('已删除!');
        $data['is_del'] = 1;
        if (!BindUser::edit($data, $id))
            return $this->fail(BindUser::getErrorInfo('删除失败,请稍候再试!'));
        else
            return $this->success('删除成功!');
    }

    /**
     * 发送商户报表模板消息
     */
    public function sendMerchantMessage()
    {
    	MerchantDailyReport::sendMerchantMessage();
    }
}