<template>
<div>
  <sticky className="sub-navbar">
    <div style="text-align:right;">
      <el-button type="success" :loading="onSubmit" @click="handleSave">保存</el-button>
      <el-button @click="handleCancel">取消</el-button>
    </div>
  </sticky>
  <div class="app-container app-edit">
    <el-form class="small-space" :model="form" :rules="rules" ref="form" label-position="right" label-width="120px" style="width: 700px;">

      <!-- 设备名称 -->
      <el-form-item label="设备名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入设备名称"></el-input>
      </el-form-item>

      <!-- 视频流地址 -->
      <el-form-item label="视频流地址" prop="stream">
        <el-input v-model="form.stream" placeholder="请输入视频流地址"></el-input>
      </el-form-item>

      <el-form-item label="篮球馆" prop="venueId">
        <el-select v-model="form.venueId" filterable placeholder="请选择篮球馆">
          <el-option
            v-for="item in venues"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</div>
</template>

<script>
import Sticky from '@/components/Sticky'
import { find, save } from '@/api//device'
import { findVenueByType } from '@/api//venue'

export default {
  components: { Sticky },
  data() {
    return {
      form: {
        id: undefined,
        name: '',
        stream: '',
        venueId: '',
        venueType: 2
      },
      venues: [],
      onSubmit: false,

      // 表单验证规则
      rules: {
        name: [
         { required: true, message: '请输入设备名称', trigger: 'blur' }
        ],
        stream: [
         { required: true, message: '请输入视频流地址', trigger: 'blur' }
        ],
        venueId: [
          { required: true, message: '请选择篮球馆', trigger: 'blur' }
        ]
//        username: [
//          { required: true, message: '请输入姓名', trigger: 'blur' },
//          { max: 15, message: '姓名长度不能超过15个字符', trigger: 'blur' }
//        ],
//        email: [
//          { required: true, message: '请输入邮箱', trigger: 'blur' },
//          { type: 'email', message: '您输入的邮箱格式不正确', trigger: 'blur' }
//        ],
//        password: [
//          { required: true, message: '请输入密码', trigger: 'blur' },
//          { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
//        ]
      }
    }
  },
  created() {
    findVenueByType(1).then(response => {
      this.venues = response.venues
    })
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.findById(this.form.id)
    }
  },
  methods: {
    /**
     * 查询门禁设备
     */
    findById(id) {
      find(id).then(response => {
        this.form.name = response.device.name
        this.form.stream = response.device.stream
        this.form.venueId = response.device.venueId
        this.form.venueType = response.device.venueType
      })
    },

    /**
     * 保存门禁设备
     */
    handleSave() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false
        }

        this.onSubmit = true
        save(this.form).then(response => {
          this.$router.go(-1)
        }).finally(() => {
          this.onSubmit = false
        })
      })
    },

    /**
     * 取消
     */
    handleCancel() {
      this.$router.go(-1)
    }
  }
}
</script>
