<?php

namespace app\product\controller;

use app\product\model\ProductAccount as AccountModel;

class AccountAddress
{


    public function index()
    {
        $info = $_GET;
        $url = 'http://city.webok.me/apichange/?user='.$info['user'].'&password='.$info['password'].'&type=getline';
        $current_url = 'http://city.webok.me/apichange/?user='.$info['user'].'&password='.$info['password'].'&type=current_line';

        //获取已经选择的地区
        $selected_address = json_decode(file_get_contents($current_url),true);;
        $selected_id = explode(',',$selected_address['data']['default_line']);
        unset($selected_id[count($selected_id)-1]);

        $data = json_decode(file_get_contents($url),true);
        //对已经选择的地区进行处理
        $list = [];
        foreach($data['data'] as $info) {
            if(in_array($info['id'],$selected_id)){
                $list[] = $info['name'].'-'.$info['id'];
            }
        }
        $data['list'] = $list;
        echo json_encode($data);
    }

    public function index2()
    {
        $info = $_GET;
        $url = 'http://city.webok.me/apichange2/?user='.$info['user'].'&password='.$info['password'].'&type=getline';
        $current_url = 'http://city.webok.me/apichange2/?user='.$info['user'].'&password='.$info['password'].'&type=current_line';

        //获取已经选择的地区
        $selected_address = json_decode(file_get_contents($current_url),true);;
        $selected_id = explode(',',$selected_address['data']['default_line']);
        unset($selected_id[count($selected_id)-1]);

        $data = json_decode(file_get_contents($url),true);
        //对已经选择的地区进行处理
        $list = [];
        foreach($data['data'] as $info) {
            if(in_array($info['id'],$selected_id)){
                $list[] = $info['name'].'-'.$info['id'];
            }
        }
        $data['list'] = $list;
        echo json_encode($data);
    }

    public function select()
    {
        $data = json_decode(file_get_contents("php://input"),true);
        $add_text = '';
        $add_id = '';
        $account = $data['account'];
        $password = $data['password'];
        foreach ($data['list'] as $info) {
            $temp = explode('-',$info);
            $add_text .= $temp[0].',';
            $add_id .= $temp[1].',';
        }
        $url = 'http://city.webok.me/apichange/?user='.$account.'&password='.$password.'&type=edit&current_line_list='.$add_text.'&line_id_list='.$add_id;

        $content = linkcurl($url,'GET',[],[],0);
        
        echo $content;
    }

    public function indexXigua()
    {
        $info = $_GET;
        $url = 'http://www.yzip.vip/apichange/?user='.$info['user'].'&password='.$info['password'].'&type=getline';
        $current_url = 'http://www.yzip.vip/apichange/?user='.$info['user'].'&password='.$info['password'].'&type=current_line';

        //获取已经选择的地区
        $selected_address = json_decode(file_get_contents($current_url),true);;
        $selected_id = explode(',',$selected_address['data']['default_line']);
        unset($selected_id[count($selected_id)-1]);

        $data = json_decode(file_get_contents($url),true);
        //对已经选择的地区进行处理
        $list = [];
        foreach($data['data'] as $info) {
            if(in_array($info['id'],$selected_id)){
                $list[] = $info['name'].'-'.$info['id'];
            }
        }
        $data['list'] = $list;
        echo json_encode($data);
    }

    public function selectXigua()
    {
        $data = json_decode(file_get_contents("php://input"),true);
        $add_text = '';
        $add_id = '';
        $account = $data['account'];
        $password = $data['password'];
        foreach ($data['list'] as $info) {
            $temp = explode('-',$info);
            $add_text .= $temp[0].',';
            $add_id .= $temp[1].',';
        }
        $url = 'http://www.yzip.vip/apichange/?user='.$account.'&password='.$password.'&type=edit&current_line_list='.$add_text.'&line_id_list='.$add_id;

        $content = linkcurl($url,'GET',[],[],0);
        
        echo $content;
    }
    

