const vm = new Vue({
    el: "#app",
    data: {
        current_page: '1',
        page_size: '10',
        main_corp_list: [],
        user_area_list: [],
        order_status_list: [],
        order_type_list: [],
        order_come_list: [],
        order_validity_count: 0,
        GMV: 0,
        start_time: getDateByDay(-7, getDateTime(0)),
        end_time: '',
        start_date_start: '',
        start_date_end: '',
        order_listinfo: [],
        temp_order_listinfo: [],
        edit_obj_index:'',
        edit_obj:{},
        no_info: "无查询结果",
        select_order_type: '',
        select_order_status: '',
        order_id: ''
    },
    //过滤器
    filters: {
        filterReplace: function (total_money) {
            return `￥${total_money}`;
        }
    },
    //钩子函数
    mounted: function () {
        this.$nextTick(function () {

            this.httpBaseInfo();
        })
    },
    //方法
    methods: {
        //解决jquery日历插件的问题
        loadJqueryDate() {
            $(".form_date").datetimepicker({
                format: 'yyyy-mm-dd',
                autoclose: true,
                minView: 2,
                pickerPosition: "bottom-left"
            });

        },
        loadJquerySelect(){
            setTimeout(function () {
                $('.order_come').comboSelect();
            }, 0);
        },
        //配置页面参数基本信息
        httpBaseInfo() {
            let _this = this;
            this.$http.post(
                base_api + "order/order/get-initialize", {}, {emulateJSON: true}
            ).then(res => {
                    let res_data = res.body;
                    let data = res_data.data;
                    ZZLog('参数返回', data);
                    this.order_status_list = data.order_status_list;
                    this.order_type_list = data.order_type_list;
                    this.order_come_list = data.fx_user_list;
                    if (this.order_come_list) {
                        this.order_come_list.unshift({
                            fx_uid: "",
                            wechat_name: "全部"
                        });
                    }
                    //运营负责人
                    this.main_corp_list = data.main_corp_list;
                    if (this.main_corp_list) {
                        this.main_corp_list.unshift({
                            id: "",
                            user_name: "全部"
                        });
                    }
                    //分销商所属区域
                    this.user_area_list = data.user_area_list;
                    if (this.user_area_list) {
                        this.user_area_list.unshift({
                            area_id: "",
                            area_name: "全部"
                        });
                    }


                    this.loadJquerySelect();
//					this.searchDidClick();
                },
                error => {
                    ZZAlertInfo(infoApiError);
                })
            this.loadJqueryDate();
            this.httpFirstData();
        },
        //搜索按钮的点击事件
        searchDidClick() {
            this.httpFirstData();
        },
        //导出订单列表
        exportOrderList(){
            this.start_time = $('#start_time').val();
            this.end_time = $('#end_time').val();
            this.start_date_start = $('#start_date_start').val();
            this.start_date_end = $('#start_date_end').val();
            let order_id = this.order_id;
            let order_type_id = this.select_order_type;
            let start_time = this.start_time;
            let end_time = this.end_time;
            let order_status_id = this.select_order_status;
            let start_date_start = this.start_date_start;
            let start_date_end = this.start_date_end;
            let fx_uid = $('.order_come').val();
            let main_search = $('.main_search').val();
            let contacts_name = $('#contacts_name').val();
            let contacts_phone = $('#contacts_phone').val();
            let user_area = $('.user_area').val();
            let cstr = `order/order/export-order-list&contacts_name=${contacts_name}&contacts_phone=${contacts_phone}&start_time=${start_time}&end_time=${end_time}&start_date_start=${start_date_start}&start_date_end=${start_date_end}&order_id=${order_id}&order_type_id=${order_type_id}&order_status_id=${order_status_id}&fx_uid=${fx_uid}&main_search=${main_search}&user_area=${user_area}`;
            location.href = base_api + cstr;
        },
        //第一次查询数据 (搜索按钮的点击查询)
        httpFirstData() {
            this.start_time = $('#start_time').val();
            this.end_time = $('#end_time').val();
            this.start_date_start = $('#start_date_start').val();
            this.start_date_end = $('#start_date_end').val();
            this.current_page = '1';
            let order_id = this.order_id;
            let order_type_id = this.select_order_type;
            let start_time = this.start_time;
            let end_time = this.end_time;
            let order_status_id = this.select_order_status;
            let start_date_start = this.start_date_start;
            let start_date_end = this.start_date_end;
            let main_search = $('.main_search').val();
            let fx_uid = $('.order_come').val();
            let contacts_name = $('#contacts_name').val();
            let contacts_phone = $('#contacts_phone').val();
            let user_area = $('.user_area').val();
            let data = {
                current_page: this.current_page,
                page_size: this.page_size,
                order_id,
                order_type_id,
                start_time,
                end_time,
                order_status_id,
                start_date_start,
                start_date_end,
                fx_uid,
                main_search,
                contacts_name,
                contacts_phone,
                user_area
            };

            let urlstr = base_api + 'order/order/get-order-list';
            this.$http.post(urlstr, data, {emulateJSON: true}).then(res => {
                let res_data = res.body;
                ZZLog(res_data);
                if (res_data.flag == false) {
                    ZZAlertInfo(res_data.msg, () => {
                        ifLogin(res_data);
                    });
                } else {
                    let data = res_data.data;
                    let page = data.page;
                    this.order_validity_count = data.order_validity_count;
                    this.GMV = data.GMV;
                    this.order_listinfo = data.order_list;
                    this.temp_order_listinfo = clone(data.order_list);
                    function clone(obj) {
                        // Handle the 3 simple types, and null or undefined
                        if (null == obj || "object" != typeof obj) return obj;

                        // Handle Date
                        if (obj instanceof Date) {
                            var copy = new Date();
                            copy.setTime(obj.getTime());
                            return copy;
                        }

                        // Handle Array
                        if (obj instanceof Array) {
                            var copy = [];
                            for (var i = 0, len = obj.length; i < len; ++i) {
                                copy[i] = Object.assign({}, obj[i]);
                            }
                            return copy;
                        }

                        // Handle Object
                        if (obj instanceof Object) {
                            return Object.assign({}, obj);
                        }
                        console.warn("Unable to copy obj! Its type isn't supported.");
                        throw new Error("Unable to copy obj! Its type isn't supported.");
                    };
                    let totalpage = page.total_pages;
                    $('#body_list').show();
                    this.createPage(this.current_page, totalpage);
                    if (page.total_pages > 0) {
                        $('.pageDiv').show();
                    } else {
                        $('.pageDiv').hide();
                    }
                }
            }, error => {
                ZZAlertInfo(infoApiError);
            })
        },
        //创建分页控件
        createPage(currpage, totalpage) {
            $(".pageDiv").createPage({
                pageCount: totalpage, //总页数
                current: currpage, //当前页
                turndown: 'true', //是否显示跳转框，显示为true，不现实为false,一定记得加上引号...
                backFn: (p) => {
                    this.current_page = p;
                    this.httpData();
                }
            })
        },
        //分页查询数据
        httpData() {
            this.start_time = $('#start_time').val();
            this.end_time = $('#end_time').val();
            this.start_date_start = $('#start_date_start').val();
            this.start_date_end = $('#start_date_end').val();
            let _current_page = this.current_page;
            let order_id = this.order_id;
            let order_type_id = this.select_order_type;
            let start_time = this.start_time;
            let end_time = this.end_time;
//			this.start_time = $('#start_time').val();
//			this.end_time = $('#end_time').val();
            let order_status_id = this.select_order_status;
            let start_date_start = this.start_date_start;
            let start_date_end = this.start_date_end;
            let main_search = $('.main_search').val();
            let fx_uid = $('.order_come').val();
            let contacts_name = $('#contacts_name').val();
            let contacts_phone = $('#contacts_phone').val();
            let user_area = $('.user_area').val();
            let data = {
                current_page: this.current_page,
                page_size: this.page_size,
                order_id,
                order_type_id,
                start_time,
                end_time,
                order_status_id,
                start_date_start,
                start_date_end,
                fx_uid,
                main_search,
                contacts_name,
                contacts_phone,
                user_area
            }
            let urlstr = base_api + 'order/order/get-order-list';
            ZZLog('分页数据参数', data);
            this.$http.post(urlstr, data, {emulateJSON: true}).then(res => {
                let res_data = res.body;
                ZZLog(res_data);
                if (res_data.flag == false) {
                    ZZAlertInfo(res_data.msg, () => {
                        ifLogin(res_data);
                    });
                } else {
                    let data = res_data.data;
                    let page = data.page;
                    this.order_validity_count = data.order_validity_count;
                    this.GMV = data.GMV;
                    this.order_listinfo = data.order_list;
                    this.temp_order_listinfo = clone(data.order_list);
                    function clone(obj) {
                        // Handle the 3 simple types, and null or undefined
                        if (null == obj || "object" != typeof obj) return obj;

                        // Handle Date
                        if (obj instanceof Date) {
                            var copy = new Date();
                            copy.setTime(obj.getTime());
                            return copy;
                        }

                        // Handle Array
                        if (obj instanceof Array) {
                            var copy = [];
                            for (var i = 0, len = obj.length; i < len; ++i) {
                                copy[i] = Object.assign({}, obj[i]);
                            }
                            return copy;
                        }

                        // Handle Object
                        if (obj instanceof Object) {
                            return Object.assign({}, obj);
                        }
                        console.warn("Unable to copy obj! Its type isn't supported.");
                        throw new Error("Unable to copy obj! Its type isn't supported.");
                    };
                    $('#body_list').show();
                }
            }, error => {
                ZZAlertInfo(infoApiError);
            })
        },
        //重置按钮
        resetDidClick() {
            this.order_id = '';
            this.select_order_type = '';
            this.select_order_status = '';
            this.start_date_start = '';
            this.start_date_end = '';
            this.start_time = '';
            this.end_time = '';
            $('#start_time').val('');
            $('#end_time').val('');
            $('#start_date_end').val('');
            $('#start_date_start').val('');

            $('.order_come').val('');
            $('.order_come').comboSelect();
            $('.main_search').val('');
            $('.main_search').comboSelect();
            $('#contacts_name').val('');
            $('#contacts_phone').val('');
            $('.user_area').val('');
            $('.user_area').comboSelect();
            this.httpFirstData();
        },
        //取消订单
        cancelDidClick(dict) {
            let urlstr = base_api + 'order/order/cancel';
            let data = {order_id: dict.order_id};
            ZZConfirm('确定取消当前订单？', () => {
                this.$http.post(urlstr, data, {emulateJSON: true}).then(res => {
                    let res_data = res.body;
                    ZZLog(res_data);
                    if (res_data.flag == false) {
                        ZZAlertInfo(res_data.msg, () => {
                            ifLogin(res_data);
                        });
                    } else {
                        this.httpData();
                    }
                }, error => {
                    ZZAlertInfo(infoApiError);
                })
            })
        },
        orderStatusChange() {
            this.start_time = $('#start_time').val();
            this.end_time = $('#end_time').val();
        },
        orderTypeChange() {
            this.start_time = $('#start_time').val();
            this.end_time = $('#end_time').val();
        },
        changStartTime(obj, index){
            this.edit_obj = obj;
            this.edit_obj_index = index;
        },
        cancelChangeOrder(){
            this.order_listinfo = clone(this.temp_order_listinfo);
            function clone(obj) {
                // Handle the 3 simple types, and null or undefined
                if (null == obj || "object" != typeof obj) return obj;

                // Handle Date
                if (obj instanceof Date) {
                    var copy = new Date();
                    copy.setTime(obj.getTime());
                    return copy;
                }

                // Handle Array
                if (obj instanceof Array) {
                    var copy = [];
                    for (var i = 0, len = obj.length; i < len; ++i) {
                        copy[i] = Object.assign({}, obj[i]);
                    }
                    return copy;
                }

                // Handle Object
                if (obj instanceof Object) {
                    return Object.assign({}, obj);
                }
                console.warn("Unable to copy obj! Its type isn't supported.");
                throw new Error("Unable to copy obj! Its type isn't supported.");
            };
        },
        //修改订单信息
        changeOrder(){
            obj = this.edit_obj;
            index = this.edit_obj_index;
            console.log(obj.start_time, this.temp_order_listinfo[index].start_time);
            if (obj.start_time == this.temp_order_listinfo[index].start_time) {
                ZZAlertInfo("请修改出发时间再提交！");
                return false;
            }
            let reg = /^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/;
            let regExp = new RegExp(reg);
            if (!regExp.test(obj.start_time)) {
                ZZAlertInfo("时间格式不正确,正确格式为:1970-01-01 00:00:00");
                return false;
            }

            let urlstr = base_api + 'order/order/change-start-time';
            let data = {order_id: obj.order_id,start_time:obj.start_time};
            ZZConfirm('确定修改当前订单的出发时间？', () => {
                this.$http.post(urlstr, data, {emulateJSON: true}).then(res => {
                    let res_data = res.body;
                    ZZLog(res_data);
                    if (res_data.flag == false) {
                        ZZAlertInfo(res_data.msg, () => {
                            ifLogin(res_data);
                        });
                    } else {
                        ZZAlertInfo("修改成功！");
                        this.httpData();
                        $('#setStartTimeModelDel').click();
                    }
                }, error => {
                    ZZAlertInfo(infoApiError);
                })
            })
        }
    }
})