<?php
namespace Factory\Admin;

use Dever;

class Excel extends Core
{
    public function out($id, $user, $data)
    {
        if (!$data) {
            Dever::alert('无导出数据');
        }
        $info = Dever::db('factory/info')->find($id);
        if (!$info) {
            Dever::alert('操作失败');
        }

        $method = 'out_' . $info['out_template'];
        $file = '发货信息表';
        return $this->$method($file, $user, $data);
    }

    # 模板模板
    public function out_1($file, $user, $data)
    {
        $this->out_2($file, $user, $data, $file);
    }

    # 九阳模板
    public function out_2($file, $user, $data, $name = '')
    {
        $header = array();

        $header = array('序号', '申请发货时间', '申请人', '产品名称', '数量', '订单号', '收件人姓名', '收件人电话', '收件地址', '发货时间', '发件物流', '运单编号', '备注');

        $file = $name ? $name : '中食民安&九阳发货信息表';

        $body = array();

        $i = 1;
        foreach ($data as $k => $v) {
            if (!is_array($v)) {
                continue;
            }

            Dever::load('mshop/lib/buy')->setOutStatus($v['id'], 2);

            if ($v['ps_info'] && $v['ps_info']['cdate_day']) {
                $ps_date = $v['ps_info']['cdate_day'];
            } else {
                $ps_date = '-';
            }

            $service_name = array();
            $service_num = array();
            if (isset($v['ps_info']['service']) && $v['ps_info']['service']) {
                foreach ($v['ps_info']['service'] as $k1 => $v1) {
                    $service_name[] = $v1['service_name'];
                    $service_num[] = $v1['order_num'];
                }
            }

            $service_name = implode(',', $service_name);
            $service_num = implode(',', $service_num);

            if ($v['parent_type'] == 2) {
                $parent_order = Dever::db('shop/sell_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['uid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
                        $v['type_info']['name'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('passport/user')->find($parent_order['uid']);
                        $v['type_info']['name'] = $address['username'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            } elseif ($v['parent_type'] == 3) {
                $parent_order = Dever::db('agent/dh_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['mid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('agent/address')->getOne($parent_order['mid'], $parent_order['address_id']);
                        $v['type_info']['name'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('agent/member')->find($parent_order['mid']);
                        $v['type_info']['name'] = $address['name'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            }

            foreach ($v['goods'] as $k1 => $v1) {

                $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }

                $d = array
                (
                    $i,
                    $v['operdate_day'],
                    $user['name'],
                    $goods_info['name'] . ' ' . $sku,
                    $v1['num'],
                    $v['order_num'],

                    $v['type_info']['name'],
                    $v['type_info']['mobile'],
                    $v['type_info']['address'],

                    $ps_date,

                    $service_name,
                    $service_num,

                    $v['info'],
                    
                );

                $body[] = $d;

                $i++;
            }
        }

        Dever::excelExport($body, $header, $file);
    }

    # 纯米模板
    public function out_3($file, $user, $data)
    {
        $header = array();

        $header = array('序号', '申请人', '发货日期', '客户名称', '品名', '单位', '数量', '订单号', '收件人', '收件电话', '收件邮寄地址', '备注');

        $file = '纯米发货信息表';

        $body = array();

        $i = 1;
        foreach ($data as $k => $v) {
            if (!is_array($v)) {
                continue;
            }

            Dever::load('mshop/lib/buy')->setOutStatus($v['id'], 2);
            if ($v['ps_info'] && $v['ps_info']['cdate_day']) {
                $ps_date = $v['ps_info']['cdate_day'];
            } else {
                $ps_date = '-';
            }

            if ($v['parent_type'] == 2) {
                $parent_order = Dever::db('shop/sell_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['uid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
                        $v['type_info']['truename'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('passport/user')->find($parent_order['uid']);
                        $v['type_info']['truename'] = $address['username'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            } elseif ($v['parent_type'] == 3) {
                $parent_order = Dever::db('agent/dh_order')->find($v['parent_order_id']);

                if ($parent_order && $parent_order['mid']) {
                    if ($parent_order['address_id']) {
                        $address = Dever::load('agent/address')->getOne($parent_order['mid'], $parent_order['address_id']);
                        $v['type_info']['truename'] = $address['contact'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = $address['address_info'];
                    } else {
                        $address = Dever::db('agent/member')->find($parent_order['mid']);
                        $v['type_info']['truename'] = $address['name'];
                        $v['type_info']['mobile'] = $address['mobile'];
                        $v['type_info']['address'] = '';
                    }
                }
            }
            
            foreach ($v['goods'] as $k1 => $v1) {

                $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
                if (isset($goods_info['sku'])) {
                    $sku = $goods_info['sku']['string'];
                } else {
                    $sku = '';
                }

                $d = array
                (
                    $i,
                    $user['name'],
                    $ps_date,
                    '中食民安（北京）',
                    $goods_info['name'] . ' ' . $sku,
                    $goods_info['unit'],
                    $v1['num'],

                    $v['order_num'],

                    $v['type_info']['truename'],
                    $v['type_info']['mobile'],
                    $v['type_info']['address'],

                    $v['info'],
                    
                );

                $body[] = $d;

                $i++;
            }
        }

        Dever::excelExport($body, $header, $file);
    }
}