<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="digit" v-model="formData.biangao"  :placeholder="biangaoInput.placeholder" :disabled="biangaoInput.disabled"/>
                </u-form-item>
                <u-form-item label="角度(°)" label-width="160">
                    <u-input type="digit" v-model="formData.angle" :placeholder="angleInput.placeholder" :disabled="angleInput.disabled"/>
                </u-form-item>
                <u-form-item label="切口(cm)" label-width="160">
                    <u-input type="digit" v-model="formData.slice" :placeholder="sliceInput.placeholder" :disabled="sliceInput.disabled"/>
                </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>{{$util.round(item.value,2)}}{{item.unit}}
                    <text v-if="item.isHalf">{{$util.round(item.value/2,2)}}{{item.unit}}</text>
                </view>
            </view>
            <view class="title">计算图</view>
            <view class="calc-img">
                <text class="slice1">{{rules.slice.value?$util.round(rules.slice.value/2,2):'**'}}{{rules.slice.unit}}</text>
                <text class="slice2">{{rules.slice.value?$util.round(rules.slice.value/2,2):'**'}}{{rules.slice.unit}}</text>
                <text class="slice3 bottom">{{rules.slice.value?rules.slice.value:'**'}}{{rules.slice.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: 'slice',
                mathImgs: mathImgs,
                formData: {
                    biangao: '',
                    angle: '',
                    slice: '',
                },
                // 用来验证 输入
                rules: {
                    biangao: {name:'边高', value:'',unit:'cm',show: true},
                    angle:{name:'角度', value:'',unit:'°',show: true},
                    slice:{name:'切口', value:'',unit:'cm',show: true,isHalf: true},
                }
            }
        },
        methods: {
            handleBigImage(){
                uni.previewImage({
                    urls: [mathImgs[this.name].big],
                });
            },
            handelCalc(){
                if( this.formData.angle &&  this.formData.angle > 90){
                    this.$u.toast('角度不能大于90');
                    return
                }
                this.initRules();
                /*1、 已知边高、角度：切口=边高×tan（角度°÷2）×2*/
                if(this.formData.biangao && this.formData.angle){
                    this.rules.slice.value = this.formData.biangao * this.$util.tan(this.formData.angle / 2) * 2
                }
                /*2、 已知边高、切口：tan（角度°÷2）=切口÷（边高×2）*/
                if(this.formData.biangao && this.formData.slice){
                    this.rules.angle.value = this.$util.atan(this.formData.slice / (this.formData.biangao * 2)) * 2
                }
                /*3、 已知角度、切口：边高=切口÷{tan（角度°÷2）×2}×2*/
                if(this.formData.angle && this.formData.slice){
                    this.rules.biangao.value = this.formData.slice * this.$util.tan(this.formData.angle / 2) * 2
                }
                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 = "";
                }
            },
            getInputItem(item){
                let validate = [];
                for (const itemKey in this.rules) {
                    if(item !== itemKey && typeof this.formData[itemKey] !== "undefined")
                        validate.push(this.formData[itemKey]);
                }
                let placeholder = `请输入${this.rules[item].name}`;
                let disabled = false;
                if(this.$util.checkArrayNotNullNumber(validate)){
                    disabled = true;
                    placeholder = '无需输入';
                }
                return {placeholder: placeholder,disabled:disabled};

            },
            handleClear() {
                this.initRules();
                this.formData = {
                    biangao: '',
                    angle: '',
                    slice: '',
                };
            }
        },
        computed:{
            angleInput(){
                return this.getInputItem('angle')
            },
            sliceInput(){
                return this.getInputItem('slice')
            },
            biangaoInput(){
                return this.getInputItem('biangao')
            },
            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";
    .slice1,.slice2{
        top: -15rpx;
    }
    .slice1{
        left: 270rpx;
    }
    .slice2{
        left: 360rpx;
    }
    .slice3{
        top: 280rpx !important;
        left: 320rpx;
    }
</style>
