import agentApi from '../../apis/agent'
import { encryption } from '../../utils/encryption'
import { bindInput } from '../../utils/utils'
import { isPhoneX } from './../../utils/tools'
const panelData={
    'mudi':[
        {
            id:'mudi',
            items:[{
                name:'不限',
                id:''
            },{
                name:'刚需',
                id:''
            },{
                name:'改善',
                id:''
            }]
        }
    ],
    'yixiang':[
        {
            id:'yixiang',
            items:[{
                name:'不限',
                id:''
            },{
                name:'强烈',
                id:3,
                icon_num: 3,
                icon: 'iconcus_star',
            },{
                name:'一般',
                id:2,
                icon: 'iconcus_star',
                icon_num: 2
            },{
                name:'较弱',
                id:1,
                icon: 'iconcus_star',
                icon_num: 1
            },{
                name:'暂不关注',
                id:0
            }]
        }
    ],
    'paixu':[
        {
            id:'paixu',
            items:[{
                name:'不限',
                id:''
            },{
                name:'最近维护',
                id:'time'
            }]
        }
    ]
}

let type_acive = ''


const app = getApp();
Page({
    /**
     * 组件的属性列表
     */
    data: {
        clients: [],
        //todo 4个 icon栏位，可后台配置, 调用IconClick函数。
        icon_list: [
            {name: '目的',icon:'edit',type:"mudi"},
            {name: '意向',icon:'edit',type:"yixiang"},
            {name: '排序',icon:'write',type:"paixu"}
        ],
        type_acive,
        tab_panel:panelData[type_acive]||[],
        fetcherData: {
            page: 1,
            agency_user_id: '',
            size: 10,
            user_wish: '',
            purpose:''
        },
        has_next: true,
        navbarInitTop: 0,
        seachKey:'',
    },
    bindInput,
    changeListType(e){
        let {type} = e.currentTarget.dataset
        let obj = {
            type_acive:type,
            tab_panel:panelData[type]
        }
        if (type===this.data.type_acive) {
            obj['type_acive'] =''
        }
        this.setData(obj)
    },
    selecthandle(e){
        let {selectObj} = e.detail
        let tmp = Object.assign(this.data.fetcherData,{
            purpose:(selectObj.mudi)?String(selectObj.mudi.name):'',
            user_wish:selectObj.yixiang?selectObj.yixiang.id:'',
            order: selectObj.paixu ? selectObj.paixu.id : ''
        })
        this.setData({
            fetcherData: tmp
        })
        this.getClientList()
    },
    closeTypeActive(){
        this.setData({
            type_acive:''
        })
    },
    toAddClient(){
        wx.navigateTo({
            url: '/pages/add-client/add-client'
        })
    },

    onReachBottomDistance: 50,
    onReachBottom: function () {
        let page =  this.data.fetcherData.page++
        this.setData({
            'fetcherData.page': page
        })
        this.getClientList()
    },
    
    // onPageScroll: function (e) {  // 调用showImg函数
    //     var top = e.scrollTop;
    //     try {
    //         //判断'滚动条'滚动的距离 和 '元素在初始时'距顶部的距离进行判断
    //         var isSatisfy = top >= this.data.navbarInitTop ? true : false;
    //         //为了防止不停的setData, 这儿做了一个等式判断。 只有处于吸顶的临界值才会不相等
    //         if (this.data.isFixedTop === isSatisfy) {
    //             return false;
    //         }
    //         console.log(isSatisfy)
    //         this.setData({
    //             isFixedTop: isSatisfy
    //         });
    //     } catch (e) {
    //         console.log(e)
    //     }
    // },
    onShow: function () {
        this.getClientList()
    },
    getClientList: function () {
        this.setData({
            'fetcherData.agency_user_id': app.globalData.userinfo.agency_user_id
        })
        app.wxshowloading('拼命加载中...');
        agentApi.fetchCustomers(this.data.fetcherData).then(res => {
            this.setData({
                clients: res.data.list
            })
        }).finally(() => {
            wx.hideLoading()
        })
    },

    tapSearch(){
        this.setData({
            'fetcherData.user_name': this.data.seachKey,
        })
        this.getClientList()
    },
    
    onLoad: function (options) {
        isPhoneX().then(res => {
            this.setData({
                isPhoneX: res
            })
        })
    }
  
});
