<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="用户名字" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="联系方式" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 添加处理状态筛选 -->
      <el-form-item label="处理状态" prop="isHandle">
        <el-select
          v-model="queryParams.isHandle"
          placeholder="请选择处理状态"
          clearable
          size="small"
        >
          <el-option label="未处理" value="0"></el-option>
          <el-option label="已处理" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="投诉时间" prop="complaintTime">
            <el-date-picker v-model="complaintTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"></el-date-picker>
      </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">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['his:complaint:add']"
        >新增</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['his:complaint:edit']"
        >修改</el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['his:complaint:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:complaint:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="complaintList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户" align="center" prop="userName" />
      <!-- <el-table-column label="联系方式" align="center" prop="phone" /> -->
      <el-table-column label="投诉时间" align="center" prop="createTime" />
      <!-- 修改处理状态列显示 -->
      <el-table-column label="是否处理" align="center" prop="isHandle">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.isHandle == 1 ? 'success' : 'warning'"
            disable-transitions
          >
            {{ formatHandleStatus(scope.row.isHandle) }}
          </el-tag>
        </template>
      </el-table-column>
       <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-hasPermi="['his:complaint:edit']"
            @click="handleUpdate(scope.row)"
          >投诉详情</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['his:complaint:edit']"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:complaint:remove']"
          >删除</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="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item> -->
        <el-form-item label="投诉内容" prop="name">
          <el-input v-model="form.name" :disabled="true" />
        </el-form-item>
        <el-form-item label="详细内容" v-if="form.content && form.content!=''">
          <el-input type="textarea" v-model="form.content" :disabled="true"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone" v-if="form.phone && form.phone!=''">
          <el-input v-model="form.phone"  :disabled="true"/>
        </el-form-item>
        <el-form-item label="图片" v-if="form.urls && form.urls!=''">
          <div v-for="(url, index) in imageUrls(form.urls)" :key="index" class="image-container">
            <el-image :src="url" :preview-src-list="[url]" style="width: 100px; height: 100px;"></el-image>
          </div>
        </el-form-item>
        <el-form-item label="交易截图" v-if="form.tradeImage && form.tradeImage!=''">
          <!-- <ImageUpload v-model="form.tradeImage" type="image" :num="10" :width="150" :height="150" /> -->
          <div v-for="(url, index) in imageUrls(form.tradeImage)" :key="index" class="image-container">
            <el-image :src="url" :preview-src-list="[url]" style="width: 100px; height: 100px;"></el-image>
          </div>
        </el-form-item>
        <el-form-item label="被投诉人信息" prop="account" v-if="form.account && form.account!=''">
          <el-input v-model="form.account"  :disabled="true"/>
        </el-form-item>
        <!-- 修改处理状态为下拉选择 -->
        <el-form-item label="是否处理" prop="isHandle">
          <el-select v-model="form.isHandle" placeholder="请选择处理状态">
            <el-option label="未处理" :value="0"></el-option>
            <el-option label="已处理" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
              <el-input type="textarea" v-model="form.remarks" :maxlength="250" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComplaint, getComplaint, delComplaint, addComplaint, updateComplaint, exportComplaint } from "@/api/his/complaint";

export default {
  name: "Complaint",
  data() {
    return {
      complaintTime:null,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户投诉表格数据
      complaintList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        userName: null, // 添加用户名查询参数
        templateId: null,
        content: null,
        phone: null,
        urls: null,
        account: null,
        isHandle: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        templateId: [
          { required: true, message: "投诉模板id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },

  methods: {
    imageUrls(urls) {
      return String(urls).split(",");
    },
    /**
     * 格式化处理状态显示
     * @param {Number} status - 处理状态 0:未处理 1:已处理
     * @returns {String} - 格式化后的状态文本
     */
    formatHandleStatus(status) {
      return status == 1 ? '已处理' : '未处理';
    },

    /** 查询用户投诉列表 */
    getList() {
      this.loading = true;
      console.log(this.queryParams);
      listComplaint(this.queryParams).then(response => {
        this.complaintList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        templateId: null,
        content: null,
        phone: null,
        urls: null,
        account: null,
        createTime: null,
        isHandle: 0, // 默认设置为未处理
        remarks:null
      };
      this.resetForm("form");
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    
    /** 重置按钮操作 */
    resetQuery() {
      
      this.complaintTime = null;
      this.queryParams.complaintsTime=null;
      this.queryParams.complainteTime=null;
      
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户投诉";
    },
    
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getComplaint(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "投诉详情";
      });
    },
    
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateComplaint(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addComplaint(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户投诉编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delComplaint(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户投诉数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportComplaint(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    changeTime(){
        if(this.complaintTime!=null){
          this.queryParams.complaintsTime=this.complaintTime[0];
          this.queryParams.complainteTime=this.complaintTime[1];
        }else{
          this.queryParams.complaintsTime=null;
          this.queryParams.complainteTime=null;
        }
        

      },
  }
};
</script>