<?php

namespace Mshop\Src;

use Dever;
use Mshop\Lib\Core;

class My extends Core
{
    public function home()
    {
        $config = Dever::db('main/manage_config')->find();
        $this->data['config'] = $config;
        $this->data['phone'] = $config['phone'];
        $this->data['user'] = $this->user;
        $this->data['shop'] = $this->shop;
        $this->data['date'] = date('Y-m-d');

        $where['mobile'] = $this->user['mobile'];
        $user = Dever::db('shop/member')->select($where);

        if ($user) {
            $this->data['other_shop'] = array();
            foreach ($user as $k => $v) {
                if ($v['shop_id'] != $this->shop_id) {
                    $shop = Dever::db('shop/info')->getOne($v['shop_id']);
                    if ($shop) {
                        $this->data['other_shop'][] = $shop;
                    }
                }
            }
        }

        return $this->data;
    }

    public function getEdit()
    {
        $this->data['user'] = $this->user;
        $this->data['shop'] = $this->shop;

        return $this->data;
    }

    # 修改门店资料
    public function edit()
    {
        $data = array();
        $name = Dever::input('name');
        if ($name) {
            $data['name'] = $name;
        }
        $lng = Dever::input('lng');
        if ($lng) {
            $data['lng'] = $lng;
        }
        $lat = Dever::input('lat');
        if ($lat) {
            $data['lat'] = $lat;
        }
        $address = Dever::input('address');
        if ($address) {
            $data['address'] = $address;
        }
        $truename = Dever::input('truename');
        if ($truename) {
            $data['truename'] = $name;
        }
        $mobile = Dever::input('mobile');
        if ($mobile) {
            $data['mobile'] = $mobile;
        }
        $open = Dever::input('open');
        if ($open) {
            $data['open'] = $open;
        }
        $method = Dever::input('method');
        if ($method) {
            $data['method'] = $method;
        }
        $worktime = Dever::input('worktime');
        if ($worktime) {
            $data['worktime'] = $worktime;
        }

        if ($data) {
            $data['where_id'] = $this->shop_id;
            Dever::db('shop/info')->update($data);
        }
        

        return 'ok';
    }

    # 员工列表
    public function getUser()
    {
        $where['shop_id'] = $this->shop_id;
        $this->data['user'] = Dever::db('shop/member')->select_page($where);
        $role = Dever::db('shop/member')->config['role'];
        foreach ($this->data['user'] as $k => $v) {
            $v['role_name'] = array();
            $v['role'] = explode(',', $v['role_id']);
            foreach ($role as $k1 => $v1) {
                if (in_array($k1, $v['role'])) {
                    $v['role_name'][] = $v1;
                }
            }
            $this->data['user'][$k]['role_name'] = implode(',', $v['role_name']);
        }

        return $this->data;
    }

    # 添加员工
    public function getEditUser()
    {
        $this->data['role'] = Dever::db('shop/member')->config['role'];

        $id = Dever::input('id');

        if ($id) {
            $this->data['user'] = Dever::db('shop/member')->find($id);
        }

        return $this->data;
    }

    # 编辑员工
    public function upUser()
    {
        $id = Dever::input('id');
        $role = Dever::input('role');
        $avatar = Dever::input('avatar');
        $name = Dever::input('name');
        $mobile = Dever::input('mobile');
        if ($role) {
            $update['role_id'] = $role;
        }
        
        if ($avatar) {
            $update['avatar'] = $avatar;
        }

        if ($name) {
            $update['name'] = $name;
        } else {
            Dever::alert('姓名不能为空');
        }

        if ($mobile) {
            $update['mobile'] = $mobile;
            $check = Dever::db('shop/member')->find(array('mobile' => $mobile, 'shop_id' => $this->shop_id));
            if ($check && $id && $id != $check['id']) {
                Dever::alert('电话已存在');
            } elseif ($check && !$id) {
                Dever::alert('电话已存在');
            }
        } else {
            Dever::alert('电话不能为空');
        }

        $update['shop_id'] = $this->shop_id;

        if ($id) {
            $update['where_id'] = $id;
            Dever::db('shop/member')->update($update);
        } else {
            $id = Dever::db('shop/member')->insert($update);
        }

        return 'ok';
    }

    # 删除员工
    public function delUser()
    {
        $id = Dever::input('id');
        $info = Dever::db('shop/member')->find($id);
        if ($info && $info['shop_id'] == $this->shop_id) {
            Dever::db('shop/member')->delete($id);
        } else {
            Dever::alert('您没有权限删除');
        }

        return 'ok';
    }

