<template>
  <el-form
    inline
    :model="form"
    ref="form"
    label-width="100px"
  >
    <el-form-item
      class="is-required"
      verify
      label="名称"
      prop="name"
    >
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item
      class="is-required"
      verify
      label="绑定景点"
      prop="scenicIdList"
    >
      <el-select
        v-model="form.scenicIdList"
        multiple
      >
        <el-option
          v-for="item in sceneList"
          :key="item.id"
          :value="item.id"
          :label="item.name"
        ></el-option>
      </el-select>
    </el-form-item>
    <div class="dialog-btn-wrap">
      <el-button @click="handleClose">
        关闭
      </el-button>
      <el-button
        @click="submit"
        type="primary"
      >
        保存
      </el-button>
    </div>
  </el-form>
</template>

<script>
import { updateDevice } from '@/api/checker.js'
export default {
  props: {
    currentItem: {
      type: Object,
      default: () => {}
    },
    sceneList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      form: {
        id: '',
        name: '',
        scenicIdList: []
      }
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          updateDevice(this.form).then(res => {
            this.$message.success('操作成功')
            this.handleClose()
            this.$emit('updateList')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    reset () {
      this.$refs.form.resetFields()
    }
  },
  watch: {
    visible (val) {
      if (val) {
        this.reset()
        this.form = { ...this.currentItem }
        console.log(this.form, this.sceneList, 'ba')
      }
    }
  }
}
</script>
