<?php

    /***************************************************************\
    |                                                               |
    |   房哨（专业的房产运营ERP）                                     |
    |                                                               |
    |   © 2019 http://fs.ytsofts.cn/ All rights reserved.           |
    |                                                               |
    |   房哨由淮南市银泰软件科技有限公司提供技术支持                    |
    |                                                               |
    |   QQ:984784483                                                |
    |                                                               |
    \***************************************************************/

    namespace Erpadmin\Controller;
    use Think\Controller;
    class ConfigController extends ErpadminbaseController {

        public function index() {
            if (IS_POST) {
                k_set_option('site_info',I('option'),1);
                $this->success('更新成功');
            } else {
                $this->site_info = k_get_option('site_info',1);
                $this->site_info = k_get_option('site_info',1);
                $this->fkyzt_config = k_get_option('fkyzt_config',1);
                $this->base_set = k_get_option('base_set',1);
                if ($this->base_set['servicearea']) {
                    $servicearea_str = $this->base_set['servicearea'];
                    $servicecity_str = $this->base_set['servicecity'];
                    $where['jjrxt_region.id'] = array('in',$servicecity_str);
                    $where['jjrxt_xingzhengqu.id'] = array('in',$servicearea_str);
                    $region = M('xingzhengqu')->join('jjrxt_region on jjrxt_xingzhengqu.pid=jjrxt_region.id')->where($where)->field('jjrxt_xingzhengqu.id,jjrxt_xingzhengqu.xzqming,jjrxt_region.name as regionname,jjrxt_xingzhengqu.pid')->select();
                    $region_arr = array();
                    foreach ($region as $key => $value) {
                        $region_arr[] = $value['xzqming'] .'-'.$value['regionname'];
                    }
                    $this->region_str = implode(',',$region_arr);
                }
                $fkyzt = $this->fkyzt_config;
                $fkyzt['fygongpan_zhuangtai'] = $fkyzt['fygongpan_zhuangtai']?$fkyzt['fygongpan_zhuangtai']:0;
                $fkyzt['fysipan_zhuangtai'] = $fkyzt['fysipan_zhuangtai']?$fkyzt['fysipan_zhuangtai']:0;
                $fkyzt['kygongke_zhuangtai'] = $fkyzt['kygongke_zhuangtai']?$fkyzt['kygongke_zhuangtai']:0;
                $fkyzt['kysike_zhuangtai'] = $fkyzt['kysike_zhuangtai']?$fkyzt['kysike_zhuangtai']:0;
                $fyfpzt=$fkyzt['fyfengpan_zhuangtai'];
                $kyfpzt=$fkyzt['kyfengpan_zhuangtai'];
                $fyfpztids=explode(",",$fyfpzt);
                foreach ($fyfpztids as $key => $value) {
                    $fyfpztid[$value]=$value;
                }
                $kyfpztids=explode(",",$kyfpzt);
                foreach ($kyfpztids as $key => $value) {
                    $kyfpztid[$value]=$value;
                }
                $this->fyfpztid=$fyfpztid;
                $this->kyfpztid=$kyfpztid;
                $this->fyfpzt=M('peizhi')->where(" pzming = 'zhuangtai' and zhuangtai = 1 and lxid <> ".$fkyzt['fygongpan_zhuangtai']." and lxid <> ".$fkyzt['fysipan_zhuangtai'])->order('xsshunxu asc')->select();
                $this->kyfpzt=M('peizhi')->where(" pzming = 'khzhuangtai' and zhuangtai = 1 and lxid <> ".$fkyzt['kygongke_zhuangtai']." and lxid <> ".$fkyzt['kysike_zhuangtai'])->order('xsshunxu asc')->select();
                $this->display();
            }
        }

        public function setservicearea() {
            if (IS_POST) {
                $data2 = I('post.data2');
                if (!$data2) {
                    $this->error('至少选择一个业务地区!');
                } 
                $data1 = I('post.data1');
                $config_arr = k_get_option('base_set',1);
                $config_arr['servicecity'] = $data1;
                $config_arr['servicearea'] = $data2;
                k_set_option('base_set',$config_arr,1);
                $this->success('保存成功!');
            } else {
                $config_arr = k_get_option('base_set',1);
                $servicecity_str = $config_arr['servicecity'];
                if ($servicecity_str) {
                    $this->region = M('region')->where(array('id'=>array('in',$servicecity_str)))->select();
                }
                $servicearea_str = $config_arr['servicearea'];
                if ($servicecity_str && $servicearea_str) {
                    $where['jjrxt_region.id'] = array('in',$servicecity_str);
                    $where['jjrxt_xingzhengqu.id'] = array('in',$servicearea_str);
                    $this->area = M('xingzhengqu')->join('jjrxt_region on jjrxt_xingzhengqu.pid=jjrxt_region.id')->where($where)->field('jjrxt_xingzhengqu.id,jjrxt_xingzhengqu.xzqming,jjrxt_region.name as regionname,jjrxt_xingzhengqu.pid')->select();
                }
                $this->display();
            }
        }

        public function fkyzt_config() {
            if ((I('fychengjiao_zhuangtai') == I('fygongpan_zhuangtai')) || (I('fychengjiao_zhuangtai') == I('fysipan_zhuangtai')) || ((I('fygongpan_zhuangtai') == I('fysipan_zhuangtai')) && (I('fygongpan_zhuangtai') > 0) && (I('fysipan_zhuangtai') > 0))){
                $this->error('房源状态相同');
            }
            if ((I('kychengjiao_zhuangtai') == I('kygongke_zhuangtai')) || (I('kychengjiao_zhuangtai') == I('kysike_zhuangtai')) || ((I('kygongke_zhuangtai') == I('kysike_zhuangtai')) && (I('kygongke_zhuangtai') > 0) && (I('kysike_zhuangtai') > 0))){
                $this->error('客源状态相同');
            }
            $fyfpzt=I('fyfpzt');
            if ($fyfpzt) {
                foreach ($fyfpzt as $key => $value) {
                    if (I('fygongpan_zhuangtai') == $value){
                        $this->error('房源无效状态中有房源公盘状态');
                    }
                    if (I('fysipan_zhuangtai') == $value){
                        $this->error('房源无效状态中有房源私盘状态');
                    }
                    $fyarr[]=$value;
                }
            }
            $fyids=implode(",",$fyarr);
            if (!in_array(I('fychengjiao_zhuangtai'), $fyarr)){
                $this->error('房源无效状态中没有房源成交状态');
            }
            $kyfpzt=I('kyfpzt');
            if ($kyfpzt) {
                foreach ($kyfpzt as $key => $value) {
                    if (I('kygongke_zhuangtai') == $value){
                        $this->error('客源无效状态中有客源公客状态');
                    }
                    if (I('kysike_zhuangtai') == $value){
                        $this->error('客源无效状态中有客源私客状态');
                    }
                    $kyarr[]=$value;
                }
            }
            $kyids=implode(",",$kyarr);
            if (!in_array(I('kychengjiao_zhuangtai'), $kyarr)){
                $this->error('客源无效状态中没有客源成交状态');
            }
            $arr =  array(
                'fychengjiao_zhuangtai'=>I('fychengjiao_zhuangtai'),
                'kychengjiao_zhuangtai'=>I('kychengjiao_zhuangtai'),
                'fygongpan_zhuangtai'=>I('fygongpan_zhuangtai'),
                'kygongke_zhuangtai'=>I('kygongke_zhuangtai'),
                'fysipan_zhuangtai'=>I('fysipan_zhuangtai'),
                'kysike_zhuangtai'=>I('kysike_zhuangtai'),
                'fyfengpan_zhuangtai'=>$fyids,
                'kyfengpan_zhuangtai'=>$kyids,
            );
            k_set_option('fkyzt_config',$arr,1);
            $this->success('更新成功');
        }

    }