<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('kefufeedback.反馈时间')" prop="cretim">
        <el-date-picker clearable
          v-model="queryParams.cretim"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('kefufeedback.请选择反馈时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('kefufeedback.用户id')" prop="userId">
        <el-input
          v-model="queryParams.userId"
          :placeholder="$t('kefufeedback.请输入反馈用户id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('kefufeedback.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('kefufeedback.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:feedback:remove']"
        >{{$t('kefufeedback.删除')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:feedback:export']"
        >{{$t('kefufeedback.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="feedbackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id"  width="100"/>
      <el-table-column :label="$t('kefufeedback.反馈时间')" align="center" prop="cretim"/>
      <el-table-column :label="$t('kefufeedback.反馈用户')" align="center" prop="user">
        <template slot-scope="scope">
          <div>{{scope.row.user.nickName}}</div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('kefufeedback.问题内容')" align="center" prop="content" />
      <el-table-column :label="$t('kefufeedback.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:feedback:edit']"
          >{{$t('kefufeedback.回复问题')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:feedback:remove']"
          >{{$t('kefufeedback.删除')}}</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"
    />

    <!-- 添加或修改feedback对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'165px'">
        <el-form-item :label="$t('kefufeedback.反馈时间')" prop="cretim">
          <div>{{form.cretim}}</div>
        </el-form-item>
        <el-form-item :label="$t('kefufeedback.反馈用户')" prop="userId">
          <div>{{form.userId}}</div>
        </el-form-item>
        <el-form-item :label="$t('kefufeedback.问题内容')" prop="userId">
          <div v-html="form.content"></div>
        </el-form-item>
        <el-divider content-position="center">{{$t('kefufeedback.回复反馈内容')}}</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSysResponse">{{$t('kefufeedback.回复')}}</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteSysResponse">{{$t('kefufeedback.删除')}}</el-button>
          </el-col>
        </el-row>
        <el-table :data="sysResponseList" :row-class-name="rowSysResponseIndex" @selection-change="handleSysResponseSelectionChange" ref="sysResponse">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column :label="$t('kefufeedback.回复内容')" prop="content">
            <template slot-scope="scope">
               <el-input
                 type="textarea"
                 :rows="2"
                 :placeholder="$t('kefufeedback.请输入回复内容')"
                 v-model="scope.row.content">
               </el-input>
            </template>
          </el-table-column>

        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('kefufeedback.确定')}}</el-button>
        <el-button @click="cancel">{{$t('kefufeedback.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFeedback, getFeedback, delFeedback, addFeedback, updateFeedback } from "@/api/system/feedback";

export default {
  name: "Feedback",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedSysResponse: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // feedback表格数据
      feedbackList: [],
      // response表格数据
      sysResponseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cretim: null,
        userId: null,
        content: null,
        user:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: this.$t('kefufeedback.反馈用户不能为空'), trigger: "blur" }
        ],
        content: [
          { required: true, message: this.$t('kefufeedback.问题内容不能为空'), trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询feedback列表 */
    getList() {
      this.loading = true;
      listFeedback(this.queryParams).then(response => {
        console.log('问题列表',response)
        this.feedbackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        cretim: null,
        userId: null,
        content: null,
        user:null
      };
      this.sysResponseList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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
      getFeedback(id).then(response => {
        this.form = response.data;
        this.sysResponseList = response.data.sysResponseList;
        this.open = true;
        this.title = this.$t('kefufeedback.回复反馈');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.sysResponseList = this.sysResponseList;
          if (this.form.id != null) {
            updateFeedback(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('kefufeedback.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addFeedback(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('kefufeedback.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('kefufeedback.是否确认删除编号为') + ids + this.$t('kefufeedback.的数据项')).then(function() {
        return delFeedback(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('kefufeedback.删除成功'));
      }).catch(() => {});
    },
	/** response序号 */
    rowSysResponseIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** response添加按钮操作 */
    handleAddSysResponse() {
      let obj = {};
      obj.cretim = "";
      obj.content = "";
      this.sysResponseList.push(obj);
    },
    /** response删除按钮操作 */
    handleDeleteSysResponse() {
      if (this.checkedSysResponse.length == 0) {
        this.$modal.msgError(this.$t('kefufeedback.请先选择要删除的回复数据'));
      } else {
        const sysResponseList = this.sysResponseList;
        const checkedSysResponse = this.checkedSysResponse;
        this.sysResponseList = sysResponseList.filter(function(item) {
          return checkedSysResponse.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleSysResponseSelectionChange(selection) {
      this.checkedSysResponse = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/feedback/export', {
        ...this.queryParams
      }, `feedback_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
