<template>
  <div>
    <el-dialog
      :before-close="handleClose"
      :close-on-click-modal="false"
      :title="title + '新增设备信息'"
      :visible="visible"
      custom-class="bbsl-location-dialog"
      width="50%"
    >
      <div>
        <el-form
          ref="formInfo"
          :model="this.params"
          :rules="rules"
          label-position="top"
          label-width="150px"
        >
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="设备编码" prop="code">
                <el-input
                  v-model="params.code"
                  clearable
                  placeholder="请输入设备编码"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备名称" prop="name">
                <el-input
                  v-model="params.name"
                  clearable
                  placeholder="请输入设备名称"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="设备类型" prop="type">
                <el-select
                  v-model="params.type"
                  clearable
                  placeholder="请选择设备类型"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="item in typeList"
                    :key="item.value"
                    :label="item.name"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="所属考场" prop="roomId">
                <el-select
                  v-model="params.roomId"
                  clearable
                  placeholder="请选择所属考场"
                  style="width: 100%;"
                >
                  <el-option
                    v-for="item in roomList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="IP地址" prop="ip">
                <el-input
                  v-model="params.ip"
                  clearable
                  placeholder="请输入IP地址"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="通道号" prop="aisle">
                <el-input
                  v-model="params.aisle"
                  clearable
                  placeholder="请输入通道号"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="8">
              <el-form-item label="访问地址" prop="url">
                <el-input
                  v-model="params.url"
                  clearable
                  placeholder="请输入访问地址"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="备注：">
                <el-input
                  v-model="params.memo"
                  :rows="2"
                  clearable
                  placeholder="请输入备注"
                  type="textarea"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="goInfo">确认</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import deviceMenu from '@/api/examination/deviceMenu.js'
import dictUtils from '@/components/dict/DictSelectUtils.js'

export default {
  name: 'warning',
  data() {
    return {
      title: '',
      roomList: [],
      typeList: [],
      visible: false,
      params: {
        roomId: '',
        code: '',
        name: '',
        ip: '',
        aisle: '',
        url: '',
        type: ''
      },
      rules: {
        roomId: [
          {
            required: true,
            message: '请选择所属考场',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入设备编码',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入设备名称',
            trigger: 'blur'
          }
        ],
        type: [
          {
            required: true,
            message: '请输入设备类型',
            trigger: 'blur'
          }
        ],
        ip: [
          {
            required: true,
            message: '请输入IP地址',
            trigger: 'blur'
          }
        ],
        aisle: [
          {
            required: true,
            message: '请输入通道号',
            trigger: 'blur'
          }
        ],
        url: [
          {
            required: true,
            message: '请输入访问地址',
            trigger: 'blur'
          }
        ]
      },
      queryParam: {
        pageBean: {
          page: -1,
          pageSize: 10,
          showTotal: true
        }
      }
    }
  },
  created() {
    this.typeList = dictUtils.getDictItemsFromCache('sblx')
  },
  mounted() {},
  methods: {
    add(val) {
      this.getRoomQuery()
      this.visible = true
      this.title = '新增'
      this.params.type = this.typeList[0].value
      // this.params = val
    },
    edit(row) {
      this.getRoomQuery()
      this.visible = true
      this.title = '修改'
      deviceMenu.detailId(row.id, res => {
        this.params = res
      })
    },
    //保存
    goInfo() {
      this.$refs.formInfo.validate(valid => {
        if (valid) {
          if (this.title === '新增') {
            deviceMenu.addData(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '保存成功',
                  duration: 2000
                })
                this.handleClose()
                this.$parent.loadData(1)
              } else {
              }
            })
          }
          if (this.title === '修改') {
            deviceMenu.editData(this.params, response => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '修改成功',
                  duration: 2000
                })
                this.handleClose()
                this.$parent.loadData(1)
              } else {
              }
            })
          }
        } else {
          return false
        }
      })
    },
    getRoomQuery() {
      deviceMenu.roomQuery(this.queryParam, res => {
        if (res.rows) {
          this.roomList = res.rows
        }
      })
    },
    // 关闭新增人员窗口
    handleClose() {
      this.visible = false
      this.params = {
        roomId: '',
        code: '',
        name: '',
        ip: '',
        aisle: '',
        url: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
