<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="售后编号" prop="complaintNo">
        <el-input v-model="queryParams.complaintNo" placeholder="请输入售后编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="执行状态" prop="executeStatus">
        <el-select v-model="queryParams.executeStatus" placeholder="请选择" clearable>
          <el-option label="待执行反馈" :value="0" />
          <el-option label="已反馈完成" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" border>
      <el-table-column label="售后编号" align="center" width="150">
        <template slot-scope="scope">
          {{ (scope.row.acceptInfo || {}).complaintNo || '' }}
        </template>
      </el-table-column>
      <el-table-column label="投诉日期" align="center" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime((scope.row.acceptInfo || {}).complaintDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行部门" align="center" prop="deptId" width="100">
        <template slot-scope="scope">
          {{ getDeptName(scope.row.deptId) }}
        </template>
      </el-table-column>
      <el-table-column label="执行状态" align="center" prop="executeStatus" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.executeStatus === 0" type="warning">待执行</el-tag>
          <el-tag v-else-if="scope.row.executeStatus === 1" type="success">已反馈</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="执行结果" align="center" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <div v-html="scope.row.executeResult"></div>
        </template>
      </el-table-column>
      <el-table-column label="反馈时间" align="center" prop="feedbackTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.feedbackTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.executeStatus === 0" size="mini" type="text" icon="el-icon-edit" @click="handleExecute(scope.row, true)">执行反馈</el-button>
          <el-button v-else size="mini" type="text" icon="el-icon-view" @click="handleExecute(scope.row, false)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog :title="(isEditable ? '执行反馈 - ' : '查看反馈 - ') + getDeptLabel()" :visible.sync="execOpen" width="900px" append-to-body :close-on-click-modal="false">
      <div v-loading="detailLoading" class="exec-dialog">
        <HeaderControlSection
          :complaintNo="acceptDetail.complaintNo"
          :flowStatus="acceptDetail.flowStatus"
          :meta="acceptDetail"
        >
          <template #basic-info>
            <BasicInfoSection :data="acceptDetail" />
          </template>
        </HeaderControlSection>

        <el-divider />

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

        <ContractInfoSection :coilList="dialogCoilList" />

        <div v-if="acceptDetail.planContent">
          <div class="section-title">处理方案</div>
          <div class="plan-content" v-html="acceptDetail.planContent"></div>
        </div>

        <div v-if="isEditable">
          <div class="section-title">填写执行反馈</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>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">{{ isEditable ? '取 消' : '关 闭' }}</el-button>
        <el-button v-if="isEditable" :loading="submitLoading" type="primary" @click="submitExecute">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "AftermarketTodo",
  components: { HeaderControlSection, BasicInfoSection, CoilInfoSection, ContractInfoSection },
  data() {
    return {
      loading: false,
      detailLoading: false,
      submitLoading: false,
      showSearch: true,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        complaintNo: undefined,
        executeStatus: undefined
      },
      dataList: [],
      execOpen: false,
      isEditable: false,
      acceptDetail: {},
      dialogCoilList: [],
      coilLoading: false,
      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 };
      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.dataList = rows;
        this.total = response.total;
      }).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();
    },
    handleExecute(row, editable) {
      this.currentRel = row;
      this.isEditable = editable;
      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 (!editable) {
        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;
      }
      this.execOpen = true;
    },
    closeDialog() {
      this.execOpen = false;
      this.isEditable = 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.execOpen = false;
        this.isEditable = false;
        this.getList();
      }).finally(() => { this.submitLoading = false; });
    }
  }
};
</script>

<style scoped>
.exec-dialog {
  max-height: 65vh;
  overflow-y: auto;
}
.exec-dialog /deep/ .section-title {
  margin-top: 16px;
}
.section-title {
  font-size: 15px;
  font-weight: 600;
  color: #1d2129;
  margin: 16px 0 12px 0;
  padding: 0 0 10px 0;
  border-bottom: 2px solid transparent;
  border-image: linear-gradient(to right, #c0c4cc, transparent) 1;
  white-space: nowrap;
}
.section-title:first-child {
  margin-top: 0;
}
.plan-content {
  padding: 12px;
  background: #f5f7fa;
  border-radius: 4px;
  font-size: 13px;
  line-height: 1.6;
}
.empty-data {
  color: #909399;
  font-size: 13px;
  padding: 8px 0;
}
</style>