    # 打印机列表
    public function getPrint()
    {
        $where['shop_id'] = $this->shop_id;
        $this->data['print'] = Dever::db('shop/print')->select($where);

        return $this->data;
    }

    # 添加打印机
    public function getEditPrint()
    {
        $id = Dever::input('id');

        if ($id) {
            $this->data['print'] = Dever::db('shop/print')->find($id);
        }

        return $this->data;
    }

    # 编辑打印机
    public function upPrint()
    {
        $id = Dever::input('id');
        $name = Dever::input('name');
        $number = Dever::input('number');
        $key = Dever::input('key');
        $phonenum = Dever::input('phonenum');
        if ($name) {
            $update['name'] = $name;
        } else {
            Dever::alert('名称不能为空');
        }

        if ($number) {
            $update['number'] = $number;
        } else {
            Dever::alert('编号不能为空');
        }

        if ($key) {
            $update['key'] = $key;
        } else {
            Dever::alert('识别码不能为空');
        }

        if ($phonenum) {
            $update['phonenum'] = $phonenum;
        }

        $update['shop_id'] = $this->shop_id;

        if ($id) {
            $update['where_id'] = $id;
            Dever::db('shop/print')->update($update);
        } else {
            $id = Dever::db('shop/print')->insert($update);
        }

        return 'ok';
    }

    # 删除打印机
    public function delPrint()
    {
        $id = Dever::input('id');
        $info = Dever::db('shop/print')->find($id);
        if ($info && $info['shop_id'] == $this->shop_id) {
            Dever::db('shop/print')->update(array('where_id' => $info['id'], 'state' => 2));

            //Dever::load('mshop/lib/feieyun')->del(array($info));
        } else {
            Dever::alert('您没有权限删除');
        }

        return 'ok';
    }

    # 营销活动 优惠券列表
    public function getCoupon()
    {
        $this->data['method'] = Dever::db('goods/coupon')->config['method'];
        $this->data['coupon'] = Dever::db('goods/coupon')->select();

        if ($this->data['coupon']) {
            $this->data['day'] = Dever::db('shop/coupon')->config['time'];
            foreach ($this->data['coupon'] as $k => $v) {
                $this->data['coupon'][$k]['check'] = 1;
                $check = Dever::db('shop/coupon')->find(array('shop_id' => $this->shop_id, 'coupon_id' => $v['id']));
                if ($check) {
                    $this->data['coupon'][$k]['check'] = 2;
                    $this->data['coupon'][$k]['shop'] = $check;
                }
            }
        }
        return $this->data;
    }

    # 营销活动 配置
    public function setCoupon()
    {
        /*
        $coupon_city = Dever::input('coupon_city');

        if ($coupon_city) {
            $update['where_id'] = $this->shop_id;
            $update['coupon_city'] = $coupon_city;
            Dever::db('shop/info')->update($update);
        }
        */
        $coupon = Dever::input('coupon');

        //$coupon[] = array('id' => 1, 'day' => 7, 'method' => 2, 'num' => 10);

        //print_r(Dever::json_encode($coupon));die;

        if ($coupon) {
            $coupon = Dever::json_decode($coupon);
            foreach ($coupon as $k => $v) {
                $info = Dever::db('shop/coupon')->find(array('shop_id' => $this->shop_id, 'coupon_id' => $v['id']));
                $data = array();
                $data['method'] = $v['method'];
                $data['coupon_id'] = $v['id'];
                $data['coupon'] = $v['method'] . ',' . $v['id'];
                $data['day'] = $v['day'];
                $data['num'] = $v['num'];
                if (!$info) {
                    $data['shop_id'] = $this->shop_id;
                    Dever::db('shop/coupon')->insert($data);
                } else {
                    $data['where_id'] = $info['id'];
                    Dever::db('shop/coupon')->update($data);
                }
            }
        }

        return $this->getCoupon();
    }

    # 获取参与活动领取的优惠券
    public function getActCoupon()
    {
        $act_id = Dever::input('act_id', 1);
        $this->data['shop_coupon'] = Dever::load('shop/lib/coupon')->getData($this->shop_id);

        foreach ($this->data['shop_coupon'] as $k => $v) {
            $this->data['shop_coupon'][$k]['info'] = Dever::db('goods/coupon')->find($v['coupon_id']);
            $check = Dever::db('shop/coupon_act')->find(array('shop_id' => $this->shop_id, 'shop_coupon_id' => $v['id'], 'act_id' => $act_id));
            $this->data['shop_coupon'][$k]['check'] = 1;
            if ($check) {
                $this->data['shop_coupon'][$k]['act_coupon'] = $check;
                $this->data['shop_coupon'][$k]['check'] = 2;
            }
        }

        return $this->data;
    }

