<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class index extends CI_Controller {
	 function __construct() {
		parent::__construct();
		header('Access-Control-Allow-Origin:*');
	    header('Access-Control-Allow-Methods:POST, GET');
	    header('Access-Control-Max-Age:0');
	    header('Access-Control-Allow-Headers:x-requested-with, Content-Type');
	    header('Access-Control-Allow-Credentials:true');
		$this->load->helper('dingtools');
		$this->AppKey = 'dingjcbxgidah9uspeuc';
		$this->AppSecret = 'C4-8rUDK1u5Twpsw7U3yo42s_bbYxFIqzLMp2j7uI80Sa8D-OPbtSoCMgZxHxo2d';
		$this->load->model('dingtools_model');
	 }
	 
	 public function index(){
		 exit('前方高能！！！');
	 }
	 
	 public function sendApproval(){
		 //接收数据
		 $groupname = $this->input->post('groupname');
		 $paytocompany = $this->input->post('paytocompany');
		 $img = $this->input->post('img');
		 $money = $this->input->post('money');
		 $postunionid = $this->input->post('postunionid');
		 $approvers = $this->input->post('approvers');
		 $message = $this->input->post('message');
		 $currency = $this->input->post('currency');
		 $channelType = $this->input->post('ChannelType');
		 $bankName = $this->input->post('BankName');
		 $bankAccount = $this->input->post('BankAccount');
		 
		 if(empty($postunionid)){
			 exit('参数(postunionid)为空');
		 }
		 
		 //获取access_token
		 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
		 $access_token = GetPost_http($access_url,'','');
		 $access_token = json_decode($access_token)->access_token;
		 $url = 'https://oapi.dingtalk.com/topapi/processinstance/create?access_token='.$access_token;
		 
		 $originator_user_id = $this->getuserid($postunionid);
		 $dept_id = $this->getdeptid($originator_user_id);
		 $user_list = explode(',',$approvers);
		 $approversidstr = '';
		 if(count($user_list) > 1){
			 foreach($user_list as $userid){
				 $approversidstr .= $this->getuserid($userid).',';
			 }
			 $approversidstr = substr($approversidstr,0,strlen($approversidstr) - 1);
		 }else{
			 $approversidstr = $this->getuserid($user_list['0']);
		 }
		 
		 $postData = array();
		 //表单code
		 $postData['process_code'] = 'PROC-9D90693F-DE34-423C-9B92-A19FFFC87577';
		 //发起人id
		 $postData['originator_user_id'] = $originator_user_id;
		 //发起人部门id
		 $postData['dept_id'] = $dept_id;
		 //审批人id
		 
		 $postData['approvers'] = $approversidstr;
		 $postData['form_component_values'] = array();
		 $postData['form_component_values']['0']['name'] = '金额';
		 $postData['form_component_values']['0']['value'] = $money;
		 $postData['form_component_values']['1']['name'] = '备注';
		 $postData['form_component_values']['1']['value'] = $message;
		 $postData['form_component_values']['2']['name'] = '团名';
		 $postData['form_component_values']['2']['value'] = $groupname;
		 $postData['form_component_values']['3']['name'] = '付款单位';
		 $postData['form_component_values']['3']['value'] = $paytocompany;
		 $postData['form_component_values']['4']['name'] = '附件';
		 $postData['form_component_values']['4']['value'] = json_decode($img);
		 $postData['form_component_values']['5']['name'] = '币种';
		 $postData['form_component_values']['5']['value'] = $currency;
		 $postData['form_component_values']['5']['name'] = '币种';
		 $postData['form_component_values']['5']['value'] = $currency;
		 $postData['form_component_values']['6']['name'] = '渠道类型';
		 $postData['form_component_values']['6']['value'] = $channelType;
		 $postData['form_component_values']['7']['name'] = '开户行';
		 $postData['form_component_values']['7']['value'] = $bankName;
		 $postData['form_component_values']['8']['name'] = '账号';
		 $postData['form_component_values']['8']['value'] = $bankAccount;
		 
		 //print_r(json_encode($postData));die();
		 $response = GetPost_http($url,json_encode($postData),'json');
		 
		 if(!empty($response)){
			 echo $response;
		 }
	 }
	 
	 public function sendRefund(){
		 //获取access_token
		 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
		 $access_token = GetPost_http($access_url,'','');
		 $access_token = json_decode($access_token)->access_token;
		 $url = 'https://oapi.dingtalk.com/topapi/processinstance/create?access_token='.$access_token;
		 
		 $postData = array();
		 $postData['process_code'] = 'PROC-95F150AE-A2A9-4B99-B2D6-C26F62EC1E06';
		 //发起人id
		 $postData['originator_user_id'] = '103218225237808971';
		 //发起人部门id
		 $postData['dept_id'] = '111848239';
		 //审批人id
		 $postData['approvers'] = '011702444537734182';
		 $postData['form_component_values'] = array();
		 $postData['form_component_values']['0']['name'] = '退款金额';
		 $postData['form_component_values']['0']['value'] = 1000;
		 
		 $approoval = GetPost_http($url,json_encode($postData),'json');
		 print_r($approoval);
	 }
	 
	 //获取用户id
	 function getuserid($unionid){
		 if(empty($unionid)){
			 exit('参数(unionid)为空');
		 }else{
			 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
			 $access_token = GetPost_http($access_url,'','');
			 $access_token = json_decode($access_token)->access_token;
			 $listurl = 'https://oapi.dingtalk.com/user/getUseridByUnionid?access_token='.$access_token.'&unionid='.$unionid;
			 
			 $userlist = GetPost_http($listurl,'','');
			 $userid = json_decode($userlist)->userid;
			 return $userid;
		 }
	 }
	 
	 //获取部门列表
	 function getdeptid($userid){
		 if(empty($userid)){
			 exit('参数(userid)为空');
		 }else{
			 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
			 $access_token = GetPost_http($access_url,'','');
			 $access_token = json_decode($access_token)->access_token;
			 $detailurl = 'https://oapi.dingtalk.com/user/get?access_token='.$access_token.'&userid='.$userid;
			 
			 $userdetail = GetPost_http($detailurl,'','');
			 $departid = json_decode($userdetail)->department;
			 return $departid['0'];
		 }
	 }
	 
	 //创建回调
	 public function registercallback(){
		 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
		 $access_token = GetPost_http($access_url,'','');
		 $access_token = json_decode($access_token)->access_token;
		 //ca4f05c2ab663cd2a65f2c50366bf8a9
		 $callback_url = 'https://oapi.dingtalk.com/call_back/register_call_back?access_token='.$access_token;
		 $post_data = array();
		 $post_data['call_back_tag'] = ['bpms_task_change','bpms_instance_change'];
		 $post_data['token'] = '123456';
		 $post_data['aes_key'] = 'abcdefgABCDEFG0123456789hHiIjJKkLlMmnNOpQrs';
		 $post_data['url'] = 'http://www.mycht.cn/dingdingcallback/receive.php';
		 
		 $register = GetPost_http($callback_url,json_encode($post_data),'json');
		 print_r($register);
	 }
	 
	 public function recivecallback(){
		$recivejson = file_get_contents('php://input');
		log_message('error',$recivejson);
		$recivedata = json_decode($recivejson);
		$data = array();
		if(strpos($recivedata->title,'团款审批')){
			$data['processInstanceId'] = $recivedata->processInstanceId;
			$data['title'] = $recivedata->title;
			$data['type'] = $recivedata->type;
			// $data['result'] = $recivedata->result;
			$unionid = $this->get_unionid($recivedata->staffId);
			$data['unionid'] = $unionid;
			$data['EventType'] = $recivedata->EventType;
			
			if(isset($recivedata->remark)){
				$data['remark'] = $recivedata->remark;
			}else{
				$data['remark'] = '';
			}
			
			if(isset($recivedata->result)){
				$data['result'] = $recivedata->result;
			}else{
				$data['result'] = '';
			}
			
			$this->dingtools_model->add_instance_id($data);
		}
		
	 }
	 
	 public function get_unionid($userid){
		 $access_url = 'https://oapi.dingtalk.com/gettoken?appkey='.$this->AppKey.'&appsecret='.$this->AppSecret;
         $access_token = GetPost_http($access_url,'','');
	     $access_token = json_decode($access_token)->access_token;
	     
		 $user_detail_url = 'https://oapi.dingtalk.com/user/get?access_token='.$access_token.'&userid='.$userid;
		 $user_detail = GetPost_http($user_detail_url,'','get');
		 $unionid = json_decode($user_detail)->unionid;
		 return $unionid;
	 }
}