<template>
  <w-grid
    ref="treeGridRef"
    :title="$t('engine.st.indepFactorDefine.grid.title')"
    dense
    :checkbox-selection="false"
    :tree="true"
    :tree-icon="
      (row) => {
        if (row.type === 'FOLDER') {
          return { name: 'folder', color: 'amber' };
        } else if (row.type === 'ORIGINAL') {
          return { name: 'bi-file-earmark-text' };
        } else if (row.type === 'EXTEND_ARITHMETIC') {
          return { name: 'bi-file-earmark-text', color: 'green' };
        } else if (row.type === 'EXTEND_DELAY') {
          return { name: 'bi-file-earmark-text', color: 'blue' };
        } else if (row.type === 'EXTEND_DIFFERENTIAL') {
          return { name: 'bi-file-earmark-text', color: 'amber' };
        } else {
          return { name: 'bi-file-earmark-text' };
        }
      }
    "
    :data-url="Environment.apiContextPath('/api/st/scenarioFactorDefine')"
    :pageable="false"
    :sort-by="['order']"
    :toolbar-actions="[
      'refresh',
      'separator',
      'expand',
      'separator',
      {
        name: 'synchronize',
        label: $t('synchronize'),
        icon: 'bi-repeat',
        click: () => {},
      },
      'separator',
      'view',
    ]"
    :columns="[
      { width: '100%', name: 'name', label: $t('name') },
      { width: 150, name: 'code', label: $t('code') },
      {
        width: 120,
        name: 'type',
        label: $t('type'),
        format: (value, row) => {
          if (value === 'FOLDER') {
            return '';
          }
          return Formater.enum(Enums.FactorDefineType)(value);
        },
      },
      { width: 60, name: 'period', label: $t('period'), format: Formater.enum(Enums.Period) },
      { width: 80, name: 'valueType', label: $t('valueType'), format: Formater.enum(Enums.ValueType) },
    ]"
    :editor="{
      dialog: {
        width: '500px',
      },
      form: {
        colsNum: 1,
        fields: [
          {
            name: 'type',
            label: $t('type'),
            type: 'w-select',
            requiredIf: true,
            options: Options.enum(Enums.FactorDefineType),
            onChange: (arg) => {
              findAllWithoutById(arg.form.getFieldValue('id'));
            },
          },
          {
            name: 'code',
            label: $t('code'),
            type: 'w-text',
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') !== 'FOLDER';
            },
          },
          { name: 'name', label: $t('name'), type: 'w-text', requiredIf: true },
          { name: 'description', label: $t('description'), type: 'w-text' },
          { name: 'order', label: $t('order'), type: 'w-number' },
          {
            name: 'period',
            label: $t('period'),
            requiredIf: true,
            type: 'w-select',
            options: Options.enum(Enums.Period),
            showIf: (arg) => {
              return arg.form.getFieldValue('type') !== 'FOLDER';
            },
          },
          {
            name: 'valueType',
            label: $t('valueType'),
            requiredIf: true,
            type: 'w-select',
            options: Options.enum(Enums.ValueType),
            showIf: (arg) => {
              return arg.form.getFieldValue('type') !== 'FOLDER';
            },
          },

          {
            name: 'formula',
            label: $t('engine.st.indepFactorDefine.grid.entity.formula'),
            type: 'w-code-mirror',
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') === 'EXTEND_ARITHMETIC';
            },
          },
          {
            name: 'delayFactorDefine',
            label: $t('engine.st.indepFactorDefine.grid.entity.delayFactorDefine'),
            type: 'w-select',
            options: factorDefineOptionsRef,
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') === 'EXTEND_DELAY';
            },
          },
          {
            name: 'delayIssue',
            label: $t('engine.st.indepFactorDefine.grid.entity.delayIssue'),
            type: 'w-number',
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') === 'EXTEND_DELAY';
            },
          },
          {
            name: 'differentialFactorDefine',
            label: $t('engine.st.indepFactorDefine.grid.entity.differentialFactorDefine'),
            type: 'w-select',
            options: factorDefineOptionsRef,
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') === 'EXTEND_DIFFERENTIAL';
            },
          },
          {
            name: 'differentialDegree',
            label: $t('engine.st.indepFactorDefine.grid.entity.differentialDegree'),
            type: 'w-number',
            requiredIf: true,
            showIf: (arg) => {
              return arg.form.getFieldValue('type') === 'EXTEND_DIFFERENTIAL';
            },
          },
        ],
      },
    }"
    :viewer="{
      panel: {
        columnNum: 1,
        fields: [
          { name: 'id', label: $t('id') },
          { name: 'type', label: $t('type') },
          { name: 'period', label: $t('period') },
          { name: 'code', label: $t('code') },
          { name: 'name', label: $t('name') },
          { name: 'description', label: $t('description') },
          { name: 'order', label: $t('order') },
          { name: 'valueType', label: $t('valueType') },

          { name: 'formula', label: $t('engine.st.indepFactorDefine.grid.entity.formula') },
          { name: 'delayFactorDefine', label: $t('engine.st.indepFactorDefine.grid.entity.delayFactorDefine') },
          { name: 'delayIssue', label: $t('engine.st.indepFactorDefine.grid.entity.delayIssue') },
          { name: 'differentialFactorDefine', label: $t('engine.st.indepFactorDefine.grid.entity.differentialFactorDefine') },
          { name: 'differentialDegree', label: $t('engine.st.indepFactorDefine.grid.entity.differentialDegree') },

          { name: 'dataComeFrom', label: $t('dataComeFrom') },
          { name: 'creator', label: $t('creator') },
          { name: 'createDate', label: $t('createDate') },
          { name: 'lastModifier', label: $t('lastModifier') },
          { name: 'lastModifyDate', label: $t('lastModifyDate'), format: Formater.none() },
          { name: 'corporationCode', label: $t('corporationCode') },
        ],
      },
    }"
    @after-editor-open="
      (selected) => {
        if (selected?.type === 'EXTEND_DELAY' || selected?.type === 'EXTEND_DIFFERENTIAL') {
          findAllWithoutById(selected.id);
        }
      }
    "
  ></w-grid>
</template>
<script setup lang="ts">
import 'tailwindcss/utilities.css';
import { ref } from 'vue';
import { Environment, Formater, Options, EnumTools, axios } from 'platform-core';

const treeGridRef = ref();
const factorDefineOptionsRef = ref([]);
const Enums = await EnumTools.fetch(['io.sc.engine.st.enums.FactorDefineType', 'io.sc.engine.st.enums.Period', 'io.sc.engine.st.enums.ValueType']);
const findAllWithoutById = (id) => {
  axios.get(Environment.apiContextPath('/api/st/indepFactorDefine/findAllWithoutById?defineId=' + (id ? id : ''))).then((response) => {
    const defines = response?.data;
    if (defines) {
      const options = [];
      for (const define of defines) {
        if (define.type !== 'FOLDER') {
          options.push({ value: define.id, label: define.name });
        }
      }
      factorDefineOptionsRef.value = options;
    }
  });
};
</script>
