<template>
  <div>
    <el-dialog append-to-body title="修改主机规格" width="70%" :close-on-click-modal="false" v-if="addData.dialog" :visible.sync="addData.dialog">
      <basic-form :model="addData.data" ref="addData.data" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="24">
            <basic-form-item label="规格：" prop="instanceType">
              <el-form :inline="true">
                <basic-form-item>
                  <el-input placeholder="名称" v-model="searchData.name"></el-input>
                </basic-form-item>
                <basic-form-item>
                  <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                </basic-form-item>
              </el-form>
              <div class="table-container">
                <el-table :data="data" highlight-current-row tooltip-effect="dark" stripe border fit>
                  <el-table-column label="名称">
                    <template slot-scope="scope">
                      <el-radio :label="scope.row.id" v-model="addData.data.flavorId">
                        {{ scope.row.name }}
                      </el-radio>
                    </template>
                  </el-table-column>
                  <el-table-column label="id" prop="flavorUuid"> </el-table-column>
                  <el-table-column label="cpu" prop="cpu"> </el-table-column>
                  <el-table-column label="内存(GB)" prop="memory"> </el-table-column>
                </el-table>
                <div class="pagination-container">
                  <el-pagination @size-change="handleSizeChange" @current-change="getData" :current-page.sync="params.page" :page-sizes="[5, 10, 20, 30, 50]" :page-size="params.rows" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
                </div>
              </div>
            </basic-form-item>
          </el-col>
          <el-col :span="20">
            <el-col :span="8">
              <basic-form-item label="是否强制关机：" required-message="是否强制关机">
                <el-select v-model="addData.data.forceStop" clearable placeholder="请选择" value-key="value">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"> </el-option>
                </el-select>
              </basic-form-item>
            </el-col>
          </el-col>
        </el-row>
      </basic-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="ghost" @click.native="addData.dialog = false">取消</el-button>
        <el-button type="primary" @click.native="ok">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
/* global $ */
import { getFlavor, patchVmResize } from 'services/platform/index'
export default {
  props: {
    addData: {
      type: Object,
      default: function () {
        return {
          data: {
            labelIds: []
          },
          dialog: false
        }
      }
    },
    vendorId: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      data: [],
      params: {
        page: 1,
        rows: 10
      },
      total: 0,
      searchData: {
        name: ''
      },
      options: [
        {
          value: 'true',
          label: '是'
        },
        {
          value: 'false',
          label: '否'
        }
      ]
    }
  },
  methods: {
    ok() {
      if (this.addData.data.flavorId) {
        patchVmResize(null, this.addData.data).then(data => {
          if (data.success) {
            this.$message.success(data.message)
            this.addData.dialog = false
          }
        })
      } else return this.$message.error('请选择规格')
    },
    handleSearch() {
      this.params.page = 1
      this.params.params = this.$tools.handleSearchParam({
        vendorId: this.vendorId,
        instanceChargeType: this.addData.data.instanceChargeType,
        zone: this.addData.data.zone,
        status: 'SELL',
        family: this.addData.data.flavorUuid,
        'name:LK': this.searchData.name
      })
      this.getData()
    },
    handleSizeChange(val) {
      this.params.rows = val
      this.getData()
    },
    getData() {
      getFlavor(this.params).then(data => {
        if (data.success) {
          this.data = data.data.rows
          this.total = data.data.total
        }
      })
    }
  },
  created() {
    this.handleSearch()
  }
}
</script>

<style></style>
