// components/input/index.js
const upload_image = require("../../utils/upload/upload_image")
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        value: '',
        // 标题
        title: {
            type: String,
            value: ''
        },
        // 注释
        placeholder: {
            type: String,
            value: '请输入'
        },
        // 类型
        type: {
            type: String,
            value: 'text'
        },
        // input类型
        inputType: {
            type: String,
            value: 'text'
        },
        // 是否必填
        isRequired: {
            type: Boolean,
            value: false
        },
        // 是否禁用
        disabled: {
            type: Boolean,
            value: false
        },
        maxLength: {
            type: Number,
            value: 200
        },

        // 遍历数组
        listErgodic: {
            type: Array,
            value: function () {
                return []
            }
        },
        // key
        key1: {
            type: String,
            value: ''
        },
        // name
        name1: {
            type: String,
            value: ''
        },
        // 计数器最大值
        stepperMax: {
            type: Number,
            value: 10000000,
        },
        // 日期-开始
        startDate: {
            type: String,
            value: ''
        },
        // 日期-结束
        endDate: {
            type: String,
            value: ''
        },
        // 时间-开始
        startTime: {
            type: String,
            value: '',
        },
        // 时间-结束
        endTime: {
            type: String,
            value: '',
        },
    },

    /**
     * 组件的初始数据
     */
    data: {

    },
    lifetimes: {
        attached: function () {},
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        getValue(e) {
            // console.log(e.detail);
            let val = ''
            switch (e.currentTarget.dataset.type) {
                case '1':
                    val = e.detail
                    this.setData({
                        value: e.detail
                    })
                    break;
                default:
                    val = e.detail.value
                    this.setData({
                        value: e.detail.value
                    })
                    break;
            }
            this.triggerEvent('change', val)
        },
        //选择照片
        ChooseImage(e) {
            let that = this
            wx.chooseMedia({
                count: 1,
                mediaType: ['image'],
                sourceType: ['album', 'camera'],
                success(res) {
                    var image = {
                        url: res.tempFiles[0]
                    }
                    wx.showLoading({
                        title: '上传中~',
                    })
                    console.log(image);
                    upload_image(image).then((img) => {
                        wx.hideLoading()
                        that.triggerEvent('change', img.url)
                    }).catch(() => {
                        wx.hideLoading()
                    })
                }
            })
        },
        // 删除照片
        clearImg(e) {
            this.triggerEvent('clearImg', true)
        },
    }
})