    //火狐ip
    public function getList()
    {

        $login_url = 'http://106.52.152.33:8899/main/raduserLogin';

        $params = array(
            "username" => $_GET['user'],
            "password" => $_GET['password'],
        );

        $login_content = linkcurl($login_url,'POST',$params);

        preg_match_all('/set\-cookie:([^\r\n]*); path/i', $login_content, $m);

        $cookie = $m[1][0];

        $url = 'http://106.52.152.33:8899/panel/index';
        $header = array();
        $header[] = "Cookie:".$cookie;
        $content = linkcurl($url,'POST',[],$header);

        //获取所有大分类
        preg_match_all("/<tr(.*?)<\/tr>/is", $content, $result);
        //只获取ipmodel的数据
        preg_match_all("/<td>(.*?)<\/td>/is", $result[1][1], $ipmodel);
        //获取城市数据
        preg_match_all("/<td id=\"pools\">(.*?)<a/is", $result[1][2], $city);

        $data = [
            'ipmodel' => $ipmodel[1],
            'city' => $city[1],
        ];

        // print_r($data);

        echo json_encode($data);
    }

    public function setAddress()
    {
        
        $data = json_decode(file_get_contents("php://input"),true);

        $login_url = 'http://106.52.152.33:8899/main/raduserLogin';

        $params = array(
            "username" => $data['account'],
            "password" => $data['password'],
        );

        $login_content = linkcurl($login_url,'POST',$params);

        preg_match_all('/set\-cookie:([^\r\n]*); path/i', $login_content, $m);

        $cookie = $m[1][0];

        $url = 'http://106.52.152.33:8899/panel/index';
        $params = array(
            "act" => 'save',
            "username" => $data['account'],
            "ipmode" => $data['ipmode'],
            "citys[]" => implode(',',$data['list']),
        );
        $header = array();
        $header[] = "Cookie:".$cookie;
        $data = linkcurl($url,'POST',$params,$header);

        if (strpos($data, '提交成功') !== false) {
            echo json_encode(['code'=>1]);
        }
    }

    
    //专用界面登录
    public function login()
    {
        $post = json_decode(file_get_contents("php://input"),true);
        if (empty($post['account']))
        {
            echo json_encode(['code'=>-1]);
            die;
        }
        if (empty($post['pass']))
        {
            echo json_encode(['code'=>-2]);
            die;
        }

        $account_model = new AccountModel;

        $account_info = $account_model->getOne(['Account'=>$post['account'],'Pwd'=>$post['pass']],'ProductId,Account,Pwd');
        if(empty($account_info)){
            echo json_encode(['code'=>-3]);
            die;
        }

        echo json_encode(['code'=>1,'data'=>$account_info]);

    }

    //强子地区列表
    public function qiangzi_address() 
    {
        $params = array(
            "username" => $_GET['user'],
            "password" => $_GET['password'],
        );

        $login_url = 'http://qixun.woniuruanjian.com:2222/main/raduserLogin';
      
        $login_content = linkcurl($login_url,'POST',$params);

        preg_match_all('/set\-cookie:([^\r\n]*); path/i', $login_content, $m);

        $cookie = $m[1][0];


        $url = 'http://qixun.woniuruanjian.com:2222/panel/index';

        $header = array();
        $header[] = "Cookie:".$cookie;
        $html = linkcurl($url,'POST',[],$header,0);

        //获取所有大分类
        preg_match("/<td id=\"pools\">(.*?)<\/td>/is", $html, $result);

        $result = str_replace('span','label',$result[0]);
        // echo $result;die;
        echo json_encode($result);
    }

    //强子
    public function qiangzi() 
    {
        $data = json_decode(file_get_contents("php://input"),true);
        $login_url = 'http://qixun.woniuruanjian.com:2222/main/raduserLogin?username=jt3936&password=717';
        $params = array(
            "username" => $data['account'],
            "password" => $data['password'],
        );
        $login_content = linkcurl($login_url,'POST',$params);

        preg_match_all('/set\-cookie:([^\r\n]*); path/i', $login_content, $m);

        $cookie = $m[1][0];

        $params = array(
            "act" => 'save',
            "username" => $data['account'],
            "poolids[]" => implode(',',$data['list']),
        );

        $url = 'http://qixun.woniuruanjian.com:2222/panel/index';

        $header = array();
        $header[] = "Cookie:".$cookie;
        $data = linkcurl($url,'POST',$params,$header);
        echo json_encode(['code'=>1]);
    }
}
