<?php
/*
 * @Author: wang jun
 * @Date: 2022-01-18 10:57:14
 * @Last Modified by: wang jun
 * @Last Modified time: 2022-01-19 10:38:56
 * 微信类
 */
namespace app\index\controller;

use app\index\logic\appointmentlogic;
use app\index\logic\paylogic;
use app\index\logic\wechatlogic;
use think\Controller;
use think\Log;

class Appointment extends Base
{
    /**
     * 新建申请
     *
     * @return void
     * @author wj
     * @date 2022-07-23
     */
    public function newinfo()
    {
        $param = request()->post();
        $l_a = new appointmentlogic();
        $result = $l_a->newinfo($param);
        if (!$result['status']) {
            return backjson(0, $result['msg']);
        }
        return backjson(1, $result['data']);
    }
    /**
     * 获取用户可用数据
     *
     * @param  [type] $arr
     * @return void
     * @author wj
     * @date 2022-07-25
     */
    public function getuseablelist()
    {
        $param = request()->post();
        $l_a = new appointmentlogic();
        $result = $l_a->getuseablelist($param);
        if (!$result['status']) {
            return backjson(0, $result['msg']);
        }
        return backjson(1, $result['data']);
    }
    /**
     * 支付核酸检测费用 仅小程序支付
     *
     * @return void
     * @author wj
     * @date 2022-07-23
     */
    public function payorder()
    {
        $param = request()->post();
        $l_p = new paylogic();
        $result = $l_p->getappointmentorderforxcx($param);
        if (empty($result['status'])) {
            return backjson(0, $result['msg']);
        }
        $orderData = $result['data'];
        $l_wechat = new wechatlogic();
        $result = $l_wechat->createorder($orderData);
        if (empty($result['status'])) {
            return backjson(0, $result['msg']);
        }
        $returnData = $result['data'];
        return $returnData;
    }
    /**
     * 支付回调
     *
     * @return void
     * @author wj
     * @date 2022-07-23
     */
    public function pay_call_back()
    {
        $headerinfo = request()->instance()->header();
        log::info($headerinfo);
        $reurnData = [
            "return_code" => "SUCCESS",
            "return_msg" => "OK",
        ];
        log::info('into callback');
        $data = file_get_contents('php://input');
        $backdata = (array) simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        $l_p = new paylogic();
        $reulst = $l_p->handleorder($backdata);
        if (empty($reulst['status'])) {
            $reurnData = [
                "return_code" => "FAIL",
                "return_msg" => $reulst['msg'],
            ];
        } else {
            $reurnData = [
                "return_code" => "SUCCESS",
                "return_msg" => $reulst['msg'],
            ];
        }
        $l_wechat = new wechatlogic();
        $l_wechat->paybackxml($reurnData);
    }
}
