<template>
  <el-form
    inline
    :model="form"
    ref="form"
    class="form-wrap"
    label-width="150px"
  >
    <div class="table-box">
      <el-form-item
        class="is-required"
        verify
        label="名称"
        prop="name"
      >
        <el-input
          v-model="form.name"
          style="width:80%"></el-input>
      </el-form-item>

      <el-form-item
        class="is-required"
        verify
        label="入景点"
        prop="scenic_id"
      >
        <el-select v-model="form.scenic_id">
          <el-option
            value="-1"
            label="未配置"></el-option>
          <el-option
            v-for="item in scenicList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
    </div>

    <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 { updateBinder } from '@/api/ticketbinder'
export default {
  props: {
    currentItem: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    scenicList () {
      return this.$store.state.app.scenicList
    }
  },
  data () {
    return {
      visible: false,
      form: {
        name: '',
        scenic_id: ''
      }
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          updateBinder(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.form = { ...this.currentItem }
      }
    }
  }
}
</script>
