<template>
  <div class="calculator-container">
    <v-row justify="center">
      <!-- 参数输入区域 -->
      <v-col
        cols="12"
        lg="5"
        md="6"
      >
        <v-card
          class="pa-6 parameter-card"
          elevation="8"
          rounded="xl"
        >
          <v-card-title class="text-h5 mb-6 d-flex align-center">
            <v-icon
              class="mr-3"
              color="primary"
              icon="mdi-tune"
              size="large"
            />
            {{ $t('parameters') }}
          </v-card-title>

          <v-row>
            <v-col cols="12">
              <param-input-field
                v-model="paperCoreDiameter"
                :label="$t('paperCoreDiameter')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="paperRollWallThickness"
                :label="$t('paperRollWallThickness')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="paperRollLength"
                :label="$t('paperRollLength')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="paperDensity"
                :label="$t('paperDensity')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="innerPaperWidth"
                :label="$t('innerPaperWidth')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="workFrequency"
                :label="$t('workFrequency')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="workTime"
                :label="$t('workTime')"
              />
            </v-col>
            <v-col cols="12">
              <param-input-field
                v-model="workEfficiency"
                :label="$t('workEfficiency')"
              />
            </v-col>
          </v-row>

          <v-divider class="my-6" />

          <v-row>
            <v-btn
              block
              color="warning"
              prepend-icon="mdi-refresh"
              size="large"
              variant="outlined"
              @click="resetParameters"
            >
              {{ $t('reset') }}
            </v-btn>
          </v-row>
        </v-card>
      </v-col>

      <!-- 计算结果区域 -->
      <v-col
        cols="12"
        lg="5"
        md="6"
      >
        <v-card
          class="pa-6 result-card"
          elevation="8"
          rounded="xl"
        >
          <v-card-title class="text-h5 mb-6 d-flex align-center">
            <v-icon
              class="mr-3"
              color="primary"
              icon="mdi-calculator"
              size="large"
            />
            {{ $t('results') }}
          </v-card-title>

          <v-row>
            <v-col cols="12">
              <result-card
                :label="$t('feedPaperSpeed')"
                :value="result.feedPaperSpeed"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="$t('outputSpeed')"
                :value="result.outputSpeed"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="$t('productionAmountPerHour')"
                :value="result.productionAmountPerHour"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="$t('productionWeightPerHour')"
                :value="result.productionWeightPerHour"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="$t('productionAmountPerDay')"
                :value="result.productionAmountPerDay"
              />
            </v-col>
            <v-col cols="12">
              <result-card
                :label="$t('productionWeightPerDay')"
                :value="result.productionWeightPerDay"
              />
            </v-col>
          </v-row>
        </v-card>
      </v-col>
    </v-row>
  </div>
</template>

<script setup lang="ts">
  import { computed, ref } from 'vue'
  import { createParam, type Param } from '@/types/param'

  const paperCoreDiameter = ref<Param>(createParam(76.2, 'mm'))
  const paperRollWallThickness = ref<Param>(createParam(10, 'mm'))
  const paperRollLength = ref<Param>(createParam(1000, 'mm'))
  const paperDensity = ref<Param>(createParam(0.76, 'g_per_cm3'))
  const innerPaperWidth = ref<Param>(createParam(105, 'mm'))
  const workFrequency = ref<Param>(createParam(30, 'hz'))
  const workTime = ref<Param>(createParam(8, 'hour'))
  const workEfficiency = ref<Param>(createParam(85, 'percent'))

  const result = computed(() => {
    const paperRollExternalDiameter = paperCoreDiameter.value.value + 2 * paperRollWallThickness.value.value
    const paperRollExternalRadius = paperRollExternalDiameter / 2
    const paperCoreInnerRadius = paperCoreDiameter.value.value / 2

    const feedPaperSpeed = 50 / 1400 * workFrequency.value.value / 30 * (215 * Math.PI)
    const outputSpeed = feedPaperSpeed / (paperRollExternalDiameter * Math.PI) * innerPaperWidth.value.value
    const productionAmountPerHour = outputSpeed / 100 * workEfficiency.value.value / (paperRollLength.value.value / 1000) * 60
    const productionWeightPerHour = productionAmountPerHour * (Math.PI * (Math.pow(paperRollExternalRadius, 2) - Math.pow(paperCoreInnerRadius, 2)) * paperRollLength.value.value * paperDensity.value.value / 1_000_000)
    const productionAmountPerDay = productionAmountPerHour * workTime.value.value
    const productionWeightPerDay = productionWeightPerHour * workTime.value.value

    return {
      feedPaperSpeed: createParam(feedPaperSpeed, 'm_per_min'),
      outputSpeed: createParam(outputSpeed, 'm_per_min'),
      productionAmountPerHour: createParam(productionAmountPerHour, 'pcs_per_hour'),
      productionWeightPerHour: createParam(productionWeightPerHour, 'kg_per_hour'),
      productionAmountPerDay: createParam(productionAmountPerDay, 'pcs_per_day'),
      productionWeightPerDay: createParam(productionWeightPerDay, 'kg_per_day'),
    }
  })

  const resetParameters = () => {
    paperCoreDiameter.value = createParam(76.2, 'mm')
    paperRollWallThickness.value = createParam(10, 'mm')
    paperRollLength.value = createParam(1000, 'mm')
    paperDensity.value = createParam(0.76, 'g_per_cm3')
    innerPaperWidth.value = createParam(105, 'mm')
    workFrequency.value = createParam(30, 'hz')
    workTime.value = createParam(8, 'hour')
    workEfficiency.value = createParam(85, 'percent')
  }

</script>
