<template>
  <div>
    <div>
      <el-form label-width="auto">
        <el-form-item :label="$t('STORE_LOCATION_STATUS')">
          <el-switch
            id="indicatorSwitch"
            v-model="form.indicator"
            @change="change('indicator')"
          ></el-switch>
        </el-form-item>
        <el-form-item v-if="showBootDevice" :label="$t('STORE_BOOT_DISK')">
          <el-select
            id="bootPriority"
            v-model="form.bootPriority"
            class="tiSelect"
            @change="change('bootPriority')"
            >
            <el-option
              v-for="item in form.pmcBootPriority"
              :key="item.id"
              :label="item.label"
              :value="item.label"
              :disabled="item['disable']"
            />
          </el-select>
        </el-form-item>
        <!-- 热备状态 -->
        <el-form-item v-if="!form.volumeSupport" :label="$t('FDM_HOT_STATUS')">
          <el-radio-group v-model="form.hotspare" @change="change('hotspare')">
            <el-radio
              v-for="item in form.getHotspareBtn"
              :key="item.id"
              :label="item.id"
              :disabled="item.disable"
            >
              {{ $t(item.label) }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item
          v-if="!form.volumeSupport && (data.getRaidType === 'BRCM' || showFirmwareStatus)"
          :label="$t('FDM_FIRMWARE_STATUS')"
        >
          <el-select
            id="firmwareSelect"
            v-model="form.firmwareStatus"
            class="tiSelect"
            :disabled="form.firmwareDisabled"
            @change="change('firmwareStatus')"
          >
            <el-option
              v-for="item in form.getFirmwareStatusBtn"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.getLogicalDriveBtnShow && !form.volumeSupport"
          :label="$t('HOME_LOGICAL_DISK')"
        >
          <el-select
            id="logicalSelect"
            v-model="form.spareforLogicalDrive"
            class="tiSelect"
            :disabled="form.spareforLogicalDrivedisabled"
            @change="change('spareforLogicalDrive')"
          >
            <el-option
              v-for="item in form.spareforLogicalDriveOption"
              :key="item.id"
              :label="item.label"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <!-- 启动设备 -->
        <el-form-item v-if="!driveEditModel.getVolumeSupport && showAriesBootDisk" :label="$t('STORE_BOOT_DISK')">
          <el-radio-group v-model="form.bootEnabled" @change="change('bootEnabled')">
            <el-radio
              v-for="item in form.isBootDisk"
              :key="item.id"
              :label="item.id"
              :disabled="item.disable"
            >
              {{ $t(item.label) }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="showPatrolASwitch" :label="$t('STORE_PATROL_SWITCH')">
          <el-radio-group v-model="form.patrolState" @change="change('patrolState')">
            <el-radio
              v-for="item in form.patrolSwitchStatus"
              :key="item.id"
              :label="item.id"
              :disabled="item.disable || !isOptimal"
            >
              {{ $t(item.label) }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { traduction } from '@/utils/language';
import { defineComponent, inject, onMounted, reactive, ref } from 'vue';
import { IFromSet, StorageTree, DriverEditModel, DriverNode, RaidType, Raid } from '../../../models';
import { factory } from '../../raid/raid.service';
import { getLoginOptions } from './driver-edit.service';
import { DriveStaticModel } from '../../../models/static-data';

const props = defineProps({
  data: {
    required: true,
    type: Object,
    default: () => {
      return {};
    },
  },
  settingShow: {
    required: false,
    type: Boolean,
    default: () => {
      return false;
    }
  },
});
const emit = defineEmits(['handleClose', 'update']);
const clickNode: any = inject('clickNode');
const treeRef: any = inject('treeRef');
let driveEditModel = DriverEditModel.getInstance();
const temData = {};
const isARIESrCard = ref(false);
const isOptimal = ref(false);
const showPatrolASwitch = ref(false);
const showFirmwareStatus = ref(false);
const showAriesBootDisk = ref(false);
const showBootDevice = ref(false);

const form = reactive({
  indicator: false,
  volumeSupport: false,
  getLogicalDriveBtnShow: false,
  getHotspareBtn: [{ label: 'STORE_NONE', id: 'None', name: '', disable: false }],
  hotspare: 'None',
  firmwareStatus: 'Offline',
  firmwareDisabled: false,
  getFirmwareStatusBtn: [{ id: 'HotSpareDrive', label: 'HOT SPARE', name: 'IOptions' }],
  spareforLogicalDrive: 'nodata',
  spareforLogicalDrivedisabled: false,
  spareforLogicalDriveOption: [{ id: 'noData', label: 'noData', name: 'IOptions' }],
  patrolSwitchStatus: [{ id: 'DoneOrNotPatrolled', disable: false, label: 'FDM_DISABLED' }],
  patrolState: 'DoneOrNotPatrolled',
  bootPriority: 'Primary',
  pmcBootPriority: [{ id: 'Primary', label: 'Primary', name: 'IOptions' }],
  bootEnabled: false,
  isBootDisk: [{label: 'COMMON_YES', id: true, disable: false, name: 'IRadio', attrName: 'BootDisk'}],
});
function initForm() {
  form.indicator = driveEditModel.getIndicator?.state;
  form.volumeSupport = driveEditModel.getVolumeSupport;
  form.hotspare = driveEditModel.getHotspareType?.id as string;
  form.getHotspareBtn = [...driveEditModel.getHotspareBtn] as any;
  form.getFirmwareStatusBtn = [...(driveEditModel.getFirmwareStatusBtn.options as any)];
  form.firmwareDisabled = driveEditModel.getFirmwareStatusBtn.disable;
  form.firmwareStatus = driveEditModel.getSelectData.firmwareStatus?.id as string;
  form.getLogicalDriveBtnShow = driveEditModel.getLogicalDriveBtn.show;
  form.spareforLogicalDriveOption = [...(driveEditModel.getLogicalDriveBtn.options as any)];
  form.spareforLogicalDrive = driveEditModel.getSelectData?.spareforLogicalDrive?.['id'] as string;
  form.spareforLogicalDrivedisabled = driveEditModel.getLogicalDriveBtn.disable;
  form.bootPriority = driveEditModel.getSelectData.bootPriority?.id as string;
  form.pmcBootPriority = DriveStaticModel.pmcBootPriority as any;  
  form.bootEnabled = driveEditModel.getSelectData.bootEnabled?.id as boolean;  
  form.isBootDisk = DriveStaticModel.isBootDisk as any;
  const firmwareStatusCheck = form.getFirmwareStatusBtn.find(item => {
    return item.id === form.firmwareStatus;
  });
  if (!firmwareStatusCheck) {
    form.getFirmwareStatusBtn.push(driveEditModel.getSelectData.firmwareStatus as any);
  } else {
    form.getFirmwareStatusBtn = [...(driveEditModel.getFirmwareStatusBtn.options as any)];
  }
  form.patrolSwitchStatus = DriveStaticModel.patrolSwitchStatus as any;
  form.patrolState = driveEditModel.getSelectData.patrolState?.id as string;
  emit('update', true);
}

onMounted(() => {
  const loginData = getLoginOptions(clickNode.data);
  const raidNode = StorageTree.getInstance().getParentRaid(props.data.getRaidIndex);
  const parent = StorageTree.getInstance().getParentNode(clickNode.data.labelId);
  clickNode.data.parent = parent;
  const indicator = props.data.getDriver.getIndicator
    ? (props.data.getDriver.getIndicator.state as boolean)
    : null;

  if (clickNode.data.getRaidType === RaidType.ARIES) {
    isARIESrCard.value = true;
    clickNode.data.parent?.forEach((item: any) => {
      // "巡检开关"显示逻辑：1.父元素有VolumeNode;2.父元素的VolumeNode的raid级别不为RAID0；3.物理盘类型为HDD
      if (
        item.componentName === 'VolumeNode' &&
        item.getRaidLevel !== 'RAID0' &&
        props.data.getDriver.getMediaType === 'HDD'
      ) {
        // 1880卡下的逻辑盘状态不为Optimal时，该逻辑盘下的物理盘的巡检状态置灰
        isOptimal.value = item?.getVolume?.status[1] === 'Optimal';
        showPatrolASwitch.value = true;
      }
      // "固件状态"显示逻辑：父元素为RaidNode;
      if (clickNode.data.parent.length === 1 && item.componentName === 'RaidNode') {
        showFirmwareStatus.value = true;
      }
    });
  }
  const isPmcOrHba = props.data.getRaidType === RaidType.PMC || props.data.getRaidType === RaidType.HBA;
  const hasSingleParent = clickNode.data.parent.length === 1;
  const isNotRaidMode = clickNode.data.parent[0]?.getRaid?.getMode?.id !== 'RAID';
  if (isPmcOrHba && hasSingleParent && isNotRaidMode) { 
    // 只有pmc中在raid卡下面的物理盘，且模式不为raid时才显示“启动设备”项
    showBootDevice.value = true;
  }

  if (props.data.getRaidType === RaidType.ARIES && clickNode.data.parent.length === 1) {
    // 1880卡在raid卡下面的物理盘和逻辑盘才显示“启动设备”
    showAriesBootDisk.value = true;
  }

  // hi1880卡热备盘只支持单选
  const logicSelected = isARIESrCard.value ? loginData.logicSelect[0] : loginData.logicSelect;

  if (props.settingShow) {
    factory(raidNode, raidNode.getUrl).then((node: any) => {
      raidNode.setRaid = node;
      if (clickNode.data.getRaidType === RaidType.ARIES) {
        DriverEditModel.initDriveEdit(
          indicator as boolean,
          props.data.getDriver,
          logicSelected,
          loginData.options,
          raidNode.getRaid,
          props.data.getDriver.getBootDisk,
          props.data.getRaidType,
          props.data.getDriver.getBootPriority,
          props.data.getDriver.getPatrolState,
        );
        isARIESrCard.value = true;
      } else {
        DriverEditModel.initDriveEdit(
          indicator as boolean,
          props.data.getDriver,
          logicSelected,
          loginData.options,
          raidNode.getRaid,
          props.data.getDriver.getBootDisk,
          clickNode.data.getRaidType,
          props.data.getDriver.getBootPriority,
        );
      }
      driveEditModel = DriverEditModel.getInstance();
      DriverEditModel.getInstance().setOriginData = JSON.parse(
        JSON.stringify(DriverEditModel.getInstance().getSelectData),
      );
      DriverEditModel.getInstance().checkSave();
      initForm();
    });
  } else {
    DriverEditModel.initDriveEdit(
      indicator as boolean,
      props.data.getDriver,
      logicSelected,
      loginData.options,
      {} as Raid,
      props.data.getDriver.getBootDisk,
    );
    driveEditModel = DriverEditModel.getInstance();
    DriverEditModel.getInstance().setOriginData = JSON.parse(
      JSON.stringify(DriverEditModel.getInstance().getSelectData),
    );
    DriverEditModel.getInstance().checkSave();
    initForm();
  }
});
function change(key: string) {
  if (key === 'hotspare') {
    const hotspare = form.getHotspareBtn.find(item => {
      return item.id === form.hotspare;
    });
    driveEditModel.setHotspareType(hotspare as any);
    form.spareforLogicalDrive = driveEditModel.getSelectData.spareforLogicalDrive?.['id'] as string;
    driveEditModel.setLogicalDrive(driveEditModel.getSelectData.spareforLogicalDrive || []);
  }
  if (key === 'firmwareStatus') {
    const firmwareStatus = form.getFirmwareStatusBtn.find(item => {
      return item.id === form.firmwareStatus;
    });
    driveEditModel.setFirmwareSelect(firmwareStatus as any);
  }
  if (key === 'spareforLogicalDrive') {
    const spareforLogicalDrive = form.spareforLogicalDriveOption.find(item => {
      return item.id === form.spareforLogicalDrive;
    });
    driveEditModel.setLogicalDrive(spareforLogicalDrive as any);
  }
  if (key === 'indicator') {
    driveEditModel.setIndicator(form.indicator);
  }
  if (key === 'patrolState') {
    const patrolState = form.patrolSwitchStatus.find(item => {
      return item.id === form.patrolState;
    });
    driveEditModel.setPatrolState(patrolState as any);
  }
  if (key === 'bootEnabled') {
    const bootEnabled = form.isBootDisk.find(item => {
      return item.id === form.bootEnabled;
    });
    driveEditModel.setBootDisk(bootEnabled as any);
  }
  if (key === 'bootPriority') {
    const bootPriority = form.pmcBootPriority.find(item => {
      return item.id === form.bootPriority;
    });
    driveEditModel.setBootPriority(bootPriority as any);
  }
  const param = driveEditModel.getSaveState;
  initForm();
  emit('handleClose', param);
}
</script>

<style lang="scss" scoped>
.editRadio {
  display: inline-block;
  margin-right: 20px;
}
#logicalSelect {
  width: 220px !important;
}

#firmwareSelect {
  width: 220px !important;
}
.ti3-modal-body {
  overflow-y: hidden;
}
.el-select.tiSelect{
  width: 100%;
  min-width: 100px;
  :deep(.el-select__icon){
    font-size: 16px;
  }
}
</style>