<template>
  <el-form
    inline
    :model="form"
    ref="form"
    label-width="100px"
  >
    <div class="dialog-info">
      <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="is_valid"
      >
        <el-select v-model="form.is_valid">
          <el-option
            :value="1"
            label="启用"
          ></el-option>
          <el-option
            :value="0"
            label="停用"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="入景点"
        prop="scenicIds"
      >
        <el-select
          v-model="form.scenicIds"
          multiple>
          <!-- <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>
      <el-form-item
        label="出景点"
        prop="locate_scenicIds"
      >
        <el-select
          v-model="form.locate_scenicIds"
          multiple>
          <!-- <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 { apiChannelGroup } from '@/api/checker.js'
// import elTreeselect from '@/components/SelectTree'
export default {
  // components: {
  //   elTreeselect
  // },
  props: {
    type: {
      type: String,
      default: 'new'
    },
    scenicList: {
      type: Array,
      default: () => []
    },
    currentItem: {
      type: Object,
      default: () => {}
    }
  },
  data () {
    return {
      visible: false,
      form: {
        name: '',
        is_valid: 1,
        // scenic_id: '',
        // locate_scenic_id: ''
        scenicIds: [],
        locate_scenicIds: []
      }
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url = this.type === 'new' ? 'createCheckerChannelGroup' : 'updateCheckerChannelGroup'
          apiChannelGroup(url, 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()
        if (this.type === 'edit') {
          this.form = {
            ...this.currentItem,
            scenicIds: (this.currentItem.scenics && this.currentItem.scenics.length) ? this.currentItem.scenics.map(v => v.scenic_id) : [],
            locate_scenicIds: (this.currentItem.locate_scenics && this.currentItem.locate_scenics.length) ? this.currentItem.locate_scenics.map(v => v.scenic_id) : []
          }
          console.warn(this.form)
        }
      }
    }
  }
}
</script>
