const _Http = getApp().globalData.http,
    currency = require("../../utils/currency"),
    CNY = (value, symbol = "￥", precision = 2) => currency(value, {
        symbol,
        precision
    }).format();

import {
    getTypes
} from "./projectQuery";

Component({
    options: {
        addGlobalClass: true,
    },
    properties: {
        update: {
            type: Function
        }
    },
    lifetimes: {
        attached: function () {
            getApp().globalData.Language.getLanguagePackage(this)
            this.data.organization = this.selectComponent("#organization");
            this.data.organization.initDepAndUser().then(active => {
                let userid = active.userid || wx.getStorageSync("userMsg").userid;
                this.setData({
                    "content.dataid": userid,
                    userid,
                })
                this.triggerEvent('update', "Client")
            });
            this.setFiltratelist();
        }
    },
    data: {
        list: [],
        filtratelist: [],
        total: null,
        searchShow: false,
        searchValue: "",
        showFiltrate: false,
        "content": {
            "dataid": 0,
            "type": 0,
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": "",
                "tag": "",
                "status": "",
                "projecttype": "",
                "stagename": "",
                "tradefield": "",
                "begindate": "",
                "enddate": ""
            }
        }
    },
    methods: {
        async setFiltratelist() {
            let filtratelist = [{
                label: "项目类型",
                index: null,
                showName: "value", //显示字段
                valueKey: "projecttype", //返回Key
                selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
                value: "", //选中值
                list: await getTypes('projecttype')
            }, {
                label: "项目阶段",
                index: null,
                showName: "stagename", //显示字段
                valueKey: "stagename", //返回Key
                selectKey: "stagename", //传参 代表选着字段 不传参返回整个选择对象
                value: "", //选中值
                list: await getTypes('stagename')
            }, {
                label: "领域",
                index: null,
                showName: "value", //显示字段
                valueKey: "tradefield", //返回Key
                selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
                value: "", //选中值
                list: await getTypes('tradefield')
            }, {
                label: "标签",
                index: null,
                type: "checkbox",
                showName: "tag", //显示字段
                valueKey: "tag", //返回Key
                selectKey: "tag", //传参 代表选着字段 不传参返回整个选择对象
                value: "", //选中值
                list: await getTypes('tags')
            }, {
                label: "项目状态",
                index: null,
                showName: "value", //显示字段
                valueKey: "status", //返回Key
                selectKey: "value", //传参 代表选着字段 不传参返回整个选择对象
                value: "", //选中值
                list: [{
                    value: "跟进中"
                }, {
                    value: "已成交"
                }, {
                    value: "已失败"
                }, {
                    value: "已结案"
                }]
            }]
            this.setData({
                filtratelist
            })
        },
        handleFilter({
            detail
        }) {
            if (!detail.name) return;
            let content = this.data.content,
                organization = this.data.organization;
            if (detail.name == "reset") {
                organization.setData({
                    isleave: 1
                })
                organization.initDepAndUser()
                content.type = 0;
                content.dataid = this.data.userid;
                content.where = {
                    "condition": content.where.condition || '',
                    "tag": "",
                    "status": "",
                    "projecttype": "",
                    "stagename": "",
                    "tradefield": "",
                    "begindate": "",
                    "enddate": ""
                }
            } else if (detail.name == "confirm") {
                const res = organization.data.result
                content.type = res.sa_saleareaid ? 1 : 0;
                content.dataid = res.sa_saleareaid || res.userid;
                content.where = {
                    "condition": content.where.condition || '',
                    "tag": detail.tag || '',
                    "status": detail.status || '',
                    "projecttype": detail.projecttype || '',
                    "stagename": detail.stagename || '',
                    "tradefield": detail.tradefield || '',
                    "begindate": detail.startdate || '',
                    "enddate": detail.enddate || ''
                }
            }
            this.setData({
                content
            });
            this.getList(true);
        },
        getList(init = false) {
            _Http.init(this.data.content, init).then(content => {
                _Http.basic({
                    "id": 20230715111604,
                    content
                }).then(res => {
                    console.log("项目画像", res)
                    this.selectComponent('#ListBox').automaticSetHei();
                    this.selectComponent('#ListBox').RefreshToComplete();
                    if (res.code != '1') return wx.showToast({
                        title: res.data,
                        icon: "none"
                    })
                    res.data = res.data.map(v => {
                        v.signamount_due = CNY(v.signamount_due)
                        return v
                    })
                    this.setData({
                        list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
                        "content.pageNumber": res.pageNumber + 1,
                        "content.pageSize": res.pageSize,
                        "content.pageTotal": res.pageTotal,
                        "total": res.total,
                    })
                })
            })
        },
        changeSearchShow() {
            this.setData({
                searchShow: !this.data.searchShow
            })
            setTimeout(() => {
                this.selectComponent('#ListBox').automaticSetHei();
                if (this.data.searchShow) {
                    if (this.selectComponent("#Yl_Filtrate1").data.show) return
                    this.setData({
                        focus: true
                    })
                } else {
                    this.setData({
                        focus: false
                    })
                }
            }, 350)
        },
        openFiltrate() {
            console.log("打开筛选")
        },
        onSearch() {
            this.data.content.where.condition = this.data.searchValue;
            this.getList(true);
        },
        onChange(event) {
            this.setData({
                searchValue: event.detail
            })
        },
        onClear() {
            this.setData({
                searchValue: "",
                "content.where.condition": ""
            })
            this.getList(true);
        },
    }
})