<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-promotion"></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.executeStatus" placeholder="执行状态" clearable size="mini" @change="handleQuery"
              class="header-filter">
              <el-option label="待执行反馈" :value="0" />
              <el-option label="已反馈完成" :value="1" />
            </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.relId" class="list-item"
              :class="{ active: currentRel && currentRel.relId === item.relId }" @click="handleRowClick(item)">
              <div class="item-main">
                <span class="item-title">{{ (item.acceptInfo || {}).complaintNo || '' }}</span>
                <span class="item-sub">{{ getDeptName(item.deptId) }} · {{ parseTime((item.acceptInfo || {}).complaintDate, '{y}-{m}-{d}') }}</span>
              </div>
              <div class="item-meta">
                <el-tag v-if="item.rejectMark === 1" type="danger" size="mini">已驳回</el-tag>
                <el-tag v-else-if="item.rejectMark === 2" type="info" size="mini">已隐藏</el-tag>
                <el-tag v-else-if="item.executeStatus === 0" type="warning" size="mini">待执行</el-tag>
                <el-tag v-else-if="item.executeStatus === 1" type="success" size="mini">已反馈</el-tag>
              </div>
              <div class="item-actions">
                <el-button v-if="item.executeStatus === 0" size="mini" type="text" icon="el-icon-edit" @click.stop="handleRowClick(item)"></el-button>
                <el-button v-else size="mini" type="text" icon="el-icon-view" @click.stop="handleRowClick(item)"></el-button>
              </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="!currentRel.relId" class="empty-tip">
            <i class="el-icon-info"></i>
            <span>请在左侧列表中选择一条任务查看详情</span>
          </div>
          <div v-else v-loading="detailLoading" class="detail-content">
            <HeaderControlSection
              :complaintNo="acceptDetail.complaintNo"
              :flowStatus="acceptDetail.flowStatus"
              :meta="acceptDetail"
            >
              <template #actions>
                <el-button size="mini" type="text" icon="el-icon-refresh" @click="refreshDetail" title="刷新">刷新</el-button>
              </template>

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

            <el-divider />

            <FlowOverviewSection :flowStatus="acceptDetail.flowStatus" />

            <CoilInfoSection :coilList="dialogCoilList" :loading="coilLoading" :editable="false" />

            <ContractInfoSection :coilList="dialogCoilList" />

            <div v-if="acceptDetail.planContent" class="section-gap" />
            <div v-if="acceptDetail.planContent">
              <div class="section-title">
                <span>处理方案 <span class="en-sub">· Handling Scheme</span></span>
              </div>
              <div class="plan-content" v-html="acceptDetail.planContent"></div>
            </div>

            <div v-if="isEditable" class="section-gap" />
            <div v-if="isEditable" class="exec-form-wrapper">
              <div class="section-title">
                <span>填写执行反馈 <span class="en-sub">· Execution Feedback</span></span>
              </div>
              <el-form ref="execForm" :model="execForm" label-width="100px">
                <el-form-item label="执行结果" prop="executeResult">
                  <editor v-model="execForm.executeResult" :min-height="200" />
                </el-form-item>
                <el-form-item label="反馈文件">
                  <file-upload v-model="execForm.feedbackFile" />
                </el-form-item>
              </el-form>
              <div class="form-actions">
                <el-button :loading="rejectLoading" type="danger" @click="handleReject">驳 回</el-button>
                <el-button :loading="submitLoading" type="primary" @click="submitExecute">提 交</el-button>
              </div>
            </div>
          </div>
        </div>
      </template>
    </DragResizePanel>
  </div>
</template>

<script>
import { listPlanExecuteRel, getPlanExecuteRel, updatePlanExecuteRel } from "@/api/flow/planExecuteRel";
import { listAcceptCoilRel } from "@/api/flow/acceptCoilRel";
import { feedbackReject } from "@/api/flow/complaintAccept";
import DragResizePanel from "@/components/DragResizePanel/index.vue";
import HeaderControlSection from "./components/HeaderControlSection.vue";
import BasicInfoSection from "./components/BasicInfoSection.vue";
import CoilInfoSection from "./components/CoilInfoSection.vue";
import ContractInfoSection from "./components/ContractInfoSection.vue";
import FlowOverviewSection from "./components/FlowOverviewSection.vue";

