import {
    ApiModel
} from "../../utils/api"
const _Http = new ApiModel();
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        /* 标题 */
        title: {
            type: String,
        },
        /* 选中项 */
        pitchOnItem: {
            type: String,
            value: ""
        },
        /* 回调 */
        optionChange: {
            type: Function
        }
    },
    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            _Http.basic({
                "accesstoken": wx.getStorageSync('userData').token,
                "classname": "enterprise.system.supplyanddemand",
                "method": "query_typeselectList",
                "content": {}
            }).then(res => {
                console.log(res)
                if (res.msg != "成功") return;
                let dataList = []
                for (let i = 0; i < res.data.length; i++) {
                    let checked = false;
                    if (res.data[i] == this.data.pitchOnItem) checked = true;
                    dataList.push({
                        value: res.data[i].ftype,
                        index: i,
                        checked: checked
                    })
                }
                this.setData({
                    dataList
                })
            })
        },
    },

    /**
     * 组件的初始数据
     */
    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
            })
        }
    }
})