<?php
/**
 * Author: luzheng.liu
 * Time: 2020/10/27 23:19
 */

namespace app\index\controller;


use app\common\service\SettingService;
use app\index\BaseController;
use app\index\model\SettingModel;
use app\common\until\Until;
use think\facade\Session;
use think\Request;

class Setting extends BaseController {


    public function index() {
        $input = request()->get();
        $model = new SettingModel();
        $model->setPage((int)$input['page'] ?: 1);
        $model->setPageSize((int)$input['pageSize'] ?: 10);
        //        if($this->isAdmin()){
        //            $where = [];
        //        }else {
        //            $where[] = ['status', '=', 1];
        //        }
        $where = [];
        if (!empty($input['channel_name'])) {
            $where[] = ['channel_name', 'like', '%' . $input['channel_name'] . '%'];
        }
        $model->setWhere($where);
        $data = $model->getPageList($model);
        Until::output($data);
    }

    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read() {
        $model = new SettingModel();
        $data = $model::where(['id' => (int)input('id')])->find();
        Until::output(['info' => Until::modelToArray($data)]);
    }

    /**
     * 保存更新的资源
     *
     * @return \think\Response
     */
    public function update() {
        $input = Until::getInput();
        $rule = [
            'site_id'                 => 'require',
            'unique_key'              => 'require',
            'domain|域名'               => 'require',
            'channel_id|频道id'         => 'require',
            'channel_name|频道id'         => 'require',
            'channel_auth_code|频道授权码' => 'require',
            'redirct_url|跳转链接'        => 'require',
            'auth_redirct_url|乙方跳转链接'        => 'require',
            'id'                      => 'require'
        ];
        Until::check($rule, $input);
        $service = new SettingService();
        $service->setAuthUrl($input['channel_id'], $input['redirct_url']);
        (new SettingModel())::where(['id' => (int)$input['id']])->update([
            'site_id'           => $input['site_id'],
            'unique_key'        => $input['unique_key'],
            'domain'            => $input['domain'],
            'channel_id'        => $input['channel_id'],
            'channel_name'        => $input['channel_name'],
            'channel_auth_code' => $input['channel_auth_code'],
            'redirct_url'       => $input['redirct_url'],
            'auth_redirct_url'       => $input['auth_redirct_url'],

        ]);
        Until::output(['id' => $input['id']]);
    }

    public function save() {
        $input = Until::getInput();
        $rule = [
            'site_id'                 => 'require',
            'unique_key'              => 'require',
            'domain|域名'               => 'require',
            'channel_id|频道id'         => 'require',
            'channel_name|频道名称'       => 'require',
            'channel_auth_code|频道授权码' => 'require',
            'redirct_url|跳转链接'        => 'require',
            'auth_redirct_url|乙方跳转链接'        => 'require',
        ];
        Until::check($rule, $input);
        $service = new SettingService();
        $service->verifyChannelName($input['channel_id'], $input['channel_name']);
        $service->setAuthUrl($input['channel_id'], $input['redirct_url']);
        $id = (new SettingModel())->insertGetId([
            'site_id'           => $input['site_id'],
            'unique_key'        => $input['unique_key'],
            'domain'            => $input['domain'],
            'channel_id'        => $input['channel_id'],
            'channel_name'      => $input['channel_name'],
            'channel_auth_code' => $input['channel_auth_code'],
            'redirct_url'       => $input['redirct_url'],
            'auth_redirct_url'       => $input['auth_redirct_url'],
        ]);
        Until::output(['id' => $id]);
    }

    public function authCode($channelId) {
        $info = (new SettingService())->setAuthUrl((int)$channelId,'https://webcast.bizconfstreaming.com/soa-mdl?channelId='.$channelId);
        Until::output(['authCode' => $info['encrypt_code']]);
    }


}