<?php

$html = '';
foreach ($orders as $order) {
    $content = create_print_label_content($order);
    $html .= <<<HTML
<table cellspacing="0" cellpadding="3" border="1" align="center" class="label">
<tbody>
    <tr align="left">
        <td style="line-height: 28px;">
            <font size="5" face="3 of 9 barcode" style="font-size: 26px;">
                *{$order->id}*
            </font>  * {$order->id} * <br>
            <textarea rows="15" cols="70" name="label_content_{$order->id}" id="label_content_{$order->id}">
{$content}
            </textarea>
        </td>
    </tr>
</tbody>
</table>
<input type="hidden" name="order_id_{$order->id}" value="{$order->id}" id="order_id_{$order->id}"  />
HTML;
}
echo $html;
$base_url = base_url();
echo <<<LOADING
<div style="left: -2px; top: 0px; width: 1423px; height: 754px;display: none; " id="loading-mask">
    <p id="loading_mask_loader" class="loader"><img alt="Loading..." src="{$base_url}static/images/ajax-loader-tr.gif"><br>Please wait...</p>
</div>
LOADING;
$print_label_url = site_url('shipping/deliver_management/print_label');
    public function print_label()
    {
        $style = array('position' => 'S', 'align' => 'C', 'stretch' => false, 'fitwidth' => false, 'cellfitalign' => '', 'border' => false, 'padding' => 0, 'fgcolor' => array(0, 0, 0), 'bgcolor' => false, 'text' => true, 'font' => 'helvetica', 'fontsize' => 12, 'stretchtext' => 4);
        $list_type = $this->input->post('list_type');
        if ($list_type == 1) {
            $width = 100;
            $height = 100;
            $pagelayout = array($width, $height);
            $my_tcpdf['page_format'] = $pagelayout;
            $my_tcpdf['page_orientation'] = 'L';
            $tcpdf['encoding'] = 'UTF-8';
            $this->load->library('pdf', $my_tcpdf);
            $this->pdf->SetCreator('Mallerp');
            $this->pdf->SetAuthor('Mansea');
            $this->pdf->SetTitle('Ebay ShipOrder List');
            $this->pdf->SetSubject('Mallerp');
            $this->pdf->SetKeywords('Mansea, Mallerp, zhaosenlin, 278203374, 7410992');
            $this->pdf->SetFont('arialunicid0', '', 23);
            $this->pdf->setPrintHeader(false);
            $this->pdf->setPrintFooter(false);
            $this->pdf->SetMargins(0, 0, 0);
        }
        if ($list_type == 2) {
            /*excel表头*/
            $head = array('Transaction Date', 'Transaction ID', 'Product', 'SKU', 'Quantity', 'Name', 'Shipping Cost', 'ERP order number', 'Price', 'Discount Price', 'Total Cost');
        }
        if ($list_type == 3) {
            $my_tcpdf['page_orientation'] = 'L';
            $tcpdf['encoding'] = 'UTF-8';
            $this->load->library('pdf', $my_tcpdf);
            $this->pdf->SetCreator('Mallerp');
            $this->pdf->SetAuthor('Mansea');
            $this->pdf->SetTitle('Ebay ShipOrder List');
            $this->pdf->SetSubject('Mallerp');
            $this->pdf->SetKeywords('Mansea, Mallerp, zhaosenlin, 278203374, 7410992');
            $this->pdf->SetFont('arialunicid0', '', 23);
        }
        $CI =& get_instance();
        $this->load->library('excel');
        if (!$this->input->is_post()) {
            return;
        }
        $post_keys = array_keys($_POST);
        $order_ids = array();
        $i_d = 0;
        foreach ($post_keys as $key) {
            if (strpos($key, 'checkbox_select_') === 0) {
                $order_ids[] = $_POST[$key];
            }
        }
        $orders = array();
        $user_name = $this->get_current_user_name();
        $products = array();
        $data = array();
        $all_total_cost = 0;
        foreach ($order_ids as $order_id) {
            $i_d++;
            $order = $this->order_model->get_order($order_id);
            $skus = explode(',', $order->sku_str);
            $qties = explode(',', $order->qty_str);
            $temp_skus = array();
            $temp_qties = array();
            foreach ($skus as $i => $sku) {
                if ($this->order_model->check_exists('product_makeup_sku', array('makeup_sku' => $sku))) {
                    $makeup_sku = $this->product_makeup_sku_model->fetch_makeup_sku_by_sku($skus[$i]);
                    $sku_arr = explode(',', $makeup_sku->sku);
                    $qty_arr = explode(',', $makeup_sku->qty);
                    foreach ($sku_arr as $key => $value) {
                        $temp_skus[] = $value;
                        $count_sku = (int) $qties[$i] * $qty_arr[$key];
                        $temp_qties[] = $count_sku;
                    }
                } else {
                    $temp_skus[] = $sku;
                    $temp_qties[] = $qties[$i];
                }
            }
            $skus = $temp_skus;
            $qties = $temp_qties;
            $j = 0;
            $error = FALSE;
            $label_content = create_print_label_content($order);
            $remark = $order->sys_remark;
            $remark .= sprintf(lang('print_label_remark'), date('Y-m-d H:i:s'), $user_name);
            $info = array('label_content' => $label_content, 'print_label_user' => $user_name, 'print_label_date' => date('Y-m-d H:i:s'), 'order_status' => $this->order_statuses['wait_for_shipping_confirmation'], 'sys_remark' => $remark);
            $this->order_model->update_order_information($order_id, $info);
            /*补打标签不更新库存*/
            $orders[] = $this->order_model->get_order($order_id);
            if ($list_type == 1) {
                /*添加一页pdf打印*/
                $this->pdf->AddPage();
                $page_index = 1;
                /*定义打印变量*/
                $shipping_method = shipping_method($order->is_register);
                $country_name_cn = get_country_name_cn($order->country);
                $shipaddress = "";
                $phone = '';
                $shipaddress = trim($order->address_line_1) . " " . trim($order->address_line_2) . " " . trim($order->town_city) . " " . trim($order->state_province) . " " . trim($order->zip_code);
                $print_date = date('Y.m.d');
                if (!empty($order->contact_phone_number)) {
                    $phone = "<br/>Telephone:{$order->contact_phone_number}";
                }
                $htmlshipping_method = <<<EOD
<span style="font-family:droidsansfallback;font-size:12;">{$shipping_method->name_cn} [{$order->is_register}]</span>
EOD;
                $htmlauction_site = <<<EOD
<span style="font-family:droidsansfallback;font-size:20;">{$order->auction_site}</span>
EOD;
                $text_to = <<<EOD
<span style="font-family:Arial, Helvetica, sans-serif; font-size:16;font-weight:bold;">TO:</span>
EOD;
                $ship_to_add = <<<EOD
<span style="text-align:left;white-space:nowrap;font-size:12;font-weight:bold;">{$order->name}<br>{$shipaddress} <br>{$order->country}[{$country_name_cn}] {$phone}</span>
EOD;
                $htmlprint_date = <<<EOD
<span style="text-align:left;white-space:nowrap;font-size:8;">日期:{$print_date}</span>
EOD;
                $img_hr = <<<EOD
<hr style="height:0.3mm;width:98mm;">
EOD;
                if ($order->note != '') {
                    $break_key = 4;
                    $first_page_sku_num = 5;
                    $page_break_key = 8;
                    $order_note = <<<EOD
<span style="text-align:left;white-space:nowrap;font-size:8;">NOTE:{$order->note}</span>
EOD;
                } else {
                    $break_key = 6;
                    $first_page_sku_num = 7;
                    $page_break_key = 6;
                    $order_note = '';
                }
                $this->pdf->writeHTMLCell($w = 90, $h = 8, $x = 5, $y = 1, $htmlshipping_method, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                $this->pdf->write1DBarcode($order_id, 'C128A', 5, 8, 70, 8, 0.8, $style, 'C');
                //write1DBarcode($code, $type, $x, $y, $w, $h, $xres, $newstyle, '');
                $this->pdf->writeHTMLCell($w = 18, $h = 10, $x = 81, $y = 10, $htmlauction_site, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                $this->pdf->writeHTMLCell($w = 15, $h = 30, $x = 2, $y = 21, $text_to, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'C', $autopadding = true);
                $this->pdf->writeHTMLCell($w = 75, $h = 30, $x = 18, $y = 21, $ship_to_add, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                $this->pdf->writeHTMLCell($w = 98, $h = 1, $x = 1, $y = 56, $img_hr, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                foreach ($skus as $key => $sku) {
                    $no = $key + 1;
                    $num_no = $key + 1;
                    $sql1 = 'name_cn,shelf_code';
                    $myproduct = $CI->product_model->fetch_product_by_sku($sku, $sql1);
                    $htmlproduct_list = '';
                    $htmlproduct_list = <<<EOD
<span style="white-space:nowrap;font-size:9;">({$num_no}) {$myproduct->shelf_code}-{$sku}-{$myproduct->name_cn}*{$qties[$key]}</span>
EOD;
                    $this->pdf->writeHTMLCell($w = 98, $h = 3, $x = 1, $y = 52 + 5 * $no, $htmlproduct_list, $border = 0, $ln = 0, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                    if ($key == $break_key) {
                        break;
                    }
                }
                /*备注打印在第一页*/
                if ($order->note != '') {
                    $this->pdf->writeHTMLCell($w = 98, $h = 6, $x = 1, $y = 81, $order_note, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                }
                $this->pdf->writeHTMLCell($w = 40, $h = 3, $x = 2, $y = 95, $htmlprint_date, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                if (count($skus) > $first_page_sku_num) {
                    foreach ($skus as $key => $sku) {
                        if ($key >= $first_page_sku_num && $page_index * 13 - $key == $page_break_key) {
                            $this->pdf->AddPage();
                            $page_index++;
                            $htmllink_uppage = <<<EOD
<span style="text-align:left;white-space:nowrap;font-size:12;">接上页清单:order id:{$order_id},本页是第{$page_index}页</span>
EOD;
                            $this->pdf->writeHTMLCell($w = 90, $h = 8, $x = 5, $y = 1, $htmllink_uppage, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                            $this->pdf->writeHTMLCell($w = 40, $h = 3, $x = 2, $y = 95, $htmlprint_date, $border = 0, $ln = 1, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                        }
                        if (ceil(($key + $page_break_key + 1) / 13) == $page_index && $key >= $first_page_sku_num) {
                            $no = $key + $page_break_key - ($page_index - 1) * 13 + 1;
                            $num_no = $key + 1;
                            $sql1 = 'name_cn,shelf_code';
                            $myproduct = $CI->product_model->fetch_product_by_sku($sku, $sql1);
                            $htmlproduct_list = '';
                            $htmlproduct_list = <<<EOD
<span style="white-space:nowrap;font-size:9;">({$num_no}) {$myproduct->shelf_code}-{$sku}-{$myproduct->name_cn}*{$qties[$key]}</span>
EOD;
                            $this->pdf->writeHTMLCell($w = 98, $h = 3, $x = 1, $y = 10 + 5 * $no, $htmlproduct_list, $border = 0, $ln = 0, $fill = 0, $reseth = true, $align = 'L', $autopadding = true);
                        }
                    }
                }
                if (in_array(strtoupper($order->is_register), array('CHR', 'CHS', 'HKR', 'HKS', 'MYR', 'MYS', 'SGR', 'SGS'))) {
                    $big_is_register = strtoupper($order->is_register);
                    $this->pdf->AddPage();
                    $return_post_img = <<<EOD
<img width="99mm" height="99mm" src="/static/images/returnpost/{$big_is_register}.jpg"/>
EOD;
                    $this->pdf->writeHTMLCell($w = 99, $h = 99, $x = 0, $y = 0, $return_post_img, $border = 0, $ln = 0, $fill = 0, $reseth = true, $align = 'C', $autopadding = false);
                }
            } elseif ($list_type == 2) {
                /*为wish pi excel增加一行*/
                $skus = explode(',', $order->sku_str);
                $qties = explode(',', $order->qty_str);
                $price = '';
                $discount_price = '';
                $total_cost = 0;
                foreach ($skus as $key => $sku) {
                    $sql1 = 'sale_price';
                    $myproduct = $CI->product_model->fetch_product_by_sku($sku, $sql1);
                    $price .= $myproduct->sale_price;
                    $discount_price .= price($myproduct->sale_price * 0.961);
                    $total_cost += $myproduct->sale_price * 0.961 * $qties[$key];
                    if ($key + 1 != count($skus)) {
                        $price .= ',';
                        $discount_price .= ',';
                    }
                }
                $total_cost += 1.99;
                $all_total_cost += $total_cost;
                $data[] = array($order->created_at, $order->transaction_id, $order->item_title_str, $order->sku_str, $order->qty_str, $order->name, '1.99', $order->item_no, $price, $discount_price, price($total_cost));
            } elseif ($list_type == 3) {
                /*添加一页A4  pdf打印*/
                $this->pdf->AddPage('P', 'A4');
                //$skus = explode(',', $order->sku_str);
                //$qties = explode(',', $order->qty_str);
                $print_date = date('Y.m.d');
                $user = fetch_user_name_by_id(fetch_user_id_by_login_name($order->input_user));
                $country_cn_name = get_country_name_cn($order->country);
                $html8 = <<<EOD
<table width="100%" border="1" cellspacing="0" cellpadding="0" style="font-family:Arial, Helvetica, sans-serif;font-size:12;">
<tr style="font-family:droidsansfallback;font-size:12;"><td>业务员</td><td>{$user}</td><td>订单识别号</td><td>{$order->item_no}</td><td colspan="2" rowspan="3">备注:{$order->note}</td></tr>
EOD;
                $html8 .= <<<EOD
<tr style="font-family:droidsansfallback;font-size:12;"><td>客户名称</td><td>{$order->name}</td><td>国家</td><td>{$country_cn_name}</td></tr>
<tr style="font-family:droidsansfallback;font-size:12;"><td>运输方式</td><td>{$order->is_register}</td><td>日期</td><td>{$print_date}</td></tr>
<tr style="font-family:droidsansfallback;font-size:12;"><td>货架号</td><td>SKU</td><td>数量</td><td>图片</td><td>中文名称</td><td>单价</td></tr>
EOD;
                foreach ($skus as $key => $sku) {
                    $sql1 = 'name_cn,shelf_code,sale_price,image_url,price';
                    $myproduct = $CI->product_model->fetch_product_by_sku($sku, $sql1);
                    $image = "";
                    if ($myproduct->image_url != '' && $myproduct->image_url != NULL && $myproduct->image_url != 'none') {
                        if (strpos($myproduct->image_url, 'http://') !== false) {
                            $url = $myproduct->image_url;
                        } else {
                            $url = 'http://erp.screamprice.com' . $myproduct->image_url;
                        }
                        //$header= get_headers($url);
                        if ($this->img_exits($url)) {
                            $arr = getimagesize($url);
                            if (count($arr) > 0) {
                                $image = '<img src="' . $myproduct->image_url . '" border="0" height="15mm" width="15mm" />';
                            }
                        }
                    } else {
                        $image = '<img src="http://erp.screamprice.com/static/images/404-error.png" border="0" height="25mm" width="25mm" />';
                    }
                    $html8 .= <<<EOD
<tr style="font-family:droidsansfallback;font-size:12;height:15mm;"><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$myproduct->shelf_code}</td><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$sku}</td><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$qties[$key]}</td><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$image}</td><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$myproduct->name_cn}</td><td style="font-family:droidsansfallback;font-size:12;height:15mm;">{$myproduct->sale_price}</td></tr>
EOD;
                    //if($sku=='YXDS0001'){break;}
                }
                $html8 .= <<<EOD
</table>
EOD;
                $this->pdf->writeHTMLCell($w = 200, $h = 0, $x = 5, $y = 5, $html8, $border = 1, $ln = 0, $fill = 0, $reseth = true, $align = 'L', $autopadding = false);
                //$this->pdf->writeHTML($html8, true, false, true, false, '');
            }
            /*更改 订单状态为带发货确认*/
        }
        /*保存文件*/
        if ($list_type == 1) {
            $filename = "order_print_10x10_" . date("Ymd") . ".pdf";
            $this->pdf->Output($filename, 'D');
        } elseif ($list_type == 2) {
            $data[] = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', price($all_total_cost));
            $this->excel->array_to_excel($data, $head, 'wish_pi_list_' . date('Y-m-d'));
        } elseif ($list_type == 3) {
            $filename = "order_print_a4_" . date("Ymd") . ".pdf";
            $this->pdf->Output($filename, 'D');
        }
    }