using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using CallCenterApi.Interface.Models.Filter;
using CallCenterApi.Interface.Models.Input;
using CallCenterApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.order
{
    public class OrderController : BaseController
    {
        private BLL.orders orderBLL = new BLL.orders();
        private BLL.article productBLL = new BLL.article();
        /// <summary>
        /// 获取订单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ActionResult GetList(FilterOrder filter)
        {
            var sql = " ";
            var recordCount = 0;
            var dt = BLL.PagerBLL.GetListPager(
                 "dt_orders",
                 "id",
                 "*",
                 sql,
                 "ORDER BY id desc ",
                 filter.PageSize,
                 filter.PageIndex,
                 true,
                 out recordCount);
            var list = orderBLL.DataTableToList(dt);
            var obj = new
            {
                rows = list.Select(x => new
                {
                    id = x.id,
                    orderno = x.order_no,
                    usercode = x.user_name,
                    expressno = x.express_no,
                    acceptname = x.accept_name,
                    postcode = x.post_code,
                    telphone = x.telphone,
                    mobile = x.mobile,
                    remark = x.remark,
                    orderamount = x.order_amount,
                    status = Enum.GetName(typeof(EnumOrderStatus), x.status),
                    addtime = x.add_time
                }),
                total = recordCount
            };
            return Content(obj.ToJson());
        }

        /// <summary>
        /// 获取订单实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetModel(int id)
        {
            var model = orderBLL.GetModel(id);
            return Success("获取成功", model);
        }

        /// <summary>
        /// 增加/修改订单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public ActionResult CreateOrUpdate(OrderInput input)
        {
            var model = new Model.orders();
            if (input.Id <= 0)
            {

                //订单号B开头为商品订单
                model.user_id = input.UserId;
                model.accept_name = input.Name;
                model.telphone = input.Telephone;
                model.mobile = input.Mobile;
                model.address = input.Provance + input.City + input.Area + input.Address;
                model.express_id = input.PostType;
                model.remark = input.Remark;

                model.usercode = string.IsNullOrWhiteSpace(input.UserCode) ? CurrentUser.UserData.F_UserCode : input.UserCode;


                if (string.IsNullOrWhiteSpace(input.OrderDetail))
                    return Error("请选择商品");
                decimal allAmount = 0;
                var arr = input.OrderDetail.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var item in arr)
                {
                    var arrItem = item.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    var productId = arrItem[0];
                    var count = arrItem[1];
                    var price = arrItem[2];

                    if (productBLL.Exists(0, int.Parse(productId)))
                        return Error("不存在该商品");

                    allAmount += decimal.Parse(price) * decimal.Parse(count);

                }



            }

            return View();
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public ActionResult UpdateOrderState(string order)
        {
            return View();
        }

        public ActionResult UpdateOrdrTrack()
        {
            return Success("");
        }
    }
}