<template>
  <el-dialog
    :model-value="modelValue"
    title="设备详情"
    width="520px"
    align-center
    append-to-body
    destroy-on-close
    class="device-detail-dialog"
    @update:model-value="emit('update:modelValue', $event)"
  >
    <div v-if="data" class="detail-body">
      <div class="detail-row">
        <span class="detail-label">农机名称：</span>
        <span class="detail-value">{{ data.name || "-" }}</span>
      </div>

      <div class="detail-row">
        <span class="detail-label">农机类型：</span>
        <div class="detail-value">
          <span v-if="data.type" class="tag">{{ data.type }}</span>
          <span v-else>-</span>
        </div>
      </div>

      <div class="detail-row">
        <span class="detail-label">农事类别：</span>
        <div class="detail-value tags">
          <span
            v-for="(item, idx) in data.categories || []"
            :key="idx"
            class="tag"
          >
            {{ item }}
          </span>
          <span v-if="!(data.categories && data.categories.length)">-</span>
        </div>
      </div>

      <div class="detail-row">
        <span class="detail-label">存放点位：</span>
        <span class="detail-value">{{ data.location || "-" }}</span>
      </div>

      <div class="detail-row detail-row--top">
        <span class="detail-label">执行区域：</span>
        <div class="detail-value">
          <span v-if="data.areaLabel" class="area-box">
            <div class="area-text">{{ data.areaLabel }}</div>
          </span>
          <span v-else>-</span>
        </div>
      </div>

      <div class="detail-row">
        <span class="detail-label">负责人：</span>
        <span class="detail-value">{{ data.ownerName || "-" }}</span>
      </div>

      <div class="detail-row">
        <span class="detail-label">手机号：</span>
        <span class="detail-value">{{ data.ownerPhone || "-" }}</span>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button class="btn-edit" @click="handleEdit">编辑信息</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
defineProps({
  modelValue: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Object,
    default: null,
  },
});

const emit = defineEmits(["update:modelValue", "edit"]);

const handleEdit = () => {
  emit("edit");
};
</script>

<style lang="scss" scoped>
.detail-body {
  padding: 20px 28px 0 16px;
  border-top: 1px solid #E5E6EB;
}

.detail-row {
  display: flex;
  align-items: center;
  min-height: 32px;
  margin-bottom: 12px;

  &--top {
    align-items: flex-start;
  }
}

.detail-label {
  flex-shrink: 0;
  width: 70px;
  text-align: right;
  color: #1D2129;
  font-size: 14px;
  line-height: 22px;
}

.detail-value {
  flex: 1;
  margin-left: 12px;
  color: #000;
  font-size: 14px;
  line-height: 22px;
  word-break: break-all;
}

.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag {
  display: inline-block;
  padding: 1px 10px;
  border-radius: 4px;
  background: rgba(251, 142, 51, 0.1);
  color: #FB8E33;
  font-size: 12px;
  line-height: 22px;
}

.area-box {
}

.area-thumb {
  display: block;
  width: 100%;
  height: 96px;
  object-fit: cover;
}

.dialog-footer {
  display: flex;
  justify-content: center;
  padding-bottom: 8px;
}

.btn-edit {
  min-width: 120px;
  height: 36px;
  color: $base-color;
  background: #fff;
  border-color: $base-color;
  border-radius: 4px;

  &:hover,
  &:focus {
    color: #fff;
    background: $base-color;
    border-color: $base-color;
  }
}
</style>
