<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"
        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
        label="通道组"
        prop="checker_channel_group_id"
      >
        <el-select
          v-model="form.checker_channel_group_id"
          @change="getChannelByGroup"
        >
          <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="channel_id"
      >
        <el-select
          v-model="form.channel_id"
        >
          <el-option
            :value="-1"
            label="未配置"
          ></el-option>
          <el-option
            v-for="item in channelList"
            :key="item.id"
            :value="item.id"
            :label="item.name"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="IP"
      >
        {{ form.ip }}
      </el-form-item>
      <el-form-item
        label="MAC"
      >
        {{ form.mac }}
      </el-form-item>
      <el-form-item
        label="类型"
      >
        {{ form.typeName }}
      </el-form-item>
      <el-form-item
        label="状态"
      >
        {{ form.online ? '上线' : '下线' }}
      </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, updateDevice } from '@/api/checker.js'
export default {
  props: {
    currentItem: {
      type: Object,
      default: () => {}
    },
    groupList: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      visible: false,
      form: {
        name: '',
        ip: '',
        mac: '',
        type_id: '',
        is_in: 1,
        online: 1,
        checker_channel_group_id: '',
        channel_id: ''
      },
      channelList: []
    }
  },
  methods: {
    handleClose () {
      this.$parent.handleClose()
    },
    getChannelByGroup (id) {
      return apiChannel('getCheckerChannelList', {
        keyWords3: id
      }).then(res => {
        this.channelList = [...res.list]
        this.form.channel_id = ''
      })
    },
    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 }
        this.getChannelByGroup(this.currentItem.checker_channel_group_id).then(() => {
          this.form.channel_id = this.currentItem.channel_id
        })
      }
    }
  }
}
</script>
