// packageA/mycomponent/goods_telephone_charges_pro/goods_telephone_charges_pro.js
var app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        prop_goods_info: {
            type: null, //父组件的商品详情数据
        },
        goodsId: {
            type: Number,
        },
        goodsType: {
            type: String,
        },
        hoet: {
            type: null,
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        goodsInfo: {},

        rechargeType: {},
        rechargeItem: {},
        rechargeSpecId: 0,

        displayOptions: [],
        optionsId: 0,

        show1: false,
        service_QRcode: "",
        service_mobile: "",
        cservice: "",

        //猜你喜欢
        likeArr: [],
        swipeIndex: 0,

        mobile: "",

        bannerUrl: "",

        posterShow: false,

        tapSelect: false,

        AccountList: [],
        currentAccount: null,
        editUserShow: false,

        form_account_name: "",
        form_account_number: "",
        form_charge_type: 1,

        addressShow_p: false,

        form_province: "",
        form_city: "",

        selectUserShow: false,
        radioAccount: "1",

        tipsShow: false,

        desc_text: "",
        codeArr: [{
                value: '',
                focusa: true
            },
            {
                value: '',
                focusa: false
            },
            {
                value: '',
                focusa: false
            },
            {
                value: '',
                focusa: false
            },
            {
                value: '',
                focusa: false
            },
            {
                value: '',
                focusa: false
            }
        ],
        codeValue: '',
        isProvinceShow: false
    },
    ready() {
        console.log(this.properties.prop_goods_info);
        let prop_goods_info = this.properties.prop_goods_info;
        let changeData = {};
        let goodsInfo = this.data.goodsInfo = prop_goods_info.get_goods;
        let rechargeType = goodsInfo.has_many_specs[0];
        this._handClickSpecs(rechargeType.specitem[0]);
        changeData.goodsInfo = goodsInfo;
        changeData.rechargeType = rechargeType;
        //轮播图
        if (prop_goods_info && prop_goods_info.plugin && prop_goods_info.plugin.electricityBillPro) {
            changeData.bannerUrl = prop_goods_info.plugin.electricityBillPro.banner;
        }

        //客服相关
        if (prop_goods_info.customer_service) {
            // 客服
            changeData.service_QRcode = prop_goods_info.customer_service.service_QRcode;
            changeData.service_mobile = prop_goods_info.customer_service.service_mobile;
            changeData.cservice = prop_goods_info.customer_service.cservice;
        }

        changeData.desc_text = prop_goods_info.plugin.electricityBillPro.desc;

        //猜你喜欢
        let show_push = goodsInfo.show_push.slice(0, 30);
        let likeArr = [];
        show_push.forEach(function (item, index) {
            var num = Math.floor(index / 6); //
            if (!likeArr[num]) {
                likeArr[num] = [];
            }
            likeArr[num].push(item); //将数据添加到数组里
        });
        changeData.likeArr = likeArr;
        this.setData(changeData);
        this.getDataAccountList();
        this.getAddressProvince();
    },
    /**
     * 组件的方法列表
     */
    methods: {
        // handClickSpecs(evt) {
        //   let item = evt.currentTarget.dataset.item;
        //   this._handClickSpecs(item);
        // },
        setEditUserShow() {
            this.setData({
                editUserShow: !this.data.editUserShow
            });
        },
        setSelectUserShow() {
            this.setData({
                selectUserShow: !this.data.selectUserShow
            });
        },
        setTipsShow() {
            this.setData({
                tipsShow: !this.data.tipsShow
            });
        },
        _handClickSpecs(item) {
            if (this.data.rechargeSpecId == item.id) return;
            let has_many_options = this.data.goodsInfo.has_many_options;
            let arr = [];
            for (let i = 0; i < has_many_options.length; i++) {
                //if (has_many_options[i].specs.indexOf(item.id) >= 0) {
                arr.push(has_many_options[i]);
                // }
            }
            this.setData({
                //  rechargeSpecId: item.id,
                //  rechargeItem: item,
                displayOptions: arr,
                optionsId: 0
            });
        },

        handClickOptions(evt) {
            let item = evt.currentTarget.dataset.item;
            this.setData({
                optionsId: item.id
            });
        },

        onChangeSwipeIndex(evt) {
            let index = evt.detail.current;
            this.setData({
                swipeIndex: index
            });
        },
        gotoDetailV2(evt) {
            let id = evt.currentTarget.dataset.id;
            wx.navigateTo({
                url: '/packageA/detail_v2/detail_v2?id=' + id,
            });
        },
        gotoChat(evt) {
            let url = evt.currentTarget.dataset.cservice + "&goods_id=" + this.data.goodsId;
            wx.navigateTo({
                url: url,
                fail: (err) => {
                    app.tips("客服路由出错");
                }
            });
        },

        setCserviceFlag() {
            this.setData({
                show1: !this.data.show1
            });
        },

        gotoOrderList() {
            wx.navigateTo({
                url: '/packageA/member/myOrder_v2/myOrder_v2',
            });
        },

        gotoIndex() {
            wx.reLaunch({
                url: '/packageG/index/index',
            });
        },

        setPosterShow() {
            this.setData({
                posterShow: true
            });
        },
        setTopping() {
            wx.pageScrollTo({
                scrollTop: 0
            });
        },

        sendRecharge() {
            // 手机慢充
            let {
                goodsId,
                optionsId
            } = this.data;
            // if (!mobile) {
            //   app.tips("请输入充值手机号码！");
            //   return false;
            // } else {
            //   let reg_txt = /^[1][0-9]{10}$/;
            //   if (!reg_txt.test(mobile)) {
            //     app.tips("请输入正确的充值手机号码！");
            //     return false;
            //   }
            // }
            if (!this.data.currentAccount) return app.tips("请先添加户号");
            if (optionsId <= 0) return app.tips("请选择充值金额");

            wx.navigateTo({
                url: "/packageD/buy/myOrder_v2/myOrder_v2?tag=-2&total=1&&goodsId=" + goodsId + "&optionsId=" + optionsId + "&account_id=" + this.data.currentAccount.id + "&electricity=electricity"
            });
            //this.$router.push(this.fun.getUrl("goodsorder", {}, json_data));
        },

        tapSelect_btn() {
            this.setData({
                tapSelect: !this.data.tapSelect
            });
        },
        checkProvince(key) {
            let pro = key || this.data.form_province;
            if (pro == "广东省") {
                return true;
            } else if (pro == "广西壮族自治区") {
                return true;
            } else if (pro == "云南省") {
                return true;
            } else if (pro == "贵州省") {
                return true;
            } else if (pro == "海南省") {
                return true;
            } else if (pro == "澳门特别行政区") {
                return true;
            } else if (pro == "香港特别行政区") {
                return true;
            } else {
                return false;
            }
        },
        saveBox() {
            if (app._isTextEmpty(this.data.form_account_name)) return app.tips("填写户名");
            if (app._isTextEmpty(this.data.form_account_number)) return app.tips("请输入缴费户号");
            if (app._isTextEmpty(this.data.form_province) || app._isTextEmpty(this.data.form_city)) return app.tips("请选择所在省市");
            let check = this.checkProvince();
            if (check) {
                if (app._isTextEmpty(this.data.codeValue)) {
                    wx.showModal({
                        title: '',
                        content: '请输入身份证/营业执照/机构代码后6位',
                        showCancel:false
                    })
                    return
                } else if (this.data.codeValue.length < 6) {
                    wx.showModal({
                        title: '',
                        content: '请输入身份证/营业执照/机构代码后6位',
                        showCancel:false
                    })
                    return
                }
            }
            let urlStr = app.getNetAddresss("plugin.electricity-bill-pro.frontend.accountUser.saveAccount");
            let form = {
                account_name: this.data.form_account_name,
                account_number: this.data.form_account_number,
                province: this.data.form_province,
                city: this.data.form_city,
                charge_type: this.data.form_charge_type
            };
            if (check) {
                form.code = this.data.codeValue;
            }
            let json = {
                form
            };
            if (this.data.AccountID) {
                json.id = this.data.AccountID;
            }
            
            app._postNetWork({
                url: urlStr,
                data: json,
                success: (resdata) => {
                    var res = resdata.data;
                    app.tips(res.msg);
                    if (res.result == 1) {
                        this.setData({
                            AccountID: "",
                            editUserShow: false
                        });
                        this.getDataAccountList();
                    }
                },
                fail: function (res) {
                    console.log(res);
                },
            });
        },

        newCreate() {
            this.setData({
                form_account_name: "",
                form_account_number: "",
                form_province: "",
                form_city: "",
                form_charge_type: 1,
                AccountID: "",
                editUserShow: true,
                isProvinceShow:false,
                codeValue:''
            });
        },

        onChangeCurrentAccount(evt) {
            let id = evt.detail;
            for (let i = 0; i < this.data.AccountList.length; i++) {
                if (this.data.AccountList[i].id == id) {
                    this.setData({
                        currentAccount: this.data.AccountList[i],
                        radioAccount: id,
                        selectUserShow: false
                    });

                }
            }
        },

        async delIcon(evt) {
            let item = evt.currentTarget.dataset.item;
            let confirmFlag = await app.confirm("确定删除吗？");
            if (!confirmFlag) return;
            let urlStr = app.getNetAddresss("plugin.electricity-bill-pro.frontend.accountUser.delAccount");
            app._getNetWork({
                url: urlStr,
                data: {
                    id: item.id
                },
                success: (resdata) => {
                    var res = resdata.data;
                    app.tips(res.msg);
                    if (res.result == 1) {
                        if (item.id == this.data.currentAccount.id) {
                            this.setData({
                                currentAccount: null
                            });
                        }
                        this.getDataAccountList();
                    }
                },
                fail: function (res) {
                    console.log(res);
                },
            });
        },

        // 编辑
        editIcon(evt) {
            let item = evt.currentTarget.dataset.item;
            let check = this.checkProvince(item.province);
            if(check){
                if(item.code){
                    this.setData({
                        isProvinceShow:true,
                        codeValue:item.code
                    })
                }else{
                    this.setData({
                        isProvinceShow:true,
                        codeValue:""
                    })
                }
            }else{
                this.setData({
                    isProvinceShow:false
                })
            }
            
            this.setData({
                selectUserShow: false,
                editUserShow: true,
                form_account_name: item.account_name,
                form_account_number: item.account_number,
                form_province: item.province,
                form_city: item.city,
                form_charge_type: item.charge_type,
                AccountID: item.id
            });
        },

        getDataAccountList() {
            let urlStr = app.getNetAddresss("plugin.electricity-bill-pro.frontend.accountUser.getAccountList");
            app._getNetWork({
                url: urlStr,
                success: (resdata) => {
                    var res = resdata.data;
                    if (res.result == 1) {
                        this.setData({
                            AccountList: res.data,
                            radioAccount: res.data[0] ? res.data[0].id : 0
                        });
                        // if (!this.data.currentAccount) {
                            this.setData({
                                currentAccount: res.data[0] ? res.data[0] : null,
                            });
                        // }
                    }
                },
                fail: function (res) {
                    console.log(res);
                },
            });
        },

        getAddressProvince() {
            let urlStr = app.getNetAddresss("plugin.electricity-bill-pro.frontend.accountUser.getRegion");
            app._getNetWork({
                url: urlStr,
                success: (resdata) => {
                    var res = resdata.data;
                    if (res.result == 1) {
                        let list = res.data;
                        this.setData({
                            provinceData: [{
                                values: list,
                                className: "columnl"
                            }, {
                                values: list[0].children,
                                className: "column2"
                            }]
                        });
                    }
                },
                fail: function (res) {
                    console.log(res);
                },
            });
        },

        onChangeChargeType(event) {
            this.setData({
                form_charge_type: event.detail,
            });
        },

        setAddressShow_p() {
            this.setData({
                addressShow_p: !this.data.addressShow_p
            });
        },

        onChangeAddress(event) {
            const {
                picker,
                value
            } = event.detail;
            console.log(value);
            picker.setColumnValues(1, value[0].children);
        },

        addressCallback(evt) {
            let {
                value
            } = evt.detail;
            // if (value[1].is_open != "1") return app.tips("该地区不支持电费充值");
            this.setData({
                form_province: value[0].text,
                form_city: value[1].text,
                addressShow_p: false
            });
            let check = this.checkProvince();
            if (check == true) {
                this.setData({
                    isProvinceShow: true
                })
            }else{
                this.setData({
                    isProvinceShow: false
                })
            }
            // if (this.provinceData[ind[0]].children[ind[1]].is_open == 0) {
            //   this.$dialog.alert({ message: "该地区不支持电费充值" });
            // } else {
            //   this.form.province = e[0];
            //   this.form.city = e[1];
            //   this.addressShow_p = false;
            // }
        },





    }
});