text: import PageHeaderLayout from '@/components/PageHeaderLayout'
  import HeaderSearchAdd from '@/components/HeaderSearchAdd'
  import { getList, add, remove, update } from '@/api/equipment'
  export default {
    components: { PageHeaderLayout, HeaderSearchAdd },
    data() {
      const validateName = (rule, value, callback) => {
        if (value.trim().length === 0) {
          callback(new Error('请输入设备名称'))
        } else {
          if (value.length < 1) {
            callback(new Error('不能小于1位'))
          } else {
            callback()
          }
        }
      }
      const validateIp = (rule, value, callback) => {
        if (value.trim().length === 0) {
          callback(new Error('请输入IP地址'))
        } else {
          if (value.length < 1) {
            callback(new Error('不能小于1位'))
          } else {
            callback()
          }
        }
      }
      const validatePort = (rule, value, callback) => {
        if (value.trim().length === 0) {
          callback(new Error('请输入端口号'))
        } else {
          if (value.length < 1) {
            callback(new Error('不能小于1位'))
          } else {
            callback()
          }
        }
      }
      return {
        addDialogVisible: false,
        editDialogVisible: false,
        list: [],
        searchkey: '',
        currentId: '',
        totalPage: 0,
        pageNum: 1,
        pageSize: 15,
        facilityForm: {
          facilityName: '',
          ip: '',
          port: '',
          factoryCode: ''
        },
        rules: {
          facilityName: [{ required: true, validator: validateName, trigger: 'blur' }],
          ip: [{ required: true, validator: validateIp, trigger: 'blur' }],
          port: [{ required: true, validator: validatePort, trigger: 'blur' }]
        }
      }
    },
    watch: {
    },
    created() {
      const params = {
        pageNum: 1,
        pageSize: 15,
        searchkey: ''
      }
      this.fetchData(params)
    },
    mounted() {
    },
    methods: {
      // 每页数量改变
      sizeChange(pageSize) {
        const params = {
          pageNum: this.pageNum,
          pageSize
        }
        this.pageSize = pageSize
        this.fetchData(params)
      },
      // 当前页数改变
      currentChange(pageNum) {
        const params = {
          pageSize: this.pageSize,
          pageNum
        }
        this.pageNum = pageNum
        this.fetchData(params)
      },
      handleSearch(data) {
        const params = {
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          searchkey: data
        }
        this.fetchData(params)
      },
      // 查询
      fetchData(params) {
        this.listLoading = true
        const requestParams = params || {
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          searchkey: this.searchkey
        }
        getList(requestParams).then(res => {
          if (res.code === 0) {
            this.list = res.data.list
            this.totalPage = parseInt(res.data.total)
            this.listLoading = false
          }
        })
      },
      // 添加
      handleAdd() {
        this.facilityForm.facilityName = ''
        this.facilityForm.ip = ''
        this.facilityForm.port = ''
        this.facilityForm.factoryCode = ''
        this.addDialogVisible = true
      },
      // 编辑
      handleEdit(row) {
        this.currentId = row.id
        this.facilityForm.facilityName = row.name
        this.facilityForm.ip = row.toIP
        this.facilityForm.port = row.toport
        this.facilityForm.factoryCode = row.factorycode
        this.editDialogVisible = true
      },
      // 删除
      handleDelete(row) {
        this.$confirm('此操作将永久删除该设备, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const params = {
            id: row.id
          }
          remove(params).then(res => {
            if (res.code === 0) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.editDialogVisible = false
              this.fetchData()
            }
          })
        }).catch(() => {
        })
      },
      // 关闭
      handleClose(done) {
        done()
      },
      // 添加提交
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = {
              name: this.facilityForm.facilityName,
              factorycode: '001',
              toIP: this.facilityForm.ip,
              toport: this.facilityForm.port
            }
            add(params).then(res => {
              if (res.code === 0) {
                this.$message({
                  type: 'success',
                  message: '添加成功!'
                })
                this.$refs[formName].resetFields()
                this.addDialogVisible = false
                this.fetchData()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 编辑提交
      submitEditForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = {
              id: this.currentId,
              name: this.facilityForm.facilityName,
              factorycode: '001',
              toIP: this.facilityForm.ip,
              toport: this.facilityForm.port
            }
            update(params).then(res => {
              if (res.code === 0) {
                this.$message({
                  type: 'success',
                  message: '编辑成功!'
                })
                this.$refs[formName].resetFields()
                this.editDialogVisible = false
                this.fetchData()
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      // 取消
      resetForm(formName) {
        this.editDialogVisible = false
        this.addDialogVisible = false
        this.$refs[formName].resetFields()
      }
    }
  }
  