    # 采购对账单管理
    public function stat_month()
    {
        $type = Dever::input('type', 'shop');
        $table = 'cash/' . $type;

        $this->data['config'] = Dever::db('main/manage_config')->find();
        $where['shop_id'] = $this->shop_id;
        $shop = Dever::db('shop/info')->find($this->shop_id);
        $stat_type = Dever::db($table)->config['config_type'];
        $status = Dever::db($table)->config['config_status'];

        $month = Dever::input('month');
        if ($month) {
            Dever::setInput('day', $month);
            if ($stat_type == 1) {
                list($where['start'], $where['end']) = Dever::month();
            } else {
                list($where['start'], $where['end']) = Dever::week();
            }
        }
        
        $this->data['data'] = Dever::db($table)->getAll($where);

        if ($this->data['data']) {
            foreach ($this->data['data'] as $k => $v) {
                $this->data['data'][$k]['name'] = Dever::load('cash/lib/set')->statDate($stat_type, $v['day']);
                $v['day'] = date('Y-m-d', $v['day']);
                Dever::setInput('day', $v['day']);
                if ($stat_type == 1) {
                    list($this->data['data'][$k]['start'], $this->data['data'][$k]['end']) = Dever::month();
                } elseif ($stat_type == 2) {
                    list($this->data['data'][$k]['start'], $this->data['data'][$k]['end']) = Dever::week();
                } else {
                    list($this->data['data'][$k]['start'], $this->data['data'][$k]['end']) = Dever::day();
                }
                $this->data['data'][$k]['start'] = date('Y-m-d', $this->data['data'][$k]['start']);
                $this->data['data'][$k]['end'] = date('Y-m-d', $this->data['data'][$k]['end']);
                $this->data['data'][$k]['status_name'] = $status[$v['shop_status']];
            }
        }

        return $this->data;
    }

    # 确认对账
    public function set_stat_month()
    {
        $id = Dever::input('id');
        $type = Dever::input('type', 'shop');
        $table = 'cash/' . $type;

        $where['id'] = $id;
        $where['shop_id'] = $this->shop_id;
        $info = Dever::db($table)->find($where);
        if (!$info) {
            Dever::alert('账单不存在');
        }
        if ($info) {
            Dever::db($table)->update(array('where_id' => $id, 'shop_status' => 2));
        }

        return $this->stat_month();
    }

    # 查看账单详情
    public function stat_month_view()
    {
        $id = Dever::input('id');
        $type = Dever::input('type', 'shop');
        $table = 'cash/' . $type;

        $where['id'] = $id;
        $where['shop_id'] = $this->shop_id;
        $this->data['config'] = Dever::db('main/manage_config')->find();
        $this->data['info'] = Dever::db($table)->find($where);
        $stat_type = Dever::db($table)->config['config_type'];
        $status = Dever::db($table)->config['config_status'];

        if (!$this->data['info']) {
            Dever::alert('账单不存在');
        }

        $start = $end = '';
        $this->data['info']['name'] = Dever::load('cash/lib/set')->statDate($stat_type, $this->data['info']['day']);
        $this->data['info']['day'] = date('Y-m-d', $this->data['info']['day']);
        Dever::setInput('day', $this->data['info']['day']);
        if ($stat_type == 1) {
            list($this->data['info']['start'], $this->data['info']['end']) = Dever::month();
        } elseif ($stat_type == 2) {
            list($this->data['info']['start'], $this->data['info']['end']) = Dever::week();
        } else {
            list($this->data['info']['start'], $this->data['info']['end']) = Dever::day();
        }
        $start = $this->data['info']['start'];
        $end = $this->data['info']['end'];
        $this->data['info']['start'] = date('Y-m-d', $this->data['info']['start']);
        $this->data['info']['end'] = date('Y-m-d', $this->data['info']['end']);
        
        $this->data['info']['status_name'] = $status[$this->data['info']['shop_status']];
        if ($type == 'shop_sell') {
            $this->data['info']['cash_name'] = '未返还门店账户';
            if ($this->data['info']['status'] == 2) {
                $this->data['info']['cash_name'] = '已返还门店账户';
            }
        }

        $where = array();
        $where['type'] = 1;
        $where['type_id'] = $this->shop_id;
        $where['start'] = $start;
        $where['end'] = $end;
        $where['status'] = 2;
        if ($type == 'shop_sell') {
            $where['pay_type'] = '2,3';
        } else {
            $where['pay_type'] = 1;
        }
        $this->data['data'] = Dever::db('cash/order')->getAll($where);

        if ($this->data['data']) {
            foreach ($this->data['data'] as $k => $v) {
                $this->data['data'][$k]['status_name'] = '已入账';
                $this->data['data'][$k]['cdate'] = date('Y-m-d H:i', $v['cdate']);
            }
        }

        return $this->data;
    }

