<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page="page"
      ref="crud"
      v-model="form"
      :permission="permissionList"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          v-if="permission.complaint_add"
          @click="handleAdd"
        >
          新增投诉
        </el-button>
        <el-button
          type="warning"
          size="small"
          icon="el-icon-edit"
          :disabled="selectionList.length !== 1"
          v-if="permission.complaint_edit"
          @click="handleBatchStatus"
        >
          批量处理
        </el-button>
      </template>

      <template slot="status" slot-scope="{row}">
        <el-tag
          :type="getComplaintStatusType(row.status)"
          size="small"
        >
          {{ getComplaintStatusLabel(row.status) }}
        </el-tag>
      </template>

      <template slot="replyStatus" slot-scope="{row}">
        <el-tag
          :type="getReplyStatusType(row.replyStatus)"
          size="small"
        >
          {{ getReplyStatusLabel(row.replyStatus) }}
        </el-tag>
      </template>

      <template slot="complainantType" slot-scope="{row}">
        <el-tag
          :type="getComplainantTypeType(row.complainantType)"
          size="small"
        >
          {{ getComplainantTypeLabel(row.complainantType) }}
        </el-tag>
      </template>

      <template slot="menu" slot-scope="{row}">
        <el-button
          type="text"
          size="small"
          icon="el-icon-view"
          @click="handleDetail(row)"
        >
          详情
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-chat-line-round"
          @click="handleReplyList(row)"
        >
          回复列表
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-edit"
          v-if="permission.complaint_edit && isComplaintEditable(row.status)"
          @click="handleEdit(row)"
        >
          编辑
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-check"
          v-if="isComplaintProcessable(row.status)"
          @click="handleProcess(row)"
        >
            修改状态
        </el-button>
        <el-button
          type="text"
          size="small"
          icon="el-icon-close"
          v-if="permission.complaint_edit && isComplaintClosable(row.status)"
          @click="handleClose(row)"
        >
          关闭
        </el-button>
      </template>
    </avue-crud>

    <!-- 投诉详情对话框 -->
    <el-dialog
      title="投诉详情"
      :visible.sync="detailVisible"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
      :destroy-on-close="true"
      class="complaint-detail-dialog"
    >
      <div v-if="detailData" class="complaint-detail">
        <el-row :gutter="20">
          <el-col :span="12">
            <div class="detail-item">
              <label>投诉单号：</label>
              <span>{{ detailData.complaintNo }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>投诉标题：</label>
              <span>{{ detailData.title }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <div class="detail-item">
              <label>投诉人类型：</label>
              <span>{{ getComplainantTypeLabel(detailData.complainantType) }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>投诉类型：</label>
              <span>{{ detailData.complaintType }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <div class="detail-item">
              <label>联系人：</label>
              <span>{{ detailData.contactName }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>联系电话：</label>
              <span>{{ detailData.contactPhone }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" v-if="detailData.customerName">
          <el-col :span="12">
            <div class="detail-item">
              <label>客户名称：</label>
              <span>{{ detailData.customerName }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>客户编码：</label>
              <span>{{ detailData.customerCode }}</span>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <div class="detail-item">
              <label>投诉状态：</label>
              <el-tag :type="getComplaintStatusType(detailData.status)" size="small">
                {{ getComplaintStatusLabel(detailData.status) }}
              </el-tag>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>回复状态：</label>
              <el-tag :type="detailData.replyStatus === 1 ? 'success' : 'info'" size="small">
                {{ detailData.replyStatus === 1 ? '已回复' : '未回复' }}
              </el-tag>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="24">
            <div class="detail-item">
              <label>投诉内容：</label>
              <div class="content-text">{{ detailData.content }}</div>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20" v-if="detailData.closeReason">
          <el-col :span="24">
            <div class="detail-item">
              <label>关闭原因：</label>
              <div class="content-text">{{ detailData.closeReason }}</div>
            </div>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <div class="detail-item">
              <label>提交时间：</label>
              <span>{{ detailData.submitTime }}</span>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="detail-item">
              <label>更新时间：</label>
              <span>{{ detailData.updateTime }}</span>
            </div>
          </el-col>
        </el-row>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="detailVisible = false">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 回复列表对话框 -->
    <el-dialog
      title="回复列表"
      :visible.sync="replyListVisible"
      width="1000px"
      append-to-body
      :close-on-click-modal="false"
      :destroy-on-close="true"
      class="reply-list-dialog"
    >
      <div class="reply-list-header">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          v-if="currentComplaintStatus !== COMPLAINT_STATUS.CLOSED && currentComplaintStatus !== COMPLAINT_STATUS.CANCELLED"
          @click="handleAddReply"
        >
          新增回复
        </el-button>
      </div>

      <el-table
        :data="replyList"
        v-loading="replyListLoading"
        border
        stripe
        style="margin-top: 15px;"
      >
        <el-table-column label="回复类型" width="100" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="getReplyTypeType(scope.row.replyType)"
              size="small"
            >
              {{ getReplyTypeLabel(scope.row.replyType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="回复内容" prop="replyContent" min-width="300">
          <template slot-scope="scope">
            <div class="reply-content">
              {{ scope.row.replyContent }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="回复人" prop="replierName" width="120" align="center"></el-table-column>
        <el-table-column label="回复时间" prop="replyTime" width="160" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="160" align="center"></el-table-column>
      </el-table>

      <el-pagination
        v-if="replyTotal > 0"
        @size-change="handleReplyPageSizeChange"
        @current-change="handleReplyPageChange"
        :current-page="replyPage.current"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="replyPage.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="replyTotal"
        style="margin-top: 15px; text-align: right;"
      >
      </el-pagination>

      <div slot="footer" class="dialog-footer">
        <el-button @click="replyListVisible = false">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 新增回复对话框 -->
    <el-dialog
      title="新增回复"
      :visible.sync="addReplyVisible"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
      :destroy-on-close="true"
    >
      <el-form
        :model="replyForm"
        :rules="replyRules"
        ref="replyForm"
        label-width="100px"
      >
        <el-form-item label="回复类型" prop="replyType">
          <el-select v-model="replyForm.replyType" placeholder="请选择回复类型" style="width: 100%">
            <el-option
              v-for="item in replyTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回复内容" prop="replyContent">
          <el-input
            type="textarea"
            :rows="5"
            v-model="replyForm.replyContent"
            placeholder="请输入回复内容"
            maxlength="1000"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item label="附件URL" prop="replyAttachUrl">
          <el-input
            v-model="replyForm.replyAttachUrl"
            placeholder="请输入附件URL（可选）"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="addReplyVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmAddReply" :loading="addReplyLoading">确定</el-button>
      </div>
    </el-dialog>

    <!-- 状态处理对话框 -->
    <el-dialog
      :title="statusDialogTitle"
      :visible.sync="statusVisible"
      append-to-body
      width="500px"
      :close-on-click-modal="false"
    >
      <el-form :model="statusForm" :rules="statusRules" ref="statusForm" label-width="100px">
        <el-form-item label="新状态" prop="status">
          <el-select v-model="statusForm.status" placeholder="请选择状态" style="width: 100%">
            <el-option label="待处理" :value="0"></el-option>
            <el-option label="处理中" :value="1"></el-option>
            <el-option label="已回复" :value="2"></el-option>
            <el-option label="已关闭" :value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关闭原因" prop="closeReason" v-if="statusForm.status === COMPLAINT_STATUS.CLOSED">
          <el-input
            type="textarea"
            :rows="3"
            v-model="statusForm.closeReason"
            placeholder="请输入关闭原因"
          ></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="statusVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmStatusUpdate" :loading="statusLoading">确定</el-button>
      </div>
    </el-dialog>
  </basic-container>
</template>

<script>
import complaintMixin from './complaintMixin';

export default {
    mixins: [complaintMixin],
}

</script>

<style lang="scss" scoped>
.complaint-detail {
  .detail-item {
    margin-bottom: 15px;

    label {
      font-weight: bold;
      color: #606266;
      margin-right: 10px;
    }

    .content-text {
      margin-top: 5px;
      padding: 10px;
      background-color: #f5f7fa;
      border-radius: 4px;
      line-height: 1.6;
      white-space: pre-wrap;
    }
  }
}

.reply-list-dialog {
  .reply-list-header {
    display: flex;
    justify-content: flex-end;
    margin-bottom: 10px;
  }

  .reply-content {
    word-break: break-word;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
  }

  .el-table {
    font-size: 14px;

    .el-table__cell {
      padding: 12px 0;
    }
  }
}
::v-deep .el-col-md-8 { 
  width: 24.33333%;
}
</style>
