const _Http = getApp().globalData.http;
Page({
    data: {
        isShowTabbar: true, //是否显示底部
        tabsActive: 1, //tabs 选中项
        sa_competitorid: 0,
        detail: {}, //详情数据
        briefs: [], //简介列表
        tabsList: [{
            label: "详细信息",
            icon: "icon-tabxiangxixinxi1"
        }, {
            label: "参与项目",
            icon: "icon-tabxiangmu"
        }, {
            label: "地址管理",
            icon: "icon-tabdizhi"
        }, {
            label: "联系人",
            icon: "icon-tablianxiren"
        }, {
            label: "跟进动态",
            icon: "icon-tabgenjinjilu"
        }, {
            label: "操作记录",
            icon: "icon-tabcaozuojilu1"
        }, {
            label: "附件",
            icon: "icon-tabfujian1"
        }, {
            label: "任务",
            icon: "icon-tabrenwu"
        }],
        list1: [], //基本信息
        list2: [], //系统信息
        tabbarList: [{
            icon: "icon-bianji",
            label: "编辑"
        }, {
            icon: "icon-shanchu",
            label: "作废"
        }]
    },
    onLoad(options) {
        this.setData({
            sa_competitorid: options.id,
            isAdmin: wx.getStorageSync('auth').wPublicCustomer.options.some(v => v == "admin"), //是否具有管理权限
		})
        this.getDetail();
    },
    //详情按钮回调
    tabbarOnClick({
        detail
    }) {
        let data = this.data.detail;
        switch (detail.label) {
            case "跟进":
                wx.navigateTo({
                    url: `/packageA/setclient/modules/trace/add/index?ownertable=sa_competitor&ownerid=${data.sa_competitorid}`,
                })
                break;
            case "编辑":
                wx.navigateTo({
                    url: `/packageA/opponent/add?data=${JSON.stringify(data)}`,
                })
                break;
            case "作废":
                wx.navigateTo({
                    url: `/packageA/setclient/delete?item=${JSON.stringify({
                        "sa_competitorids": [this.data.detail.sa_competitorid],
                        datastatus: 1, //（0:正常；1:作废；2:锁定）
                    })}&id=20221018164202`,
                })
                break;
            default:
                console.log(detail)
                break;
        }
    },
    /* 获取详情 */
    getDetail() {
        /* 基本信息 */
        _Http.basic({
            "id": 20221019105802,
            "content": {
                nocache: true,
                sa_competitorid: this.data.sa_competitorid
            },
        }).then(res => {
            console.log("竞争对手详情", res)
            if (res.msg != '成功') return wx.showToast({
                title: res.data,
                icon: "none"
            })
            /* 摘要信息 */
            let briefs = [{
                label: "品牌",
                value: res.data.brandname
            }, {
                label: "优势信息",
                value: res.data.advantage
            }, {
                label: "劣势信息",
                value: res.data.inferiority
            }];
            /* 基本信息 */
            let list1 = [{
                label: "名称",
                value: res.data.enterprisename
            }, {
                label: "品牌",
                value: res.data.brandname
            }, {
                label: "优势信息",
                value: res.data.advantage
            }, {
                label: "劣势信息",
                value: res.data.inferiority
            }, {
                label: "备注信息",
                value: res.data.remarks
            }];
            /* 系统信息 */
            let list2 = [{
                label: "创建人",
                value: res.data.createby
            }, {
                label: "创建时间",
                value: res.data.createdate
            }, {
                label: "最近编辑人",
                value: res.data.changeby
            }, {
                label: "最近编辑时间",
                value: res.data.changedate
            }];
            this.setData({
                detail: res.data,
                briefs,
                list1,
                list2,
                isShowTabbar: this.data.isAdmin || res.data.createuserid == wx.getStorageSync('userMsg').userid
            });
            //获取标签
            this.getTags();
            this.partialRenewal();
        });
    },
    //tabs 切换
    tabsChange({
        detail
    }) {
        this.setData({
            tabsActive: detail
        });
        this.partialRenewal();
    },
    //局部数据更新 tabs
    partialRenewal(init = false) {
        let id = this.data.detail.sa_competitorid;
        let model = '';
        let name = this.data.tabsList[this.data.tabsActive].label;
        switch (name) {
            case "任务":
                model = "#Work"
                break;
            case "参与项目":
                model = "#Project"
                break;
            case "跟进动态":
                model = "#Trace"
                break;
            case "操作记录":
                model = "#Record"
                break;
            case "附件":
                model = "#Files"
                break;
            case "地址管理":
                id = this.data.detail.sys_enterpriseid;
                model = "#Address"
                break;
            case "联系人":
                id = this.data.detail.sys_enterpriseid;
                model = "#Contacts"
                break;
        };
        if (model) {
            // 确定好模块ID total = null 是第一次加载 加载数据
            // init是新增或修改需要清数据
            let Component = this.selectComponent(model);
            const {
                total,
                pageNumber,
                pageTotal
            } = Component.data.content;
            if (total == null || init) {
                console.log(1)
                Component.getList(id, init);
            } else if (pageNumber <= pageTotal) {
                console.log(2)
                Component.getList(id, false);
            } else {
                //用来判断 在搜索页面修改，与tabs选项不一致 但是切换到该选项 重置数据
            }
        }
    },
    //更新标签
    getTags() {
        this.selectComponent("#Tags").getTags();
    },
    onReachBottom() {
        this.partialRenewal();
    },
    onShareAppMessage() {}
})