<?php
/**
 * Created by PhpStorm.
 * User: 思维定制
 * Date: 2018/9/5
 * Time: 18:03
 */

namespace App\Http\Controllers\Web;


use App\Models\MedicalOrderModel;
use Illuminate\Support\Facades\Cache;
use App\Models\MedicalSmsConfModel;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function index(Request $request){

        $conf['nationality'] = '民族';
        $conf['name'] = '姓名';
        $conf['native_place'] = '籍贯';
        $conf['email'] = '邮件';
        $conf['work'] = '工作';
        $conf['mobile'] = '电话';
        $conf['address'] = '通讯地址';
        $conf['verify'] = '验证码';


        if($request->method() == 'POST') {
            $data = $request->input();
            $code = Cache::get($data['mobile'].'post');
            foreach ($data as $key=>$val){
                if(empty($val)&&$key!='sample_type'&&$key!='sex'){
                    $res['state'] = 0;
                    $res['err'] = $conf[$key].'不能为空！';
                    return $this->api($res);
                }
            }
            /*if($code !== $data['verify']){
                $res['state'] = 0;
                $res['err'] = '验证码错误！';
                return $this->api($res);
            }*/
            $check = MedicalOrderModel::where('mobile',$data['mobile'])->first();
            if($check&&!empty($check->pdf)){
                $res['state'] = 2;
                $res['err'] = '您已提交';
                $res['redirect_url'] = url('web/Index/post',array('id'=>$check->id));
                $res['pdf'] = $check->pdf;
                return $this->api($res);
            }
            unset($data['verify']);
            $query = MedicalOrderModel::create($data);
            if($query){
                $res['state'] = 1;
                $res['err'] = '提交成功';
                $res['redirect_url'] = url('web/Index/post',array('id'=>$query->id));
            }else{
                $res['state'] = 0;
                $res['err'] ='网络错误';
            }
            return $this->api($res);
        }else{
            return view('web.order.index');
        }
    }
    public function post(){
        return view('web.order.post');
    }

    /**
     ***聚合数据
     ***DATE:2015-05-25
     */
    public function smsJuHe(Request $request)
    {
        $data = $request->input();
        $conf = MedicalSmsConfModel::find(1);
        header('content-type:text/html;charset=utf-8');

        $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
        $num = rand(1000,9999);
        Cache::put($data['mobile'].'post',$num,10000);
        $smsConf = array(
            'key'   => $conf->apikey, //您申请的APPKEY
            'mobile'    => $data['mobile'], //接受短信的用户手机号码
            'tpl_id'    => $conf->temp_id, //您申请的短信模板ID，根据实际情况修改
            'tpl_value' =>'#code#=1234&#company#='.$conf->sign //您设置的模板变量，根据实际情况修改
        );

        $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信
        //$content=false;
        if($content){
            $result = json_decode($content,true);
            $error_code = $result['error_code'];
            if($error_code == 0){
                //状态为0，说明短信发送成功
                $res['state'] = 1;
            }else{
                //状态非0，说明失败
                $msg = $result['reason'];
                $res['err'] = "短信发送失败(".$error_code.")：".$msg;
                $res['state'] = 0;
            }
        }else{
            $res['state'] = 0;
            $res['err'] = '发送失败';
        }
        return $this->api($res);
    }

    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $params [请求的参数]
     * @param  int $ipost [是否采用POST形式]
     * @return  string
     */
    function juhecurl($url,$params=false,$ispost=0){
        $httpInfo = array();
        $ch = curl_init();
        curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
        curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
        curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
        curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
        curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
        if( $ispost )
        {
            curl_setopt( $ch , CURLOPT_POST , true );
            curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
            curl_setopt( $ch , CURLOPT_URL , $url );
        }
        else
        {
            if($params){
                curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
            }else{
                curl_setopt( $ch , CURLOPT_URL , $url);
            }
        }
        $response = curl_exec( $ch );
        if ($response === FALSE) {
            //echo "cURL Error: " . curl_error($ch);
            return false;
        }
        $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
        $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
        curl_close( $ch );
        return $response;
    }
}