<template>
  <div class="app-container objection-container">
    <DragResizePanel :initialSize="280" :minSize="280" :maxSize="600">
      <template #panelA>
        <div class="left-panel">
          <div class="panel-header">
            <div class="header-title">
              <i class="el-icon-s-data"></i>
              <span>意见汇总</span>
              <el-button size="mini" type="text" icon="el-icon-refresh" @click="getList" style="margin-left:4px;"
                title="刷新列表"></el-button>
            </div>
            <el-select v-model="queryParams.flowStatus" placeholder="全部阶段" clearable size="mini" @change="handleQuery"
              class="header-filter">
              <el-option label="待汇总方案" :value="3" />
              <el-option label="全部办结" :value="4" />
            </el-select>
          </div>

          <div class="search-row">
            <el-input v-model="queryParams.complaintNo" placeholder="搜索售后编号..." clearable prefix-icon="el-icon-search"
              size="small" @keyup.enter.native="handleQuery" @clear="handleQuery" />
          </div>

          <div v-loading="loading" class="list-body">
            <div v-for="item in dataList" :key="item.acceptId" class="list-item"
              :class="{ active: currentRow && currentRow.acceptId === item.acceptId }" @click="handleRowClick(item)">
              <div class="item-main">
                <span class="item-title">{{ item.complaintNo }}</span>
                <span class="item-sub">{{ parseTime(item.complaintDate, '{y}-{m}-{d}') }}</span>
              </div>
              <div class="item-meta">
                <el-tag v-if="item.flowStatus === 3" size="mini">待汇总方案</el-tag>
                <el-tag v-else-if="item.flowStatus === 4" type="success" size="mini">全部办结</el-tag>
              </div>
            </div>
            <div v-if="dataList.length === 0 && !loading" class="list-empty">
              <i class="el-icon-folder-opened"></i>
              <span>暂无待汇总的售后单</span>
            </div>
          </div>

          <div class="list-footer">
            <pagination :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
              @pagination="getList" />
          </div>
        </div>
      </template>

      <template #panelB>
        <div class="right-panel">
          <div v-if="!currentRow" class="empty-tip">
            <i class="el-icon-info"></i>
            <span>请在左侧列表中选择一条售后单查看详情</span>
          </div>
          <div v-else v-loading="detailLoading" class="detail-content">
            <HeaderControlSection :complaintNo="currentRow.complaintNo" :flowStatus="currentRow.flowStatus"
              :meta="currentRow">
              <template #actions>
                <el-button v-if="currentRow.flowStatus === 3" size="mini" type="success" plain
                  icon="el-icon-circle-check" :loading="completeLoading" @click="handleComplete">办 结</el-button>
                <el-button v-if="currentRow.flowStatus === 4" size="mini" type="primary" plain
                  icon="el-icon-download" @click="handleExportPdf">导出</el-button>
                <el-button size="mini" type="text" icon="el-icon-refresh" @click="handleRefreshDetail"
                  title="刷新详情">刷新</el-button>
              </template>

              <template #basic-info>
                <BasicInfoSection :data="currentRow" />
              </template>

              <template #flow-overview>
                <FlowOverviewSection :flowStatus="currentRow.flowStatus" />
              </template>
            </HeaderControlSection>

            <el-divider />

            <CoilInfoSection :coilList="coilList" :loading="coilLoading" :editable="false"
              @refresh="loadCoilList(currentRow.acceptId)" />

            <div class="section-gap" />
            <ContractInfoSection :coilList="coilList" />

            <div class="section-gap" />
            <DepartmentOpinionSection :taskList="taskList" @refresh="refreshTaskList" />

            <div class="section-gap" />
            <HandlingSchemeSection :content="currentRow.planContent" :editable="currentRow.flowStatus === 3" @save="handleSavePlan" />
          </div>
        </div>
      </template>
    </DragResizePanel>

    <ExportPdfDialog ref="exportPdfDialog" />
  </div>
</template>

