<template>
  <div>
    <el-dialog title="调整配置" append-to-body :visible.sync="addData.dialog" width="40%">
      <el-row>
        <el-form label-width="80px" :model="addData.data" ref="data">
          <basic-form-item label="规格" prop="flavorId" :rules="[required]" required-message="该字段为必填字段">
            <el-select v-model="addData.data.flavorId" filterable>
              <el-option v-for="item in flavorList" :key="item.id + item.name" :label="item.name + '(' + item.cpu + '核/' + item.memory + 'GB)'" :value="item.id"></el-option>
            </el-select>
          </basic-form-item>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="ghost" @click="addData.dialog = false">取 消</el-button>
        <el-button type="primary" @click="ok">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
/* global $ */
import { getVmSize, patchVmResize } from 'services/platform/index'
export default {
  props: {
    addData: {
      type: Object,
      default: function () {
        return {
          data: {
            labelIds: []
          },
          dialog: false
        }
      }
    }
  },
  data() {
    return {
      flavorList: []
    }
  },
  methods: {
    ok() {
      this.$refs.data.validate(valid => {
        if (valid) {
          patchVmResize('resize', this.addData.data).then(data => {
            if (data.success) {
              this.$message({
                type: 'success',
                message: data.message
              })
              this.addData.dialog = false
              this.$emit('back')
            }
          })
        }
      })
    },
    getFlavor() {
      getVmSize(this.addData.data.id).then(data => {
        if (data.success) {
          this.flavorList = data.data
        }
      })
    }
  },
  created() {
    this.getFlavor()
  }
}
</script>

<style></style>
