{% extends 'mobile/include/common_page_template.html' %}
{% load xiaozhuang_console_tag %}
{% block title %}
购物车
{% endblock %}
{% block body %}
<div class="works-head" style="background: #fbbc50">
<a class="iconBack icon iconfont_list" href="javascript: history.go(-1);"></a>
<h1 style="font-size: 16px;font-weight: normal;">购物车</h1>
<p class="car_product_modify" id="modify">编辑</p>
</div>
<div class="product_car_contaier">
{% for provider in provider_list %}
<div class="product_parent">
<div class="product_car_headInfo">
<p class="productCar_info"><span><i
class="iconfont_car car_select car_store"></i></span><span><i
class="iconfont_car"></i></span><span>{{ provider.provider_user.name }}</span><span> > </span>
</p>
</div>
{% for cart_item in provider.cart_item_list %}
<div class="car_product_detail">
<p class="flex-center"><span><i class="iconfont_car car_select deleteTag"></i></span>
</p>
<p class="car_product_img"><img src="/static/mobile/images/psds40528.jpg" alt=""></p>
<div class="car_product_description">
<p> {{ cart_item.product.name }}</p>
<p class='car_product_main'>主材套餐</p>
<p class="product_car_meal">月销12 笔 </p>
<p class="car_product_detail_price">{{ cart_item.product.price }}</p>
{# store init value#}
<input type="hidden" value={{ cart_item.product.price }} class="product_car_initValue">
<input type="hidden" value={{ cart_item.product.id }} class="product_id_initValue">
<div class="gw_num">
<em class="jian">-</em>
<input type="text" value="1" class="num"/>
<em class="add">+</em>
</div>
</div>
</div>
{% endfor %}
</div>
{% endfor %}
{#------------footer------------#}
<ul class="car_product_pay flex">
<li><i class="iconfont_car " id="all_select"></i></li>
<li>
<span>全选</span>
<div class="product_car_price" id="product_price_show">
<p class="car_product_payInfo" id="product_totalPrice">合计:¥ <span>22000</span></p>
<p style="height: 20px;">不含运费</p>
</div>
</li>
<li><span id="car_calculate">
<a href="{% url 'mobile_customer_corder_create' %}">
结算
</a>
</span><span id="total_num">(0)</span></li>
</ul>
<div style="height: 80px;"></div>
{#--------------------------ProductCar.js-------------------------------#}
<script type="text/javascript">
var mTotal_money = 0;
updatePrice(mTotal_money);
var all_select = $('#all_select');
$(document).ready(function () {
CarJsInit();
});
function CarJsInit() {
all_select.attr('select', 'one');
selectAll();
}
function selectAll() {
if (all_select.attr('select') == 'one') {
all_select.html('').css('color', '#f24f00');
$('.car_select').html('').css('color', '#f24f00');
all_select.attr('select', 'two');
$('.car_select').attr('clickNum', 'two');
calculate();
} else if (all_select.attr('select') == 'two') {
updatePrice(0);
$('#total_num').html("(" + 0 + ")");
all_select.attr('select', 'one');
all_select.html('').css('color', '#9f9f9d');
$('.car_select').html('').css('color', '#9f9f9d');
$('.car_select').attr('clickNum', 'one');
}
}
$('#all_select').click(function () {
selectAll();
});
var select = $('.car_select');
select.attr('clickNum', 'one');
var num = 0;
select.click(function () {
var num = $(this).attr('clickNum');
if (num == 'one') {
var select_num = 0;
$(this).attr('clickNum', 'two');
calculate();
$(this).html('').css('color', '#f24f00');
} else {
$(this).attr('clickNum', 'one');
calculate();
$(this).html('').css('color', '#9f9f9d');
}
var shop_select = $(this).parents('.car_product_detail').siblings('.product_car_headInfo').find('.car_select');
var brother = $(this).parents('.product_parent').find('.car_product_detail');
for (var i = 0; i < brother.length; i++) {
if (brother.eq(i).find('.car_select').attr('clicknum') == 'one') {
shop_select.html('').css('color', '#9f9f9d');
shop_select.attr('clicknum', 'one');
select_num++;
break;
}
}
if (select_num == 0) {
shop_select.html('').css('color', '#f24f00');
shop_select.attr('clicknum', 'two');
}
});
$('#car_calculate').click(function () {
var product = $('.car_product_detail');
var price = $('.car_product_detail_price');
var calHtml = $('#car_calculate').html()
if (calHtml == '删除') {
var car_select = $('.deleteTag');
for (var i = 0; i < car_select.length; i++) {
var count = 0;
if (car_select.eq(i).attr('clickNum') == 'two') {
car_select.eq(i).attr('statu', '001');
car_select.eq(i).attr('clickNum', 'one');
mTotal_money -= parseInt(price.eq(i).html());
updatePrice(mTotal_money);
var num = product.eq(i).siblings();
for (var j = 0; j < num.length; j++) {
if (num.eq(j).css('display') != 'none') {
count++;
}
}
product.eq(i).hide();
if (count == 1) {
product.eq(i).parent().hide();
}
}
}
} else if (calHtml == '结算') {
if (!isNull()) {
saveProductInfo();
window.location.href = "{% url 'mobile_customer_corder_create' %}";
} else {
alert('购物车无需要结算商品');
}
}
});
$('#modify').click(function () {
var modify = $('#modify').html();
if (modify == '完成') {
$('#total_num').show();
$('#modify').html('编辑');
$('#car_calculate').html('结算');
$('#product_price_show').css('display', 'block');
$('.car_select').attr('clickNum', 'one');
$('#total_num').html("(" + 0 + ")");
} else if (modify == '编辑') {
$('#total_num').hide();
$('#modify').html('完成');
$('#car_calculate').html('删除');
$('#product_price_show').css('display', 'none');
$('.car_select').attr('clickNum', 'one');
updatePrice(0);
$('#total_num').html("(" + 0 + ")");
}
$('.car_select').html('').css('color', '#9f9f9d');
$('#all_select').html('').css('color', '#9f9f9d');
});
$(document).ready(function () {
$(".add").click(function () {
var n = $(this).prev().val();
var num = parseInt(n) + 1;
if (num == 0) {
return;
}
$(this).prev().val(num);
var index = $('.add').index(this);
calculate();
});
$(".jian").click(function () {
var n = $(this).next().val();
var num = parseInt(n) - 1;
if (num == 0) {
return
}
$(this).next().val(num);
var index = $('.jian').index(this);
calculate();
});
})
function calculate() {
mTotal_money = 0;
var total_num = 0;
var car_product_num = 0;
var curr_money = $('.car_product_detail_price');
var product_num = $('.num');
for (var i = 0; i < curr_money.length; i++) {
var product_selecte = $('.car_product_detail_price').eq(i).parent().prev().prev().children(0).children(0);
if (product_selecte.attr('clickNum') == 'two' && product_selecte.attr('statu') != '001') {
var price = parseInt(product_num.eq(i).val()) * parseInt($('.product_car_initValue').eq(i).val());
mTotal_money += price;
updatePrice(mTotal_money);
car_product_num++;
total_num += parseInt(product_num.eq(i).val());
}
if (car_product_num == 0) {
updatePrice(0);
$('#total_num').html("(" + 0 + ")");
}
}
$('#total_num').html("(" + total_num + ")");
}
$(document).ready(function () {
$('.car_store').click(function () {
var num = $(this).attr('clicknum');
if (num == 'one') {
$(this).parents('.product_car_headInfo').siblings().find('.car_select').html('').css('color', '#9f9f9d');
$(this).parents('.product_car_headInfo').siblings().find('.car_select').attr('clicknum', 'one');
} else {
$(this).parents('.product_car_headInfo').siblings().find('.car_select').html('').css('color', '#f24f00');
$(this).parents('.product_car_headInfo').siblings().find('.car_select').attr('clicknum', 'two');
}
calculate();
});
});
function updatePrice(price) {
if (price >= 0)
$('#product_totalPrice').html('合计' + price);
else
$('#product_totalPrice').html('合计' + price);
}
var cookieJson = [];
function saveProductInfo() {
var car_select = $('.deleteTag');
var cookieJsonStr = '';
for (var i = 0; i < car_select.length; i++) {
if (car_select.eq(i).attr('clickNum') == 'two') {
var num = $('.num').eq(i).val();
var proId = $('.product_id_initValue').eq(i).val();
if (i != car_select.length - 1) {
cookieJsonStr += '[' + proId + ',' + num + ',' + 1 + '],';
} else {
cookieJsonStr += '[' + proId + ',' + num + ',' + 1 + ']';
}
}
}
var cookieJson = '[' + cookieJsonStr + ']';
setCookie('cart_info', cookieJson, "d30");
var xiaozhuangCart = new XiaozhuangCart();
$.get("http://127.0.0.1:8000/mobile/mall/cart/price", function (data, status) {
alert("Data: " + data.total_price + "\nStatus: " + status);
});
}
function isNull() {
var car_product_num = 0;
var curr_money = $('.car_product_detail_price');
var product_num = $('.num');
for (var i = 0; i < curr_money.length; i++) {
var product_selecte = $('.car_product_detail_price').eq(i).parent().prev().prev().children(0).children(0);
if (product_selecte.attr('clickNum') == 'two' && product_selecte.attr('statu') != '001') {
car_product_num++;
}
if (car_product_num == 0) {
return 1;
}
return 0;
}
}
$(function () {
saveProductInfo();
var cookieJson = getCookie("car_Info");
var cooieJsonVar = JSON.parse(cookieJson);
});
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
function setCookie(name, value, time) {
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() + strsec * 1);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
function getsec(str) {
var str1 = str.substring(1, str.length) * 1;
var str2 = str.substring(0, 1);
if (str2 == "s") {
return str1 * 1000;
}
else if (str2 == "h") {
return str1 * 60 * 60 * 1000;
}
else if (str2 == "d") {
return str1 * 24 * 60 * 60 * 1000;
}
}
function XiaozhuangCart() {
this.cart_item_map = {};
try {
var cart_info_string = $.cookie('cart_info');
if (typeof(cart_info_string) == 'undefined') {
cart_info_string = '[]';
}
console.log('The cart info string is:' + cart_info_string);
for (var pid_and_num in window.JSON.parse(cart_info_string)) {
this.cart_item_map[pid_and_num[0]] = pid_and_num[1];
}
} catch (e) {
console.log("Get cart info from cookie failed: " + e);
}
this.cart_item_map = {
2: 10,
};
}
;
XiaozhuangCart.prototype.addProductToCart = function (pid, num) {
this.cart_item_map[pid] = num;
return this;
};
XiaozhuangCart.prototype.removeProductFromCart = function (pid) {
delete this.cart_item_map[pid];
return this;
};
XiaozhuangCart.prototype.saveToLocal = function () {
var cart_item_list = [];
for (var pid in this.cart_item_map) {
cart_item_list.push([pid, this.cart_item_map[pid]]);
}
$.cookie('cart_info', window.JSON.stringify(cart_item_list), {expires: 30});
console.log("Save cookie to local: " + $.cookie('cart_info'));
return this;
};
</script>
</div>
{% endblock %}