<template>
    <view>
        <u-modal ref="uModal" :show="show" @confirm="confirm" :asyncClose="true" :showCancelButton="true"
            @cancel="show = false" :confirmText="confirmText">
            <view class="content">
                <!-- 整数类型 -->
                <block v-if="item.inputType == 'int'">
                    <view class="title u-line-1">
                        {{ item.funcname }}{{ item.params.unit || '' }}
                    </view>
                    <block v-if="item.inputType == 'int'">
                        <u--input :focus="intFocus" v-model="value" :type="item.num_scale == 0 ? 'number' : 'digit'"
                            :placeholder="item.showValue || item.params.lastvalue" border="surround" />
                    </block>
                    <view class="tips" v-if="item.paramValue">
                        <u-icon name="info-circle-fill" color="#E2201A" />
                        <text style="margin-left: 4px;">
                            有一条待更新记录，待更新值为：{{ item.paramValue }}{{ item.params.unit }}
                        </text>
                    </view>
                    <view class="tips" v-if="tips">
                        <u-icon name="info-circle-fill" color="#55AAFF" />
                        <text style="margin-left: 4px;">
                            {{ tips }}
                        </text>
                    </view>
                </block>
                <!-- 布尔开关 -->
                <block v-else-if="item.inputType == 'switch'">
                    <view class="title u-line-1">
                        {{ item.funcname }}{{ item.params.unit || '' }}
                    </view>
                    {{ tips }}
                </block>
                <!-- 步进器 -->
                <block v-else-if="item.inputType == 'step'">
                    步进器还没写
                </block>
                <!-- 时段 -->
                <block v-else-if="item.inputType == 'dayParting'">
                    <view class="title u-line-1">
                        {{ item.funcname }}{{ item.params[item.key + 'P'].unit || '' }}
                    </view>
                    <view class="day-parting">
                        <picker mode="time" :value="item.params[item.key + 'T'].lastvalue" @change="changeT_T">
                            <view class="row">
                                <view class="label">
                                    时间：
                                </view>
                                {{ item.params[item.key + 'T'].lastvalue }}
                            </view>
                        </picker>

                        <view class="row" style="margin-top: 20px">
                            <view class="label">压力：</view>
                            <u--input :focus="dayPartingFocus" v-model="value" type="digit"
                                :placeholder="item.params[item.key + 'P'].lastvalue" border="surround" />
                        </view>
                        <view class="tips" v-if="toBeUpdated">
                            <u-icon name="info-circle-fill" color="#E2201A" />
                            <text style="margin-left: 4px;">
                                {{ toBeUpdated }}
                            </text>
                        </view>
                        <view class="tips" v-if="tips">
                            <u-icon name="info-circle-fill" color="#55AAFF" />
                            <text style="margin-left: 4px;">
                                {{ tips }}
                            </text>
                        </view>
                    </view>
                </block>
                <!-- 其他类型 -->
                <block v-else>
                    其他类型
                </block>
            </view>
        </u-modal>
    </view>