    # 查看账单详情
    public function stat_month_excel()
    {
        $data = $this->stat_month_view();

        $file = $data['info']['name'] . '对账单';
        $header = $body = array();

        $header = array
        (
            'top' => array
            (
                $file . ' ' . $data['info']['status_name'] . ' 对账金额¥' . $data['info']['cash'],
            ),
            '单号',
            '时间',
            '金额',
            '状态'
        );
        if ($data['data']) {
            $body = array();
            foreach ($data['data'] as $k => $v) {
                $body[$k][0] = $v['order_num'];
                $body[$k][1] = $v['cdate'];
                $body[$k][2] = $v['cash'];
                $body[$k][3] = $v['status_name'];
            }
        }
        Dever::excelExport($body, $header, $file);
    }

    # 门店物料
    public function info()
    {
        $config = Dever::db('main/manage_config')->find();
        $this->data['file'] = $config['file'];
        $param['project'] = 1;
        $param['width'] = Dever::input('w', 150);
        $param['path'] = 'pages/app/shop/shop';
        $param['send'] = $this->shop_id;
        $this->data['ercode'] = Dever::load('wechat_applet/code')->get($param);
        return $this->data;
    }

    # 根据商品条形码编号获取商品
    public function getGoods()
    {
        return Dever::load('goods/lib/info')->getGoodsByCode($this->shop_id, 1, true);
    }

    #资金账户列表
    public function record_list(){
        $shop_id = Dever::input('shop_id');
        $month = Dever::input('month');
        $shop = Dever::db('shop/info')->find($shop_id);
        $data = array();
        $data['price'] = $shop['price'];
        if ($month) {
            $start = $month . '-01 00:00:00';
            $end = date('Y-m-d', strtotime("$start +1 month -1 day"));
            $where['start'] = Dever::maketime($start);
            $where['end'] = Dever::maketime($end . ' 23:59:59');
        }
        $where['shop_id'] = $shop_id;
        $where['state'] = 1;
        $data['list'] = Dever::db('shop/record')->getAll($where);
        foreach($data['list'] as $k=>$v){
            /*
            if($v['type'] == 1 || $v['type'] == 3 || $v['type'] == 6 || $v['type'] == 7 || $v['type'] == 8){
                $data['list'][$k]['price'] = '+'.$v['price'];
            }else{
                $data['list'][$k]['price'] = $v['price'];
            }
            */
            if ($v['price'] > 0) {
                $data['list'][$k]['price'] = '+'.$v['price'];
            }
            $data['list'][$k]['cdate'] = date('Y.m.d H:i',$v['cdate']);
            $typeconfig = Dever::db('shop/record')->config['config_type'];
            $data['list'][$k]['type_name'] = Dever::status($typeconfig,$v['type']);
            $config = Dever::db('shop/record')->config['config_status'];
            $data['list'][$k]['status_name'] = Dever::status($config,$v['status']);
        }
        $data['fund'] = Dever::db('main/manage_config')->getFund(array('state'=>1));
        return $data;
    }
    #资金账户详情页
    public function record_desc(){
        $id = Dever::input('id');
        $data = Dever::db('shop/record')->find($id);
        /*
        if($data['type'] == 1 || $data['type'] == 3 || $data['type'] == 6 || $data['type'] == 7 || $data['type'] == 8){
            $data['price'] = '+'.$data['price'];
        }else{
            $data['price'] = $data['price'];
        }*/
        if ($data['price'] > 0) {
            $data['price'] = '+'.$data['price'];
        }
        $typeconfig = Dever::db('shop/record')->config['config_type'];
        $data['type_name'] = Dever::status($typeconfig,$data['type']);
        $config = Dever::db('shop/record')->config['config_status'];
        $data['status_name'] = Dever::status($config,$data['status']);
        $data['cdate'] = date('Y.m.d H:i',$data['cdate']);
        return $data;
    }

}