<template>
  <div class="c-info">
    <div class="l-flex--row has-bottom-padding u-bold">
      <span class="c-sibling-item">设备信息</span>
      <span
        v-if="hasEditPermission"
        class="c-sibling-item c-info__edit u-pointer"
        @click="toEdit"
      >
        <i class="el-icon-edit" />
        编辑
      </span>
    </div>
    <div class="l-flex--row c-info__block">
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">设备名称</div>
        <div class="l-flex__fill c-info__value">{{ device.name }}</div>
      </div>
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">产品</div>
        <div class="l-flex__fill c-info__value">{{ device.productName }}</div>
      </div>
    </div>
    <div class="l-flex--row c-info__block">
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">MAC</div>
        <div class="l-flex__fill c-info__value">{{ device.mac }}</div>
      </div>
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">序列号</div>
        <div class="l-flex__fill c-info__value">{{ device.serialNumber }}</div>
      </div>
    </div>
    <div class="l-flex--row c-info__block">
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">分辨率</div>
        <div class="l-flex__fill c-info__value">{{ device.resolutionRatio }}</div>
      </div>
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">创建时间</div>
        <div class="l-flex__fill c-info__value">{{ device.createTime }}</div>
      </div>
    </div>
    <div class="l-flex--row c-info__block">
      <div class="l-flex--row l-flex__fill c-info__item">
        <div class="l-flex__none c-info__title">地址</div>
        <div class="l-flex__fill c-info__value primary">{{ device.remark }}</div>
      </div>
    </div>
    <el-dialog
      title="编辑"
      :visible.sync="show"
      custom-class="c-dialog"
      :close-on-click-modal="false"
    >
      <div class="c-form">
        <div class="c-form__section">
          <span class="c-form__label required">名称：</span>
          <el-input
            v-model.trim="info.name"
            class="c-form__item"
            maxlength="50"
            show-word-limit
          />
        </div>
        <div class="c-form__section">
          <span class="c-form__label required">地址：</span>
          <el-input
            v-model="info.remark"
            class="c-form__item"
            type="textarea"
            :rows="3"
            maxlength="100"
            show-word-limit
          />
        </div>
      </div>
      <template #footer>
        <button
          class="o-button"
          @click="save"
        >
          确定
        </button>
        <button
          class="o-button cancel"
          @click="close"
        >
          取消
        </button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { updateDevice } from '@/api/device'

export default {
  name: 'DeviceInfo',
  props: {
    device: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      show: false,
      info: {}
    }
  },
  methods: {
    toEdit () {
      this.info = {
        name: this.device.name,
        remark: this.device.remark
      }
      this.show = true
    },
    close () {
      this.show = false
    },
    save () {
      if (!this.info.name) {
        this.$message({
          type: 'warning',
          message: '名称不能为空'
        })
        return
      }
      if (!this.info.remark) {
        this.$message({
          type: 'warning',
          message: '地址不能为空'
        })
        return
      }
      if (this.info.name === this.device.name && this.info.remark === this.device.remark) {
        this.close()
        return
      }
      updateDevice({
        id: this.device.id,
        ...this.info
      }).then(() => {
        Object.assign(this.device, this.info)
        this.close()
      })
    }
  }
}
</script>
