<?php

namespace Admin\Controller;
use User\Api\UserApi as UserApi;
use Base\Service\PromoteService as PromoteService;
use Think\Controller;

/**
 * 后台首页控制器
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */
class VerifyBillController extends Controller {
    /**
     * VerifyBillController constructor.
     */
    public $companyinfo;
    public function __construct()
    {
        parent::__construct();
        $this->companyinfo = session('cp_user_auth');
        if(ACTION_NAME == "show" && isset($_REQUEST['from']) && $_REQUEST['from']=="pc"){
            //不验证
        }else{
            if (empty(session('cp_user_auth'))) {
                redirect('admin.php?s=public/cp_login.html');
            }
        }
        
    }


    /**
     *
     */
    public function index(){
        $m_title = '对账单管理';
        $l = $this->companyinfo;
        $list_data = [];
        $field = "id,company_name,withdraw_type,company_name,company_belong,statement_begin_time,statement_end_time,statement_money,platform_amount,confirm_status,confirm_log,op_time";
        if(isset($l['promote_company_id'])){
            $map = [
                "company_id"=>["in",$l['promote_company_id']], 
                "confirm_status"=>['NEQ',0], 
                "company_belong"=>['NEQ',9]
            ];
            $list_data = DM('company_statement')
            ->field($field)
            ->where($map)
            ->order('op_time desc')
            ->select();
        }
        
        if(isset($l['partner_id'])){
            $map = [
                "company_id"=>["in",$l['partner_id']], 
                "confirm_status"=>['NEQ',0], 
                "company_belong"=>9
            ];
            $partner_data = DM('company_statement')
            ->field($field)
            ->where($map)
            ->order('op_time desc')
            ->select();

            $list_data = array_merge($list_data,$partner_data);
        }
        foreach ($list_data as $k => &$v) {
            $v['statement_begin_time'] = date('Y-m-d',$v['statement_begin_time']);
            $v['statement_end_time'] = date('Y-m-d',$v['statement_end_time']);
            $v['op_time'] = date('Y-m-d H:i:s',$v['op_time']);
            $v["valid"] = "{$v['statement_begin_time']} ~ {$v['statement_end_time']}";
            $v['withdraw_type_1'] = ($v['withdraw_type'] == 1 ? "月结" :"周结");
            $v['withdraw_type_2'] = ($v['withdraw_type'] == 2 ? "补点" :"正常结算");

            if ($v['confirm_status'] == 1) {
                $v['verify_status_str'] = '未确认';
            }
            if ($v['confirm_status'] == 2) {
                $v['verify_status_str'] = '已确认';
            }
        }
        $this->assign('list_data', $list_data);
        $this->assign('meta_title', $m_title);
        $this->display();
    }

    public function view()
    {
        if(!isset($_REQUEST['id'])){
            $this->error('参数错误');
        }
        $id = $_REQUEST['id'];
        //获取基本信息
        $dbres = M("CompanyStatement","tab_")->where("id='{$id}'")->find();
        $first_party_info = json_decode($dbres['first_party_info'],true);
        $second_party_info = json_decode($dbres['second_party_info'],true);
        $statement_info = json_decode($dbres['statement_info'],true);
        
        if($dbres['pay_type'] == 2){
            $company = $second_party_info['partner'];
            $pyinfo = $second_party_info;
            $fkf_pyinfo = $first_party_info;
        }else{
            $company = $first_party_info['partner'];
            $pyinfo = $first_party_info;
            $fkf_pyinfo = $second_party_info;
        }
        $senddata = array(
            "company"=>$company,
            "payinfo"=>$pyinfo,
            "fkf_payinfo"=>$fkf_pyinfo,
            "first_part_company"=>$first_party_info['partner'],
            "second_part_company"=>$second_party_info['partner'],
            "pay_type"=>$dbres['pay_type'],
            "withdraw_type"=>$dbres['withdraw_type'],//2补丁
            "first_party_info"=>$first_party_info,
            "second_party_info"=>$second_party_info,
            "statement_info"=>$statement_info,
            "statement_count"=>array("pay_amount"=>$dbres['pay_amount'],"statement_money"=>$dbres['statement_money'],"big_ratio_money"=>convertAmountToCn($dbres['statement_money']))
        );
        $this->assign("data",$senddata);

        if($dbres["company_belong"] == 9){
            //上游
            $this->display("CompanyStatement/viewCpStatement");
        }else{
            $this->display("CompanyStatement/viewPcStatement");
        }
    }

    public function confirm()
    {
        if(!isset($_REQUEST['id'])) $this->error("参数错误");
        $id = $_REQUEST['id'];
        $dbres = M("CompanyStatement","tab_")->field("id,confirm_status,confirm_log")->where("id='{$id}'")->find();
        if($dbres['confirm_status'] != 1){
            $this->ajaxReturn(array(
                'status' => 0,
                "info"=>"已确认"
            ));
        };
        $dbres['confirm_log'] = json_decode($dbres['confirm_log'],true);
        $dbres['confirm_log']['confirm_user']=$this->companyinfo['mobile_phone'];
        $dbres['confirm_log']['confirm_time']=date("Y-m-d H:i:s");
        $dbres['confirm_log'] = json_encode($dbres['confirm_log']);
        $dbres['confirm_status']=2;
        M("CompanyStatement","tab_")->save($dbres);
        $this->ajaxReturn(array(
            'status' => 1,
            "info"=>"操作成功"
        ));
    }
    /**
     *导出excell
     * @param int id
     */
    public function export()
    {
        if(!isset($_REQUEST['id'])) $this->error("参数错误");
        $id = $_REQUEST['id'];

        $data = M("CompanyStatement","tab_")->where(['id' => $id])->find();
        $data['first_party_info'] = json_decode($data['first_party_info'], 1);//甲方
        $data['second_party_info'] = json_decode($data['second_party_info'], 1);//乙方
        $data['statement_info'] = json_decode($data['statement_info'], 1);//结算记录
        //收款方
        $all_sum_money = $data['pay_amount'];//合计平台总额
        $all_pay_amount = $data['statement_money'];//合计结算金额

        $big_all_sum_money = convertAmountToCn($data['statement_money']);//大写

        if($data['pay_type'] == 2){
            $data['receive_company'] = $data['second_party_info'];//收款方
            $data['pay_company'] = $data['first_party_info'];
        }else{
            $data['receive_company'] = $data['first_party_info'];
            $data['pay_company'] = $data['second_party_info'];
        }
        if($data['receive_company']['invoice_type'] == 0){
            $data['receive_company']['invoice_type']="无";
        } elseif ($data['receive_company']['invoice_type'] == 1) {
            $data['receive_company']['invoice_type']="专票";
        } else {
            $data['receive_company']['invoice_type']="普票";
        }
        if ($data['company_belong'] == 9) {//上游
            excelUpStreamTemplate($data, $all_sum_money, $all_pay_amount, $big_all_sum_money);
        } else {//下游
            excelDownStreamTemplate($data, $all_sum_money, $all_pay_amount, $big_all_sum_money);
        }
    }

}