import {
    ApiModel
} from "../../utils/api"
const _Http = new ApiModel();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        /* 标题 */
        title: {
            type: String,
        },
        /* 选中项 */
        pitchOnItem: {
            type: String,
            value: ""
        },
        /* 回调 */
        optionChange: {
            type: Function
        },
        /* 类型，默认供需分类 */
        type: {
            type: String,
            value: 'mr'
        }
    },
    lifetimes: {
        ready: function () {
            // 在组件实例进入页面节点树时执行
            //选择合作方式
            if (this.data.type == 'annunciate') {
                const res = {
                    msg: "成功",
                    data: [{
                        ftype: "合作商通告"
                    }, {
                        ftype: "团队通告"
                    }]
                };
                return this.returnClassify(res)
            };
            //默认供需分类
            let data = {
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "enterprise.system.supplyanddemand",
                "method": "query_typeselectList",
                "content": {}
            };
            if (this.data.type == 'product') data = {
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "enterprise.system.prodclass",
                "method": "query_typeselectList",
                "content": {}
            };
            _Http.basic(data).then(res => {
                console.log(res)
                this.returnClassify(res);
            })
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        dataList: [], //类目列表
        pitchOn: '', //选中
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /* 确定 */
        confirm() {
            this.triggerEvent("optionChange", this.data.pitchOn)
        },
        /* 多选框返回数值 */
        radioChange(e) {
            this.setData({
                pitchOn: e.detail.value
            })
        },
        /* 添加背景色 */
        pitchOn(e) {
            let dataList = this.data.dataList;
            for (let i = 0; i < dataList.length; i++) {
                dataList[i].checked = false
            }
            dataList[e.currentTarget.dataset.index].checked = !dataList[e.currentTarget.dataset.index].checked
            this.setData({
                dataList
            })
        },
        /* 返回分类 */
        returnClassify(res) {
            if (res.msg != "成功") return wx.showToast({
                title: '数据加载失败,请重新进入页面',
                icon: "none",
                duration: 5000
            })
            let dataList = []
            for (let i = 0; i < res.data.length; i++) {
                let checked = false,
                    value = "";
                if (this.data.type == 'product') {
                    if (res.data[i] == this.data.pitchOnItem) checked = true;
                    value = res.data[i];
                } else {
                    if (res.data[i].ftype == this.data.pitchOnItem) checked = true;
                    value = res.data[i].ftype;
                }
                dataList.push({
                    value,
                    index: i,
                    checked: checked
                })
            }
            this.setData({
                dataList
            })
        }
    }
})