<template>
  <w-dialog ref="dialogRef" width="600px" height="400px" :title="$t('io.sc.engine.mv.executorDialog.title')" :can-maximize="false" :maximized="false">
    <div class="px-10">
      <w-form
        ref="formRef"
        :cols-num="1"
        :cols-y-gap="10"
        :fields="[
          { name: 'rateStartDateFrom', label: $t('io.sc.engine.mv.executorDialog.form.entity.rateStartDateFrom'), type: 'text' },
          { name: 'rateStartDateTo', label: $t('io.sc.engine.mv.executorDialog.form.entity.rateStartDateTo'), type: 'text' },
          { name: 'performPeriod', label: $t('io.sc.engine.mv.executorDialog.form.entity.performPeriod'), type: 'text', defaultValue: '18' },
          {
            name: 'binomialSignificanceLevel',
            label: $t('io.sc.engine.mv.executorDialog.form.entity.binomialSignificanceLevel'),
            type: 'select',
            options: binomialSignificanceLevelOptionsRef,
            defaultValue: 0.01,
          },
          {
            name: 'chiSquareSignificanceLevel',
            label: $t('io.sc.engine.mv.executorDialog.form.entity.chiSquareSignificanceLevel'),
            type: 'select',
            options: chiSquareSignificanceLevelOptionsRef,
            defaultValue: 0.01,
          },
        ]"
      >
      </w-form>
      <div style="height: 20px"></div>
      <div class="row justify-center">
        <q-btn :label="$t('io.sc.engine.mv.executorDialog.form.action.execute')" color="primary" @click="execute"></q-btn>
      </div>
    </div>
  </w-dialog>
</template>
<script setup lang="ts">
import { ref, nextTick } from 'vue';
import { axios, Environment } from 'platform-core';

const emit = defineEmits(['afterStartExecute']);

const dialogRef = ref();
const formRef = ref();
const binomialSignificanceLevelOptionsRef = ref([]);
const chiSquareSignificanceLevelOptionsRef = ref([]);

const open = () => {
  dialogRef.value.show();
  nextTick(() => {
    axios.get(Environment.apiContextPath('/api/mv/configure/binomial/significanceLevels')).then((response) => {
      binomialSignificanceLevelOptionsRef.value.splice(0, binomialSignificanceLevelOptionsRef.value.length);
      for (let item of response.data) {
        binomialSignificanceLevelOptionsRef.value.push({ label: item, value: item });
      }
    });
    axios.get(Environment.apiContextPath('/api/mv/configure/chiSquare/significanceLevels')).then((response) => {
      chiSquareSignificanceLevelOptionsRef.value.splice(0, chiSquareSignificanceLevelOptionsRef.value.length);
      for (let item of response.data) {
        chiSquareSignificanceLevelOptionsRef.value.push({ label: item, value: item });
      }
    });
  });
};

const close = () => {
  dialogRef.value.hide();
};

const execute = () => {
  axios
    .post(Environment.apiContextPath('/api/mv/execute'), {
      executeMode: 'MANUAL',
      rateStartDateFrom: formRef.value.getFieldValue('rateStartDateFrom'),
      rateStartDateTo: formRef.value.getFieldValue('rateStartDateTo'),
      performPeriod: formRef.value.getFieldValue('performPeriod'),
      binomialSignificanceLevel: formRef.value.getFieldValue('binomialSignificanceLevel'),
      chiSquareSignificanceLevel: formRef.value.getFieldValue('chiSquareSignificanceLevel'),
    })
    .then(() => {
      emit('afterStartExecute');
      close();
    });
};

defineExpose({
  open,
  close,
});
</script>
