<template>
  <div>
    <basic-form label-width="120px" :model="itemData.data" ref="data">
      <PreviewItem :item-data="itemData"></PreviewItem>
      <div v-for="item in diskList" :key="item.uuid">
        <el-divider content-position="left">{{ item.name }}</el-divider>
        <!-- <basic-form-item label="磁盘类型：">{{item.category | typeFilter}}</basic-form-item> -->
        <basic-form-item label="原有大小：">{{ item.size || item.oldSize }} GB</basic-form-item>
        <basic-form-item label="扩容至：">
          <el-select v-model="item.skuId" class="w m-r">
            <el-option :label="generateSpec(item.spec)" v-for="item in item.skuList" :key="item.id" :value="item.id"></el-option>
          </el-select>
          <!-- <el-slider class="m-b" v-model="item.disk" :step="10" :min="item.size" :max="item.max" show-input>
          </el-slider> -->
        </basic-form-item>
        <basic-form-item label="创建lvm：" v-if="lvm">
          <el-checkbox class="m-l-xs" v-model="item.createLvm"></el-checkbox>
        </basic-form-item>
        <el-form-item label="  " v-if="lvm">
          <el-select v-if="item.createLvm" v-model="item.fileSystem" placeholder="文件系统" style="width: 200px">
            <el-option v-for="(item1, index) in fileList" :key="index" :label="item1.name" :value="item1.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="  " v-if="lvm">
          <el-input v-if="item.createLvm" v-model="item.device" style="width: 200px" placeholder="挂载目录"></el-input>
          <el-checkbox v-model="item.forceMount" v-if="item.createLvm" class="m-l-md">强制挂载</el-checkbox>
        </el-form-item>
        <basic-form-item label=" " v-else style="font-size: 12px; color: darkgrey">
          <span v-if="itemData.params.osCategory != 'WINDOWS'">LVM已存在，将对原有LVM进行扩容</span>
        </basic-form-item>
      </div>
      <div v-if="!diskList.length">暂无可扩容磁盘</div>
    </basic-form>
  </div>
</template>

<script>
import { getCategoriesByCode, getSkus, modifyResource, getSkusDetail } from 'services/services/product'
import { formatEqParams } from 'utils/index'
import { generateSpec, getSpecValue } from 'views/resource-apply/utils/index'
import PreviewItem from '../PreviewItem.vue'
import { conditionVoleme, getDictChildren } from 'services/platform/index'
export default {
  components: { PreviewItem },
  props: {
    itemData: {
      type: Object
    },
    config: {
      type: Object
    }
  },
  filters: {
    typeFilter(value) {
      const map = {
        SAS: '容量型',
        SSD: '高性能型'
      }
      return map[value]
    }
  },
  data() {
    return {
      generateSpec,
      loading: false,
      diskList: [],
      fileList: [],
      skuId: '',
      lvm: true
    }
  },
  created() {
    if (this.itemData.editDataDisks) {
      this.diskList = []
      this.itemData.editDataDisks.forEach((item, index) => {
        const result = {
          ...item,
          ...this.itemData.upgrade[index]
        }
        this.getSku(result)
        this.diskList.push(result)
      })
    } else {
      this.getDisk()
    }
    this.getDict()
    if (this.itemData.params.osCategory == 'WINDOWS') this.lvm = false
  },
  methods: {
    getDict() {
      getDictChildren({ value: 'FS_TYPE' }).then((data) => {
        if (data.success) {
          this.fileList = data.data
        }
      })
    },
    async getDisk() {
      const data = await conditionVoleme({ condition: 'vmDisks', serverId: this.itemData.id })
      if (data.success) {
        const ary = []
        data.data.forEach((item) => {
          if (item.id == this.itemData.volumeId) {
            const { uuid, name, size, category, skuId, categoryId, fileSystem, lvmMntp, forceMount } = item
            const result = {
              uuid,
              name,
              size,
              category,
              disk: size,
              categoryId,
              skuId,
              max: 1024,
              sliderMarks: {},
              fileSystem,
              device: lvmMntp,
              forceMount,
              createLvm: lvmMntp ? lvmMntp != '' : false
            }
            if (lvmMntp ? lvmMntp != '' : false) this.lvm = false
            this.getSku(result)
            ary.push(result)
          }
        })
        this.diskList = ary
      }
    },
    getSku(item) {
      const { categoryId } = item
      this.loading = true
      getSkus(formatEqParams({ categoryId, serviceCode: 'vmware.storage.disk' }))
        .then((data) => {
          if (data.success) {
            if (!data.data.length) return this.$message.error('该产品类型下不存在产品，请检查')
            const cell = data.data[0]
            // const [{ specValue: min }, { specValue: max }] = JSON.parse(cell.spec);
            if (!item.skuId) item.skuId = cell.id
            // item.min = Number(min);
            // item.max = Number(max);
            this.$set(
              item,
              'skuList',
              data.data.map((item) => {
                const spec = JSON.parse(item.spec)
                return {
                  ...item,
                  spec
                }
              })
            )
          }
        })
        .finally(() => {
          this.loading = false
        })
    },
    ok() {
      const { service, expiredTime, params, preview } = this.itemData
      const obj = {
        preview,
        params,
        service,
        instance: this.itemData.uuid,
        instanceName: this.itemData.name,
        operation: 'EditDisk',
        upgrade: [],
        editDataDisks: [],
        expiredTime
      }
      let deviceValidate = true
      this.diskList.forEach((item) => {
        const { uuid, skuId, name, createLvm, forceMount, fileSystem, device } = item
        obj.upgrade.push({
          serviceCode: 'vmware.storage.disk',
          uuid,
          skuId,
          specs: [
            {
              ...getSpecValue(item)
            }
          ]
        })
        obj.editDataDisks.push({
          name,
          ...getSpecValue(item),
          createLvm,
          forceMount,
          fileSystem,
          device,
          oldSize: item.size
        })
        if (createLvm && device == '') deviceValidate = false
      })
      if (deviceValidate) {
        return obj
      } else {
        return false
      }
    },
    getApplyData() {
      const obj = {
        ...this.itemData,
        upgrade: [],
        editDataDisks: []
      }
      let deviceValidate = true
      this.diskList.forEach((item) => {
        const { uuid, skuId, name, createLvm, forceMount, fileSystem, device } = item
        obj.upgrade.push({
          serviceCode: 'vmware.storage.disk',
          uuid,
          skuId,
          specs: [
            {
              ...getSpecValue(item)
            }
          ]
        })
        obj.editDataDisks.push({
          name,
          ...getSpecValue(item),
          createLvm,
          forceMount,
          fileSystem,
          device,
          oldSize: item.oldSize
        })
        if (createLvm && device == '') deviceValidate = false
      })
      if (deviceValidate) {
        return obj
      } else {
        return false
      }
    }
  }
}
</script>

<style></style>
