<template>
    <view class="math-container">
        <view class="header">
            <u-image width="100%" height="300rpx" :src="mathImgs[name].big" mode="aspectFit" @click="handleBigImage"></u-image>
        </view>
        <view class="main dir-top-wrap cross-center">
            <view class="form">
                <u-form-item label="底宽(cm)" label-width="160">
                    <u-input type="number" v-model="formData.dikuan" placeholder="请输入底宽"/>
                </u-form-item>
                <u-form-item label="变径(cm)" label-width="160">
                    <u-input type="number" v-model="formData.bianjin" placeholder="请输入变径"/>
                </u-form-item>
            </view>
            <view class="btn-group main-left cross-center">
                <u-button :ripple="true" @click="handelCalc" :custom-style="{backgroundColor: $u.color['mainBgColor'],color:'#ffffff',width: '260rpx',marginRight:'30rpx'}">计算</u-button>
                <u-button :ripple="true" @click="handleClear">清空</u-button>
            </view>
        </view>
        <div class="footer">
            <view class="result dir-top-wrap cross-center" v-if="showResult">
                <view v-for="item in rules" v-if="item.value && item.show">
                    <text>{{item.name}}</text>{{item.value}}{{item.unit}}
                </view>
            </view>
            <view class="title">计算图</view>
            <view class="calc-img">
                <text class="result2">{{rules.result2.value?rules.result2.value:'**'}}{{rules.result2.unit}}</text>
                <text class="slice1">{{rules.result1.value?rules.result1.value:'**'}}{{rules.result1.unit}}</text>
                <text class="slice2">{{rules.result1.value?rules.result1.value:'**'}}{{rules.result1.unit}}</text>
                <u-image width="100%" height="300rpx" :src="mathImgs[name].calc" mode="aspectFit"></u-image>
            </view>
            <view class="title">切割图</view>
            <u-image width="100%" height="300rpx" :src="mathImgs[name].slice" mode="aspectFit"></u-image>

        </div>
    </view>
</template>

<script>
    import mathImgs from "@/core/math-imgs"
    // 万能公式
    export default {
        data() {
            return {
                name: 'bianjindanbian',
                mathImgs: mathImgs,
                formData: {
                    dikuan: '',
                    bianjin: '',
                },
                // 用来验证 输入
                rules: {
                    result1: {name:'', value:'',unit:'cm',show: true},
                    result2: {name:'', value:'',unit:'cm',show: true},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                this.formData.dikuan = parseFloat(this.formData.dikuan);
                this.formData.bianjin = parseFloat(this.formData.bianjin);
                let validate = [this.formData.dikuan,this.formData.bianjin]
                if(!this.$util.checkArrayNotNullNumber(validate)){
                    this.$u.toast('至少输入两个参数');
                    return
                }
                if(this.formData.dikuan <= this.formData.bianjin){
                    this.$u.toast('变径不能大于底宽');
                    return
                }
                this.initRules();
                /**
                 1、 结果一 =（底宽-半径）×1.414
                 2、 结果二 = 底宽-半径
                 */
                this.rules.result1.value = (this.formData.dikuan - this.formData.bianjin) * 1.414
                this.rules.result2.value = this.formData.dikuan - this.formData.bianjin
                this.roundRules();
                this.$u.toast("请参考计算示意图")
            },
            roundRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = this.$util.round(this.rules[itemKey].value,2);
                }
            },
            initRules(){
                for (const itemKey in this.rules) {
                    this.rules[itemKey].value = "";
                }
            },
            handleClear() {
                this.initRules();
                this.formData = {
                    dikuan: '',
                    bianjin: '',
                };
            }
        },
        computed:{
            showResult(){
                let validate = [];
                for (const itemKey in this.rules) {
                    validate.push(this.rules[itemKey].value);
                }
                return this.$util.checkArrayNotNullNumber(validate,1)
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import "@/static/css/math.scss";
    .result2{
        top: 80rpx;
    }
    .slice1{
        top: 20rpx;
        left: 360rpx;
    }
    .slice2{
        top: 260rpx;
        left: 330rpx;
    }
</style>
