<template>
  <el-dialog
    title="接单记录"
    :visible.sync="visible"
    width="70%"
    :before-close="handleClose"
    append-to-body
  >
    <div class="dialog-content">
      <div class="dialog-header">
        <div class="order-info">
          <span>订单号: {{ orderInfo ? orderInfo.orderId : '' }}</span>
          <span style="margin-left: 20px">客户: {{ orderInfo ? orderInfo.name : '' }}</span>
        </div>
        <el-button type="primary" size="mini" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </div>

      <el-table
        height="calc(100vh - 290px)"
        :data="recordList"
        style="width: 100%"
        v-loading="loading"
        border
        stripe
      >
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column label="添加时间" align="center" width="180">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createdAt) }}
          </template>
        </el-table-column>
        <el-table-column label="标题" align="center" width="120" prop="title"/>
        <el-table-column label="内容" align="left" min-width="200">
          <template slot-scope="scope">
            <div v-if="scope.row.content">
              <div
                v-for="(item, idx) in parseContentItems(scope.row.content)"
                :key="idx"
                style="margin-bottom:12px;"
              >
                <!-- 项目费用结构 -->
                <div v-if="item.project">
                  <div>项目费用: ￥{{ item.project.price }}</div>
                  <div v-if="item.basic && item.basic.length">基础项: {{ item.basic.map(b => b.name).join('、') }}</div>
                  <div v-if="item.craft && item.craft.length">所需服务: {{ item.craft.map(c => `${c.name} ￥${c.price}*${c.count}`).join('、') }}</div>
                  <div v-if="item.material && item.material.length">所需物料: {{ item.material.map(m => `${m.name} ￥${m.price}*${m.count}`).join('、') }}</div>
                </div>
                <!-- 普通文本或名称/评价 -->
                <div v-else>
                  <div v-if="item.name || item.text">{{ item.name || item.text }}</div>
                  <!-- 图片列表 -->
                  <div v-if="item.image && (item.image.length || (typeof item.image === 'string' && item.image))" class="image-list" style="margin:8px 0;">
                    <el-image
                      v-for="(img, i2) in getItemImageList(item)"
                      :key="i2"
                      :src="img"
                      style="width:60px; height:60px; margin-right:8px;"
                      fit="cover"
                      :preview-src-list="getItemImageList(item)"
                    ></el-image>
                  </div>
                  <!-- 星级评分 -->
                  <el-rate
                    v-if="item.num !== undefined"
                    :value="item.num"
                    disabled
                    show-score
                    style="margin-top:8px;"
                  ></el-rate>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="师傅" align="center" width="120" prop="workerId" />
        <el-table-column label="金额" align="center" width="120" prop="price" />
        <el-table-column label="是否支付" align="center" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
              {{ scope.row.status === 1 ? '已接单' : '已转单' }}
            </el-tag>
          </template>
        </el-table-column>
      </el-table>

      <div v-if="!recordList || recordList.length === 0" class="empty-record">
        <i class="el-icon-document"></i>
        <p>暂无记录信息</p>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getReceiveRecords } from "@/api/system/Order"

export default {
  name: "ReceiveRecord",
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    orderInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      recordList: [],
      loading: false
    }
  },
  watch: {
    visible(val) {
      if (val && this.orderInfo) {
        this.getList()
      }
      
    },
    immediate: true
  },
  mounted(){
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      getReceiveRecords(this.orderInfo.orderId).then(response => {
        this.recordList = response.data
        this.loading = false
      }).catch(() => {
        this.loading = false
        this.$message.error('获取记录失败')
      })
    },
    parseContentItems(content) {
      if (!content) return []

      try {
        const data = JSON.parse(content)
        return Array.isArray(data) ? data : [data]
      } catch (e) {
        return []
      }
    },
    getItemImageList(item) {
      if (!item.image) return []
      try {
        const imgs = item.image
        if (Array.isArray(imgs)) return imgs
        return imgs.toString().split(',').filter(u => u)
      } catch (e) {
        return []
      }
    },
    refresh() {
      this.getList()
    },
    handleClose() {
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-content {
  min-height: 300px;
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.order-info {
  font-weight: bold;
  font-size: 14px;
}

.empty-record {
  padding: 40px 0;
  text-align: center;
  color: #909399;
}

.empty-record i {
  font-size: 40px;
  margin-bottom: 15px;
}

.image-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
</style>