export default {
  name: "AftermarketTodo",
  components: { DragResizePanel, HeaderControlSection, BasicInfoSection, CoilInfoSection, ContractInfoSection, FlowOverviewSection },
  data() {
    return {
      loading: false,
      detailLoading: false,
      coilLoading: false,
      submitLoading: false,
      rejectLoading: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        complaintNo: undefined,
        executeStatus: undefined
      },
      dataList: [],
      isEditable: false,
      acceptDetail: {},
      dialogCoilList: [],
      execForm: { executeResult: '', feedbackFile: '' },
      currentRel: {}
    };
  },
  created() {
    this.queryParams.deptId = this.$route.query.deptId != null ? Number(this.$route.query.deptId) : undefined;
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      const params = { ...this.queryParams, rejectMark: 0 };
      if (params.executeStatus === '' || params.executeStatus === undefined) {
        delete params.executeStatus;
      }
      if (params.deptId === '' || params.deptId === undefined || params.deptId === null) {
        delete params.deptId;
      }
      let complaintNoFilter = params.complaintNo;
      delete params.complaintNo;
      listPlanExecuteRel(params).then(response => {
        let rows = response.rows || [];
        if (complaintNoFilter) {
          rows = rows.filter(row => {
            const no = ((row.acceptInfo || {}).complaintNo || '');
            return no.indexOf(complaintNoFilter) !== -1;
          });
        }
        this.total = response.total;
        this.dataList = rows;
      }).finally(() => { this.loading = false; });
    },
    getDeptName(deptId) {
      const map = { 1: '生产部', 2: '质量部', 3: '销售部' };
      return map[deptId] || '部门' + deptId;
    },
    getDeptLabel() {
      const map = { 1: '生产部', 2: '质量部', 3: '销售部' };
      return map[this.currentRel.deptId] || '';
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleRowClick(row) {
      this.currentRel = row;
      this.isEditable = row.executeStatus === 0;
      this.acceptDetail = row.acceptInfo || {};
      this.execForm = { executeResult: '', feedbackFile: '' };
      this.dialogCoilList = [];
      this.detailLoading = true;
      this.coilLoading = true;

      listAcceptCoilRel({ acceptId: row.acceptId, pageNum: 1, pageSize: 999 }).then(r => {
        this.dialogCoilList = r.rows || [];
      }).finally(() => { this.coilLoading = false; });

      if (!this.isEditable) {
        getPlanExecuteRel(row.relId).then(response => {
          const rel = response.data || {};
          this.execForm = {
            executeResult: rel.executeResult || '',
            feedbackFile: rel.feedbackFile || ''
          };
        }).finally(() => { this.detailLoading = false; });
      } else {
        this.detailLoading = false;
      }
    },
    refreshDetail() {
      if (!this.currentRel.acceptId) return;
      this.detailLoading = true;
      this.coilLoading = true;
      listAcceptCoilRel({ acceptId: this.currentRel.acceptId, pageNum: 1, pageSize: 999 }).then(r => {
        this.dialogCoilList = r.rows || [];
      }).finally(() => { this.coilLoading = false; this.detailLoading = false; });
    },
    handleReject() {
      this.$prompt('请输入驳回原因', '反馈驳回', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputType: 'textarea',
        inputValidator: (val) => val ? true : '驳回原因不能为空'
      }).then(({ value }) => {
        this.rejectLoading = true;
        return feedbackReject(this.currentRel.relId, value);
      }).then(() => {
        this.$modal.msgSuccess("驳回成功");
        this.getList();
        this.currentRel = {};
      }).catch(() => { }).finally(() => { this.rejectLoading = false; });
    },
    submitExecute() {
      if (!this.execForm.executeResult) {
        this.$modal.msgWarning('请填写执行结果');
        return;
      }
      this.submitLoading = true;
      updatePlanExecuteRel({
        relId: this.currentRel.relId,
        acceptId: this.currentRel.acceptId,
        deptId: this.currentRel.deptId,
        executeResult: this.execForm.executeResult,
        feedbackFile: this.execForm.feedbackFile,
        executeStatus: 1,
        feedbackTime: this.parseTime(new Date(), '{y}-{m}-{d} {h}:{i}:{s}')
      }).then(() => {
        this.$modal.msgSuccess("执行反馈提交成功");
        this.getList();
        this.currentRel = {};
      }).finally(() => { this.submitLoading = false; });
    }
  }
};
</script>

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

/* ========== 左侧面板（复用 index.vue 风格） ========== */
.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: 120px;
}

.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;
}

.item-actions {
  flex-shrink: 0;
  opacity: 0;
  transition: opacity 0.15s;
}

.list-item:hover .item-actions {
  opacity: 1;
}

.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;
}

/* ========== 右侧面板（复用 index.vue 风格） ========== */
.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 与 index.vue 统一 */
.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-title .en-sub {
  font-size: 11px;
  font-weight: 400;
  color: #8c8c8c;
  letter-spacing: 0.5px;
  font-family: 'Georgia', 'Times New Roman', serif;
  font-style: italic;
}

.section-gap {
  height: 16px;
}

/* ========== 执行反馈填写区 ========== */
.exec-form-wrapper {
  padding-top: 4px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 0 0;
  border-top: 1px solid #e0dcd6;
  margin-top: 12px;
}

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

/* el-divider 样式与 index.vue 统一 */
.right-panel .el-divider--horizontal {
  margin: 8px 0 4px;
  background-color: #e0dcd6;
}
</style>