<script>
import { listComplaintAccept, getComplaintAccept, updateComplaintAccept } from "@/api/flow/complaintAccept";
import { listComplaintTask } from "@/api/flow/complaintTask";
import { listAcceptCoilRel } from "@/api/flow/acceptCoilRel";
import DragResizePanel from "@/components/DragResizePanel/index.vue";
import HeaderControlSection from "./components/HeaderControlSection.vue";
import BasicInfoSection from "./components/BasicInfoSection.vue";
import ContractInfoSection from "./components/ContractInfoSection.vue";
import CoilInfoSection from "./components/CoilInfoSection.vue";
import DepartmentOpinionSection from "./components/DepartmentOpinionSection.vue";
import HandlingSchemeSection from "./components/HandlingSchemeSection.vue";
import FlowOverviewSection from "./components/FlowOverviewSection.vue";
import ExportPdfDialog from "./components/ExportPdfDialog.vue";

export default {
  name: "AftermarketSummary",
  components: {
    DragResizePanel,
    HeaderControlSection, BasicInfoSection, ContractInfoSection,
    CoilInfoSection, DepartmentOpinionSection, HandlingSchemeSection, FlowOverviewSection,
    ExportPdfDialog
  },
  dicts: ['coil_quality_status'],
  data() {
    return {
      loading: true,
      detailLoading: false,
      coilLoading: false,
      completeLoading: false,
      total: 0,
      dataList: [],
      currentRow: null,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        complaintNo: undefined,
        flowStatus: 3
      },
      coilList: [],
      taskList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listComplaintAccept(this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleRowClick(row) {
      this.currentRow = row;
      this.loadDetail(row.acceptId);
    },
    loadDetail(acceptId) {
      this.detailLoading = true;
      getComplaintAccept(acceptId).then(response => {
        this.currentRow = response.data;
        this.loadRelData(acceptId);
      }).finally(() => { this.detailLoading = false; });
    },
    loadRelData(acceptId) {
      this.loadCoilList(acceptId);
      listComplaintTask({ acceptId, pageNum: 1, pageSize: 999 }).then(r => {
        this.taskList = r.rows || [];
      });
    },
    loadCoilList(acceptId) {
      this.coilLoading = true;
      listAcceptCoilRel({ acceptId, pageNum: 1, pageSize: 999 }).then(r => {
        this.coilList = r.rows || [];
      }).finally(() => { this.coilLoading = false; });
    },
    handleRefreshDetail() {
      if (this.currentRow && this.currentRow.acceptId) {
        this.loadDetail(this.currentRow.acceptId);
      }
    },
    handleSavePlan(planContent) {
      updateComplaintAccept({ acceptId: this.currentRow.acceptId, planContent }).then(() => {
        this.$modal.msgSuccess("处理方案保存成功");
        this.loadDetail(this.currentRow.acceptId);
      });
    },
    refreshTaskList() {
      if (this.currentRow && this.currentRow.acceptId) {
        listComplaintTask({ acceptId: this.currentRow.acceptId, pageNum: 1, pageSize: 999 }).then(r => {
          this.taskList = r.rows || [];
        });
      }
    },
    handleComplete() {
      this.$modal.confirm('确认将此售后单办结？办结后部门将无法再修改意见。').then(() => {
        this.completeLoading = true;
        return updateComplaintAccept({ acceptId: this.currentRow.acceptId, flowStatus: 4 });
      }).then(() => {
        this.$modal.msgSuccess("已办结");
        this.completeLoading = false;
        this.loadDetail(this.currentRow.acceptId);
        this.getList();
      }).catch(() => { this.completeLoading = false; });
    },
    handleExportPdf() {
      this.$refs.exportPdfDialog.open(this.currentRow.acceptId);
    }
  }
};
</script>

<style scoped>
.objection-container {
  height: calc(100vh - 84px);
}

.left-panel {
  display: flex;
  flex-direction: column;
  height: 100%;
  background: #f5f7fa;
  border-right: 1px solid #e4e7ed;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 8px;
  background: #f5f7fa;
}

.header-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: 600;
  color: #303133;
}

