<template>
  <div class="volumnEditBody">
    <el-form label-width="auto" label-position="left" :model="form" :rules="rules">
      <el-form-item :label="$t('COMMON_NAME')" prop="namestr">
        <el-tooltip
          popper-class="no-wrap-popover"
          placement="bottom"
          :teleported="false"
          trigger-keys
        >
          <template #content>
            <span id="volumeNameInputTip">
              {{ traduction(
                  'STORE_ASSIC15', 
                  raidType === RaidType.ARIES ? {param: 20} :
                  (isPMCCard ? {param: 64} :
                  {param: 15}) 
                )
              }}
            </span>
          </template>
          <el-input
            id="volumnName"
            v-model.trim="form.namestr"
            class="inputText"
            :maxlength="
              raidType === RaidType.ARIES ? 20 :
              (isPMCCard ? 64 :
              15) 
            "
            @input="editChange('namestr')"
          ></el-input>
        </el-tooltip>
      </el-form-item>
      <el-form-item v-if="!isPMCCard" :label="$t('STORE_DEFAULT_READ')">
        <el-select
          id="readPolicy"
          v-model="form.defaultReadPolicy.value"
          class="tiSelect"
          :disabled="raidType === RaidType.ARIES ? hbaMode : form.defaultReadPolicy.disable"
          @change="editChange('defaultReadPolicy')"
        >
          <el-option
            v-for="item in form.defaultReadPolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="!isPMCCard" :label="$t('STORE_DEFAULT_WRITE')">
        <el-select
          id="writePolicy"
          v-model="form.defaultWritePolicy.value"
          class="tiSelect"
          :disabled="raidType === RaidType.ARIES ? hbaMode : form.defaultWritePolicy.disable"
          @change="editChange('defaultWritePolicy')"
        >
          <el-option
            v-for="item in form.defaultWritePolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="raidType !== RaidType.ARIES && !isPMCCard" :label="$t('STORE_DEFAULT_IO')">
        <el-select
          id="ioPolicy"
          v-model="form.ioPolicy.value"
          class="tiSelect"
          :disabled="form.ioPolicy.disable"
          label-key="label"
          @change="editChange('ioPolicy')"
        >
          <el-option
            v-for="item in form.ioPolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="raidType !== RaidType.ARIES && !isPMCCard" :label="$t('STORE_BGISTATE')">
        <el-select
          id="bgiState"
          v-model="form.bGIEnabled.value"
          class="tiSelect"
          :disabled="form.bGIEnabled.disable"
          @change="editChange('bGIEnabled')"
        >
          <el-option
            v-for="item in form.bGIEnabled.options"
            :key="item.id"
            :label="traduction(item.label)"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="raidType !== RaidType.ARIES && !isPMCCard" :label="$t('STORE_ACCESS_POLICY')">
        <el-select
          id="accessPolicy"
          v-model="form.accessPolicy.value"
          class="tiSelect"
          :disabled="form.accessPolicy.disable"
          label-key="label"
          @change="editChange('accessPolicy')"
        >
          <el-option
            v-for="item in form.accessPolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="raidType !== RaidType.ARIES && !isPMCCard" :label="$t('STORE_CACHE_STATUS')">
        <el-select
          id="driveCachePolicy"
          v-model="form.driveCachePolicy.value"
          class="tiSelect"
          :disabled="form.driveCachePolicy.disable"
          label-key="label"
          @change="editChange('driveCachePolicy')"
        >
          <el-option
            v-for="item in form.driveCachePolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="raidType === RaidType.ARIES" :label="$t('STORE_BOOT_DISK')">
        <div id="isBootDisk" class="radioBtn">
          <el-radio-group
            v-model="form.bootEnabled.value"
            @change="editChange('bootEnabled')"
          >
            <el-radio v-for="item in form.bootEnabled.options" :key="item.id" :label="item.id">
              {{ traduction(item.label) }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
      <el-form-item v-else-if="isPMCCard && !isCache" :label="$t('STORE_BOOT_DISK')">
        <el-select
          id="bootPriority"
          v-model="form.bootPriority.value"
          class="tiSelect"
          @change="editChange('bootPriority')"
        >
          <el-option
            v-for="item in volumeStaticModel.pmcBootPriority"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-else :label="$t('STORE_BOOT_DISK')">
        <div id="isBootDisk" class="radioBtn">
          <el-radio-group
            v-model="form.bootEnabled.value"
            :disabled="form.bootEnabled.disable || isBootDiskDisabled"
            @change="editChange('bootEnabled')"
          >
            <el-radio v-for="item in form.bootEnabled.options" :key="item.id" :label="item.id">
              {{ traduction(item.label) }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
      <el-form-item v-if="form.isSSCD && raidType !== RaidType.ARIES && !isPMCCard" :label="$t('STORE_SSCD_FEATURES')">
        <div id="sscdFuncSelect" class="radioBtn">
          <el-radio-group
            v-model="form.sSDCachingEnabled.value"
            @change="editChange('sSDCachingEnabled')"
          >
            <el-radio
              v-for="item in form.sSDCachingEnabled.options"
              :key="item.id"
              :label="item.id"
              :disabled="item.disable"
            >
              {{ traduction(item.label) }}
            </el-radio>
          </el-radio-group>
        </div>
      </el-form-item>
      <!-- 条带大小 -->
      <el-form-item v-if="isPMCCard && !isCache" :label="$t('STORE_STRIPE_SIZE')">
        <el-select
          id="isBootDisk"
          v-model="form.optimumIOSizeBytes.value"
          class="tiSelect"
          :disabled="maxCacheCount > 0"
          @change="editChange('optimumIOSizeBytes')"
        >
          <el-option
            v-for="item in volumeStaticModel.stripSize"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- 加速方法 -->
      <el-form-item v-if="isPMCCard && !isCache" :label="$t('STORE_ACCELERATION')">
        <el-select
          id="accelerationMethod"
          v-model="form.accelerationMethod.value"
          class="tiSelect"
          :disabled="isMaxCacheMethod"
          @change="editChange('accelerationMethod')"
        >
          <el-option
            v-for="item in form.accelerationMethod.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <!-- 容量 -->
      <el-form-item v-if="isPMCCard && !isCache" :label="$t('COMMON_CAPACITY')" prop="capacity">
        <template #default>
          <div class="flex">
            <div class="formCapacity">
              <el-tooltip
                popper-class="no-wrap-popover"
                placement="bottom"
                :teleported="false"
                trigger-keys
                :disabled="!rules.capacity.tip"
                :content="rules.capacity.tip"
              >
                <el-input
                  id="capacity"
                  prop="capacity"
                  v-model.trim="form.capacity"
                  :disabled="rules.capacity.disabled"
                  class="inputText"
                  @input="editChange('capacity')"
                ></el-input>
              </el-tooltip>
              <el-select
                id="capacityUnit"
                v-model="form.capacityUnit.value"
                :disabled="maxCacheCount > 0 || !props.volumeNode.getVolume.getModifyingCapacity"
                class="tiSelect"
                @change="editChange('capacityUnit')"
              >
                <el-option
                  v-for="item in volumeStaticModel.capacityUnit"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                />
              </el-select>
            </div>
          </div>
        </template>
      </el-form-item>
      <!-- 写策略 -->
      <el-form-item v-if="isPMCCard && isCache" :label="$t('STORE_WRITE_POLICY')">
        <el-select
          id="writePolicy"
          v-model="form.defaultWritePolicy.value"
          class="tiSelect"
          @change="editChange('defaultWritePolicy')"
        >
          <el-option
            v-for="item in form.defaultWritePolicy.options"
            :key="item.id"
            :label="item.label"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import {
  VolumeStaticModel,
  IFromSet,
  VolumeEditModel,
  VolumeNode,
  IVolumnDisabled,
  StorageTree,
  RaidNode,
  Raid,
  RaidType,
  IStorageRadio,
} from '../../../models';
import { inject, onMounted, reactive, ref } from 'vue';
import { factory } from '../../raid/raid.service';
import { traduction } from '@/utils/language';
import { VALID_PRODUCT_TAG } from '@/utils/regular';
import { loading } from '@/utils/composition';

let volumeStaticModel = VolumeStaticModel;
const props = defineProps({
  volumeNode: {
    required: true,
    type: Object,
    default: () => {
      return {};
    },
  },
});
const clickNode: any = inject('clickNode');
const emits = defineEmits(['change']);
const raidType = ref(props.volumeNode.getRaidType as string);
const isPMCCard = ref(false);
const hbaMode = ref(false);
const isMaxCacheMethod = ref(false);
const isCache = ref(false);
const maxCacheCount = ref(VolumeStaticModel.logicDriverList.maxCacheCount);
interface IOptions {
  name: string;
  id: string | number | boolean;
  label: string;
}
VolumeEditModel.getInstance().setOriginData = JSON.parse(
  JSON.stringify(VolumeEditModel.getInstance().getSelectData),
);
let volumeEditModel: any;
const volumnDisable: any = reactive({
  data: {
    accessPolicyConfig: true,
    drivePolicyConfig: true,
    iOPolicyConfig: true,
    isSSCD: true,
    readPolicyConfig: true,
    twoCache: true,
    writePolicyConfig: true,
  },
});
const form = reactive({
  namestr: '',
  defaultReadPolicy: {
    value: '',
    disable: false,
    options: [{ id: '', label: '', name: '' }],
  },
  defaultWritePolicy: {
    value: '',
    options: <IOptions[]>[{ id: '', label: '', name: '' }],
    disable: false,
  },
  ioPolicy: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  defaultCachePolicy: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  bGIEnabled: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  accessPolicy: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  driveCachePolicy: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  bootEnabled: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  sSDCachingEnabled: {
    value: '',
    options: [{ attrName: '', id: '', label: '', name: '', disable: false }],
    disable: false,
  },
  isSSCD: true,
  optimumIOSizeBytes: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  accelerationMethod: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  capacity: '',
  capacityUnit: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
  bootPriority: {
    value: '',
    options: [{ id: '', label: '', name: '' }],
    disable: false,
  },
});
const rules = reactive({
  namestr: {
    trigger: 'change',
    type: 'text',
    min: 1,
    max: 15,
    message: traduction('VALIDTOR_FORMAT_ERROR'),
    validator: (rule: any, val: string, callback: any): void | boolean => {
      const { message } = rule;
      if (!VALID_PRODUCT_TAG.test(val)) {
        callback(new Error(message));
        return false;
      }
    },
  },
  capacity: {
    trigger: 'change',
    type: 'text',
    message: traduction('VALIDTOR_FORMAT_ERROR'),
    tip: '',
    disabled: false,
    validator: (rule: any, val: string, callback: any): void | boolean => {
      const { message } = rule;
      if (!/^\d+(\.\d+)?$/.test(val)) {
        callback(new Error(message));
        return false;
      }
    },
  },
});
// 启动设备置灰控制（博通卡逻辑盘为启动设备时置灰）
const isBootDiskDisabled = ref(false);
onMounted(() => {
  const raidNode: RaidNode = StorageTree.getInstance().getParentRaid(props.volumeNode.getRaidIndex);
  if (raidType.value === RaidType.PMC || raidType.value === RaidType.HBA) {
    isPMCCard.value = true;
  }

  isBootDiskDisabled.value = props.volumeNode.getVolume.getBootDisk.id;
  factory(raidNode, raidNode.getUrl)
    .then((raid) => {
      raidNode.setRaid = raid as Raid;
      let raidCopy = raid as Raid;
      if (raidCopy.getMode?.id === 'HBA') {
        hbaMode.value = true;
      }
      VolumeEditModel.initVolumeEdit(
        props.volumeNode.getVolume.getName,
        props.volumeNode.getVolume.getBootDisk,
        props.volumeNode.getVolume.getSSCDFunc,
        props.volumeNode.getVolume.getDriveCache,
        props.volumeNode.getVolume.getAccessPolicy,
        props.volumeNode.getVolume.getBGIState,
        props.volumeNode.getVolume.getIOPolicy,
        props.volumeNode.getVolume.getWritePolicy,
        props.volumeNode.getVolume.getReadPolicy,
        props.volumeNode.getVolume.getCacheEnableState,
        props.volumeNode.getVolume.getTwoCache,
        props.volumeNode.getRaidIndex,
        hbaMode.value,
        props.volumeNode.getVolume.getStripeSizeBytes,
        props.volumeNode.getVolume.getSpeedMethod,
        props.volumeNode.getVolume.getCapacity,
        props.volumeNode.getVolume.getCapacityUnit,
        props.volumeNode.getVolume.getBootPriority,
      );
      volumeEditModel = VolumeEditModel.getInstance();
      volumnDisable.data = volumeEditModel.getVolumnDisable;
      if (isPMCCard.value) {
        isCache.value = props.volumeNode.getVolume.getCacheEnableState;
        isMaxCacheMethod.value = volumeEditModel.getSelectData.accelerationMethod?.id === 'maxCache';
        // PMC卡中逻辑盘当前容量小于最大容量时，提示容量取值范围
        if (props.volumeNode.getVolume.getModifyingCapacity) {
          rules.capacity.tip = traduction('VALID_VALUE_RANGE_TIP',
            [
              `${props.volumeNode.getVolume.getCapacity}${String(props.volumeNode.getVolume.getCapacityUnit?.id)}`,
              `${props.volumeNode.getVolume.getMaxCapacity}${String(props.volumeNode.getVolume.getMaxCapacityUnit?.id)}`,
            ]
          );
        }
        if (maxCacheCount.value > 0 || !props.volumeNode.getVolume.getModifyingCapacity) {
          rules.capacity.disabled = true;
        } else {
          rules.capacity.disabled = false;
        }
      }
      initForm(volumeEditModel);
    })
    .finally(() => {
      loading(false);
    });
});
function initForm(volumeNode: any) {
  form.namestr = volumeNode.getSelectData.name;
  form.defaultReadPolicy.value = volumeNode.getSelectData.defaultReadPolicy?.id;
  form.defaultReadPolicy.options = volumeStaticModel.readPolicy as any;
  form.defaultReadPolicy.disable = volumeNode.getVolumnDisable.readPolicyConfig;
  form.defaultWritePolicy.value = volumeNode.getSelectData.defaultWritePolicy?.id;
  if (raidType.value === RaidType.ARIES){
    form.defaultWritePolicy.options = VolumeStaticModel.writePolicy;
  } else if (isPMCCard.value) {
    form.defaultWritePolicy.options = VolumeStaticModel.pmcWritePolicy;
  } else {
    form.defaultWritePolicy.options = volumeNode.getWritePolicy;
  }
  form.defaultWritePolicy.disable = volumeNode.getVolumnDisable.writePolicyConfig;
  form.ioPolicy.value = volumeNode.getSelectData.defaultCachePolicy?.id;
  form.ioPolicy.options = volumeStaticModel.ioPolicy as any;
  form.ioPolicy.disable = volumeNode.getVolumnDisable.iOPolicyConfig;
  form.bGIEnabled.value = volumeNode.getSelectData.bGIEnabled?.id;
  form.bGIEnabled.options = volumeStaticModel.bgiState as any;
  form.bGIEnabled.disable = volumeNode.getVolumnDisable.twoCache;
  form.accessPolicy.value = volumeNode.getSelectData.accessPolicy?.id;
  form.accessPolicy.options = volumeStaticModel.accessPolicy as any;
  form.accessPolicy.disable = volumeNode.getVolumnDisable.accessPolicyConfig;
  form.driveCachePolicy.value = volumeNode.getSelectData.driveCachePolicy?.id;
  form.driveCachePolicy.options = volumeStaticModel.driverCachePolicy as any;
  form.driveCachePolicy.disable = volumeNode.getVolumnDisable.drivePolicyConfig;
  form.bootEnabled.value = volumeNode.getSelectData.bootEnabled?.id;
  form.bootEnabled.options = volumeStaticModel.isBootDisk as any;
  form.bootEnabled.disable = volumeNode.getVolumnDisable.twoCache;
  form.optimumIOSizeBytes.value = volumeNode.getSelectData.optimumIOSizeBytes?.id;
  form.accelerationMethod.value = volumeNode.getSelectData.accelerationMethod?.id;
  form.accelerationMethod.options = getSpeedOption(raidType.value) as any;
  form.capacity = volumeNode.getSelectData.capacity;
  form.capacityUnit.value = volumeNode.getSelectData.capacityUnit?.id;
  form.bootPriority.value = volumeNode.getSelectData.bootPriority?.id;
  form.isSSCD = volumeNode.getVolumnDisable.isSSCD;
  if (form.isSSCD) {
    form.sSDCachingEnabled.value = volumeNode.getSelectData.sSDCachingEnabled?.id;
    form.sSDCachingEnabled.options = volumeStaticModel.sscdFunction as any;
  }
}

function editChange(key: string) {
  if (key === 'namestr') {
    volumeEditModel.setName(form.namestr);
  }
  if (key === 'defaultReadPolicy') {
    volumeEditModel.setReadPolicy(
      form.defaultReadPolicy.options.find(item => {
        return item.id === form.defaultReadPolicy.value;
      }),
    );
  }
  if (key === 'defaultWritePolicy') {
    volumeEditModel.setWritePolicy(
      form.defaultWritePolicy.options.find(item => {
        return item.id === form.defaultWritePolicy.value;
      }),
    );
  }
  if (key === 'ioPolicy') {
    volumeEditModel.setIOPolicy(
      form.ioPolicy.options.find(item => {
        return item.id === form.ioPolicy.value;
      }),
    );
  }
  if (key === 'bGIEnabled') {
    volumeEditModel.setBgiState(
      form.bGIEnabled.options.find(item => {
        return item.id === form.bGIEnabled.value;
      }),
    );
  }
  if (key === 'accessPolicy') {
    volumeEditModel.setAccess(
      form.accessPolicy.options.find(item => {
        return item.id === form.accessPolicy.value;
      }),
    );
  }
  if (key === 'driveCachePolicy') {
    volumeEditModel.setDriveCache(
      form.driveCachePolicy.options.find(item => {
        return item.id === form.driveCachePolicy.value;
      }),
    );
  }
  if (key === 'bootEnabled') {
    volumeEditModel.setBootDisk(
      form.bootEnabled.options.find(item => {
        return item.id === form.bootEnabled.value;
      }),
    );
  }
  if (key === 'sSDCachingEnabled') {
    volumeEditModel.setSSCDFunc(
      form.sSDCachingEnabled.options.find(item => {
        return item.id === form.sSDCachingEnabled.value;
      }),
    );
  }
  if (key === 'bootPriority') {
    volumeEditModel.setBootPriority(
      volumeStaticModel.pmcBootPriority.find(item => {
        return item.id === form.bootPriority.value;
      }),
    );
  }
  if (key === 'optimumIOSizeBytes') {
    volumeEditModel.setStripeSizeBytes(
      volumeStaticModel.stripSize.find(item => {
        return item.id === form.optimumIOSizeBytes.value;
      }),
    );
  }
  if (key === 'accelerationMethod') {
    volumeEditModel.setSpeedMethod(
      form.accelerationMethod.options.find(item => {
        return item.id === form.accelerationMethod.value;
      }),
    );
  }
  if (key === 'capacity') {
    volumeEditModel.setCapacity(form.capacity);
  }
  if (key === 'capacityUnit') {
    volumeEditModel.setCapacityUnit(
      volumeStaticModel.capacityUnit.find(item => {
        return item.id === form.capacityUnit.value;
      }),
    );
  }
  const pamer = volumeEditModel.getSaveState;
  initForm(volumeEditModel);
  emits('change', pamer);
}

function getSpeedOption(type: string) {
  let resultOption = [];
  if (type === RaidType.HBA) {
    resultOption = VolumeStaticModel.speedMethod.filter(item => item.id !== 'ControllerCache');
  } else {
    resultOption = VolumeStaticModel.speedMethod;
  }
  return resultOption;
}
</script>

<style lang="scss" scoped>
.volumnEditBody {
  max-height: none !important;
  .radioBtn {
    display: inline-block;
    margin-right: 20px;
  }
  .ti3-form .ti3-form-label {
    white-space: normal;
  }
  .el-select {
    width: 100%;
  }
}
.el-select.tiSelect{
  :deep(.el-select__icon){
    font-size: 16px;
  }
}
.flex {
  display: flex;
}
.formCapacity {
  .el-input {
    width: 100px;
    margin-right: 8px;
  }
  .el-select {
    width: 88px;
  }
}
</style>