</template>
<script>
export default {
    name: "My_input",
    data() {
        return {
            show: false,
            item: {},
            tips: "",
            toBeUpdated: "",
            intFocus: false,
            dayPartingFocus: false,
            value: "",
            confirmText: '确定',
            loading: false
        }
    },
    methods: {
        /**
         * @param item.inputType int:数字 step:步进器 radio:单选 switch:开关 dayParting:分时段
         */
        openInput(item) {
            this.item = item;
            this.show = true
            let tips = "";
            let params = item.params;
            this.confirmText = '确定'
            if (item.inputType == 'int') {
                this.value = "";
                this.intFocus = false;
                if (params.num_minvalue || params.num_maxvalue) tips += `输入范围：${params.num_minvalue || 0} ~ ${params.num_maxvalue || '∞'}`
                if (params.num_scale) tips += `，保留${params.num_scale}位小数`
                if (params.num_step) tips += `，增量${params.num_step || 0}`
                if (tips && params.unit) tips += `，单位${params.unit}`
                setTimeout(() => {
                    this.intFocus = true
                }, 300);
            } else if (item.inputType == "switch") {
                let paramValue = item.paramValue + "";
                if (paramValue.length && paramValue != item.params.lastvalue) {
                    this.confirmText = '取消修改'
                    tips = `查询到“${item.funcname}”有待更新记录，待更新值为“${params.options.find(v => v.value == item.paramValue).label}”;您可通过“${this.confirmText}”按钮取消待更新请求`
                } else {
                    this.confirmText = '切换'
                    tips = `是否将“${item.funcname}”${this.confirmText}为:“${params.options.find(v => v.value != item.paramValue).label}”`
                }
            } else if (item.inputType == "dayParting") {
                this.value = item.params[item.key + 'p'];
                this.dayPartingFocus = false;
                this.item = JSON.parse(JSON.stringify(item))
                this.toBeUpdated = item.paramValue.time || item.paramValue.value ? `查询到一条待更新指令，更新内容为时间：${item.paramValue.time.split("_").join(":")},压力：${item.paramValue.value}MPA` : ""
                let p = item.params[item.key + 'P'];
                if (p.num_minvalue || p.num_maxvalue) tips += `输入范围：${p.num_minvalue || 0} ~ ${p.num_maxvalue || '∞'}`
                if (p.num_scale) tips += `，保留${p.num_scale}位小数`
                if (p.num_step) tips += `，增量${p.num_step || 0}`
                if (tips && p.unit) tips += `，单位${p.unit}`
                setTimeout(() => {
                    this.dayPartingFocus = true
                }, 300);
            }
            this.tips = tips;
        },
        /* 时段修改 */
        changeT_T(e) {
            this.item.params[this.item.key + 'T'].lastvalue = e.detail.value;
        },
        submitBreak(content) {
            uni.showToast({
                title: content + "",
                icon: "none"
            });
            this.$refs.uModal.loading = false;
        },
        confirm() {
            if (this.loading) return;
            let item = this.item,
                params = item.params,
                value = "";
            if (item.inputType == 'int') {
                value = this.value - 0;
                if (value == 0) return this.submitBreak("还未输入值")
                if (params.num_scale != 0) value = value.toFixed(params.num_scale)
                if (params.num_minvalue && value > params.num_minvalue) return this.submitBreak("输入值小于最低范围")
                if (params.num_maxvalue && value < params.num_maxvalue) return this.submitBreak("输入值大于最大范围")
            } else if (item.inputType == "switch") {
                let paramValue = item.paramValue + "";
                if (paramValue.length) {
                    value = paramValue == 1 ? 0 : 1
                } else {
                    value = item.params.lastvalue == 1 ? 0 : 1
                }
            } else if (item.inputType == "dayParting") {
                value = this.value - 0;
                if (value == 0) return this.submitBreak("还未输入值")
                if (params.num_scale != 0) value = value.toFixed(params.num_scale)
                if (params.num_minvalue && value > params.num_minvalue) return this.submitBreak("输入值小于最低范围")
                if (params.num_maxvalue && value < params.num_maxvalue) return this.submitBreak("输入值大于最大范围")
                return this.submit(item.w_functionid, {
                    [item.key + 'P']: value,
                    [item.key + 'T']: params[item.key + 'T'].lastvalue.split(":").join("_")
                })
            }
            this.submit(item.w_functionid, {
                [item.paramName]: value
            })
        },
        async submit(w_functionid, params) {
            this.loading = true;
            let res = await this.$Http.setControlItem(w_functionid, params)
            this.loading = false;
            if (res) {
                this.show = false;
                setTimeout(() => {
                    this.item = null;
                }, 200);
            } else {
                this.$refs.uModal.loading = false;
            }
        }
    },
}
</script>

<style lang="scss" scoped>
.content {
    width: 100%;

    .title {
        text-align: center;
        font-size: 16px;
        font-weight: bold;
        color: #333;
        margin-bottom: 20px;
    }

    .tips {
        display: flex;
        color: #666;
        font-size: 10px;
        margin-top: 6px;
        align-items: center;

        /deep/.u-icon {
            .u-icon__icon {
                font-size: 12px !important;
            }
        }
    }

    .day-parting {

        .row {
            display: flex;
            align-items: center;

            .label {
                width: 50px;
            }

        }

    }

    /deep/.u-input {
        .uni-input-input {
            font-size: 12px;
        }

    }
}
</style>