<template>
  <el-drawer
    title="查看进度"
    v-model="visible"
    direction="rtl"
    size="500px"
    :close-on-click-modal="false"
    class="progress-drawer"
    @close="handleClose"
  >
    <div class="progress-container">
      <div class="input-wrapper">
        <el-input
          v-model="progressDesc"
          type="textarea"
          :rows="6"
          placeholder="请输入进度描述"
          maxlength="500"
          show-word-limit
          class="progress-input"
        />
      </div>
      <div class="action-bar">
        <el-button type="primary" icon="Plus" @click="handlePublish" :loading="loading" style="background-color: #409eff; border-color: #409eff;">
          发布
        </el-button>
      </div>

      <div class="record-list" v-loading="recordLoading">
        <div
          v-for="item in recordList"
          :key="item.id"
          class="record-item"
        >
          <div class="record-header">
            <span class="record-user">{{ item.createByName || item.createBy || '' }}</span>
            <span class="record-time">{{ item.createTime || '' }}</span>
          </div>
          <div class="record-content">{{ item.followUpCondition || item.content || '' }}</div>
        </div>
        <el-empty v-if="!recordLoading && (!recordList || recordList.length === 0)" description="暂无进度记录" :image-size="60" />
        <div class="pagination-wrapper" v-if="total > 0">
          <el-pagination
            small
            layout="prev, pager, next"
            :total="total"
            v-model:current-page="queryParams.pageNum"
            v-model:page-size="queryParams.pageSize"
            @current-change="loadRecordList"
          />
        </div>
      </div>
    </div>
  </el-drawer>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';
import { addRecord, listRecord } from '@/api/visit/record';

const visible = ref(false);
const progressDesc = ref('');
const loading = ref(false);
const currentId = ref(null);
const recordLoading = ref(false);
const recordList = ref([]);
const total = ref(0);

const emit = defineEmits(['success']);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  objectNo: undefined
});

const open = (row) => {
  visible.value = true;
  progressDesc.value = '';
  currentId.value = row ? row.id : null;
  if (currentId.value) {
    queryParams.objectNo = String(currentId.value);
    loadRecordList();
  }
};

const handleClose = () => {
  visible.value = false;
  progressDesc.value = '';
  currentId.value = null;
  recordList.value = [];
  total.value = 0;
};

const loadRecordList = async () => {
  if (!queryParams.objectNo) return;
  recordLoading.value = true;
  try {
    const res = await listRecord(queryParams);
    recordList.value = res.rows || [];
    total.value = res.total || 0;
  } catch (err) {
    console.error('加载进度记录失败:', err);
  } finally {
    recordLoading.value = false;
  }
};

const handlePublish = async () => {
  if (!progressDesc.value.trim()) {
    ElMessage.warning('请输入进度描述');
    return;
  }

  loading.value = true;
  try {
    await addRecord({
      objectNo: String(currentId.value),
      followUpCondition: progressDesc.value.trim(),
      dataType: '2'
    });
    ElMessage.success('发布成功');
    progressDesc.value = '';
    queryParams.pageNum = 1;
    loadRecordList();
    emit('success');
  } catch (error) {
    console.error('发布失败:', error);
  } finally {
    loading.value = false;
  }
};

defineExpose({
  open
});
</script>

<style scoped lang="scss">
.progress-drawer {
  :deep(.el-drawer__header) {
    margin-bottom: 0;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 16px;
    color: #333;
  }
  :deep(.el-drawer__body) {
    padding: 20px;
    background-color: #f7f9fb;
  }
}

.progress-container {
  background: transparent;
}

.input-wrapper {
  background: #fff;
  border-radius: 4px;

  .progress-input {
    :deep(.el-textarea__inner) {
      border: 1px solid #e4e7ed;
      border-radius: 4px;
      padding: 10px 15px;
      resize: none;
      box-shadow: none;
      &:focus {
        border-color: #409eff;
      }
    }
    :deep(.el-input__count) {
      background: transparent;
      bottom: 10px;
      right: 15px;
      font-size: 12px;
      color: #909399;
    }
  }
}

.action-bar {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;

  .el-button {
    padding: 8px 20px;
  }
}

.record-list {
  margin-top: 24px;

  .record-item {
    background: #fff;
    border-radius: 4px;
    padding: 14px 18px;
    margin-bottom: 12px;

    .record-header {
      display: flex;
      align-items: center;
      gap: 16px;
      margin-bottom: 8px;

      .record-user {
        font-size: 14px;
        color: #333;
      }

      .record-time {
        font-size: 13px;
        color: #999;
      }
    }

    .record-content {
      font-size: 14px;
      color: #606266;
      line-height: 1.6;
    }
  }

  .pagination-wrapper {
    display: flex;
    justify-content: center;
    margin-top: 16px;
  }
}
</style>
