<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="type"
      >
        <el-select v-model="form.type">
          <el-option
            value="三辊闸"
            label="三辊闸"
          ></el-option>
          <el-option
            value="摆闸"
            label="摆闸"
          ></el-option>
          <el-option
            value="翼闸"
            label="翼闸"
          ></el-option>
          <el-option
            value="手持机"
            label="手持机"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        class="is-required"
        verify
        label="通道组"
        prop="group_id"
      >
        <el-select v-model="form.group_id">
          <el-option
            v-for="item in groupList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        class="is-required"
        verify
        label="进出"
        prop="is_in">
        <el-select v-model="form.is_in">
          <el-option
            :value="1"
            label="进口"
          ></el-option>
          <el-option
            :value="2"
            label="出口"
          ></el-option>
          <el-option
            :value="3"
            label="双向"
          ></el-option>
        </el-select>
      </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>
    </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 { apiChannel } from '@/api/checker.js'
export default {
  props: {
    type: {
      type: String,
      default: 'new'
    },
    currentItem: {
      type: Object,
      default: () => {}
    },
    groupList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      form: {
        group_id: '',
        is_in: 1,
        type: '',
        name: '',
        is_valid: 1
      }
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let url = this.type === 'new' ? 'createCheckerChannel' : 'updateCheckerChannel'
          apiChannel(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 }
        }
      }
    }
  }
}
</script>
