<template>
  <div>
    <el-dialog title="新增虚拟卷" append-to-body :visible.sync="addData.newdialog" width="40%">
      <basic-form :model="addData.data" ref="data">
        <basic-form-item label="名称：" prop="name" validate="required">
          <el-input v-model="addData.data.name"></el-input>
        </basic-form-item>
        <basic-form-item label="容量：" prop="size" validate="required">
          <el-input v-model="addData.data.size"></el-input>
        </basic-form-item>
        <basic-form-item label="总线：" prop="bus" validate="required">
          <el-select v-model="addData.data.bus" clearable>
            <el-option v-for="(item, index) in busData" :key="index" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </basic-form-item>
        <basic-form-item label="存储策略：" prop="volumeTemplateId" validate="required">
          <el-select v-model="addData.data.volumeTemplateId" clearable>
            <el-option v-for="(item, index) in templateData" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </basic-form-item>
      </basic-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="ghost" @click="addData.newdialog = false">取 消</el-button>
        <el-button type="primary" @click="ok" :loading="loading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { patchDisk } from 'services/platform/index'
import { getVolumeTpl } from 'services/platform/smart'
export default {
  props: {
    addData: {
      type: Object
    }
  },
  data() {
    return {
      loading: false,
      templateData: [],
      busData: [
        { name: 'VIRTIO', value: 'VIRTIO' },
        { name: 'SCSI', value: 'SCSI' },
        { name: 'IDE', value: 'IDE' }
      ]
    }
  },
  created() {
    this.getTemplateData()
  },
  methods: {
    getTemplateData() {
      getVolumeTpl({
        simple: true,
        params: this.$tools.handleSearchParam({
          vendorId: this.addData.vendorId
        })
      }).then(data => {
        if (data.success) {
          this.templateData = data.data.rows
        }
      })
    },
    ok() {
      this.$refs.data.validate(valid => {
        if (valid) {
          const obj = {
            action: 'create',
            serverId: this.addData.serverId,
            addDataDisks: [this.addData.data]
          }
          this.loading = true
          patchDisk(obj)
            .then(data => {
              if (data.success) {
                this.$message.success(data.message)
                this.addData.newdialog = false
                this.$emit('back')
              }
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.w {
  width: 100px !important;
}
.add-border {
  padding: 2px;
  cursor: pointer;
  border: 1px dashed black;
  text-align: center;
}
</style>