.header-title i {
  color: #409eff;
  font-size: 16px;
}

.header-filter {
  width: 130px;
}

.search-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 14px 10px;
  background: #f5f7fa;
}

.list-body {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px;
}

.list-item {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  margin-bottom: 2px;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.15s;
}

.list-item:hover {
  background: #ebeef5;
}

.list-item.active {
  background: #d9ecff;
}

.list-item.active .item-title {
  color: #409eff;
  font-weight: 600;
}

.item-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.item-title {
  font-size: 13px;
  font-weight: 500;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.item-sub {
  font-size: 12px;
  color: #909399;
}

.item-meta {
  flex-shrink: 0;
  margin: 0 8px;
}

.list-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 0;
  color: #c0c4cc;
  font-size: 13px;
  gap: 8px;
}

.list-empty i {
  font-size: 32px;
}

.list-footer {
  border-top: 1px solid #e4e7ed;
  padding: 2px 8px 0;
  background: #f5f7fa;
}

/* ========== 右侧面板 ========== */
.right-panel {
  height: 100%;
  overflow-y: auto;
  padding: 12px 16px;
  background: #faf8f5;
}

.right-panel .detail-content {
  margin: 0 auto;
  background: #ffffff;
  padding: 28px 32px 36px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06), 0 2px 12px rgba(0,0,0,0.04);
  min-height: 100%;
}

.empty-tip {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #909399;
  font-size: 14px;
  gap: 8px;
}

.section-title {
  font-family: 'Georgia', 'Times New Roman', 'Noto Serif SC', 'SimSun', serif;
  width: 100%;
  font-size: 15px;
  font-weight: 700;
  color: #1a1a1a;
  margin: 22px 0 12px 0;
  padding: 0 0 10px 0;
  border-bottom: 1px solid #d4d0c8;
  display: flex;
  align-items: center;
  gap: 10px;
  letter-spacing: 0.3px;
}

.section-title:first-child {
  margin-top: 0;
}

.section-gap {
  height: 16px;
}

.plan-content {
  padding: 12px 16px;
  background: #faf8f5;
  border: 1px solid #e8e4de;
  border-radius: 2px;
  font-size: 13px;
  line-height: 1.8;
  color: #1a1a1a;
}

.empty-data {
  color: #8c8c8c;
  font-size: 13px;
  padding: 8px 0;
  font-style: italic;
}

/* 正式表格覆写 */
.right-panel .el-table {
  border: 1px solid #e8e4de !important;
  border-radius: 2px !important;
  font-size: 12px !important;
}

.right-panel .el-table thead th {
  background-color: #2c3e50 !important;
  color: #ffffff !important;
  font-weight: 600 !important;
  font-size: 11px !important;
  letter-spacing: 0.5px !important;
  border-bottom: none !important;
  font-family: 'Georgia', 'Times New Roman', serif;
}

.right-panel .el-table thead th .cell {
  color: #ffffff !important;
}

.right-panel .el-table__body tr:hover > td {
  background-color: #f7f5f0 !important;
}

.right-panel .el-table--border td {
  border-right: 1px solid #f0ece6 !important;
}

.right-panel .el-table--border th {
  border-right: 1px solid #3a5166 !important;
}

.right-panel .el-table td {
  padding: 6px 4px !important;
  color: #3a3a3a !important;
}

.right-panel .el-divider--horizontal {
  margin: 8px 0 4px;
  background-color: #e0dcd6;
}

.right-panel .el-tag {
  border-radius: 2px;
  font-family: 'Georgia', 'Times New Roman', serif;
  letter-spacing: 0.3px;
}

.right-panel .el-tag--mini {
  padding: 0 6px;
  line-height: 20px;
  height: 20px;
}

.right-panel .el-tag--small {
  padding: 0 8px;
}
</style>
