<template>
  <!--标准版-扫码器-->
  <div>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span">设备协议：</span>
      </div>

      <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="78px">
        <el-col :span="12">
          <el-form-item label="扫码器ip：" label-width="150px" prop="x">
            <el-input v-model.trim="form.scannerIP" />
          </el-form-item>
        </el-col>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="role-span" />
        <el-button
          :loading="false"
          icon="el-icon-check"
          size="mini"
          style="float: right; padding: 6px 9px"
          type="primary"
          @click="doSubmit"
        >保存
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  queryDriverConfig,
  updateConfig
} from '@/api/acs/device/driverConfig'

import crud from '@/mixins/crud'
import deviceCrud from '@/api/acs/device/device'

export default {
  name: 'StandardScanner',
  mixins: [crud],
  props: {
    parentForm: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      form: {
        scannerIP: ''
      },
      rules: {
      }
    }
  },
  created() {
    this.$nextTick(() => {
      // 从父表单获取设备编码
      this.device_id = this.$props.parentForm.device_id
      this.device_code = this.$props.parentForm.device_code
      queryDriverConfig(this.device_id, this.$props.parentForm.driver_code).then(data => {
        // 给表单赋值，并且属性不能为空
        if (data.form) {
          const arr = Object.keys(data.form)
          // 不为空
          if (arr.length > 0) {
            this.form = data.form
          }
        }

        // 给表单赋值，并且属性不能为空
        if (data.parentForm) {
          const arr = Object.keys(data.parentForm)
          // 不为空
          if (arr.length > 0) {
            this.opc_code = data.parentForm.opc_code
            this.plc_code = data.parentForm.plc_code
          }
        }
      })
      deviceCrud.selectDeviceList().then(data => {
        this.deviceList = data
      })
    })
  },
  methods: {
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.configLoading = true
          // 根据驱动类型判断是否为路由设备
          const parentForm = this.parentForm
          parentForm.is_route = true
          parentForm.plc_id = this.plc_id
          parentForm.opc_id = this.opc_id
          updateConfig(parentForm, this.form, this.data1, this.data2).then(res => {
            this.notify('保存成功', 'success')
            this.configLoading = false
          }).catch(err => {
            this.configLoading = false
            console.log(err.response.data.message)
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
