<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系方式" prop="concat">
        <el-input
          v-model="queryParams.concat"
          placeholder="请输入联系方式"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="待审核" value="SUBMIT"/>
          <el-option label="审核通过" value="PASS"/>
          <el-option label="审核未通过" value="NO_PASS"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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 border v-loading="loading" :data="commentList" @selection-change="handleSelectionChange">
      <el-table-column label="编号" align="center" prop="id" width="55"/>
      <el-table-column label="评论人名称" align="center" prop="name"/>
      <el-table-column label="评论内容" align="center" prop="content" show-overflow-tooltip="true"/>
      <el-table-column label="联系方式" align="center" prop="concat"/>
      <!--      <el-table-column label="评论的类型：PICTURE图片故事，GALLERY图库" align="center" prop="type"/>-->
      <el-table-column label="状态" align="center" prop="status">
        <template scope="scope">
          {{ getStatusName(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <!--      <el-table-column label="评论的外键，图片故事评论的话，就图片故事id，图库的话就图库id" align="center" prop="fkId"/>-->
      <el-table-column label="评论者IP" align="center" prop="ip"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:comment:edit']"
            v-if="scope.row.status == 'SUBMIT'"
          >审核
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleDel(scope.row)"
          >删除
          </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 :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评论人" prop="name">
          <el-input v-model="form.name" placeholder="请输入评论人名称" readonly/>
        </el-form-item>
        <el-form-item label="评论内容">
          <el-input v-model="form.content" :min-height="192" readonly type="textarea"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="concat">
          <el-input v-model="form.concat" placeholder="请输入联系方式" readonly/>
        </el-form-item>
        <el-form-item label="状态">
          <el-input :value="getStatusName(form.status)" readonly/>
        </el-form-item>
        <el-form-item label="主题" prop="fkId">
          <el-input v-model="form.title" placeholder="" readonly/>
        </el-form-item>
        <el-form-item label="评论者IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入评论者IP" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="showEdit">
        <el-button type="primary" @click="submitForm('PASS')" size="small">通过</el-button>
        <el-button type="warning" @click="submitForm('NO_PASS')" size="small">不通过</el-button>
        <el-button @click="cancel" size="small">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {approvalComment, getComment, listComment, delComment} from "@/api/system/comment";
  import Editor from '@/components/Editor';

  export default {
    name: "Comment",
    components: {Editor},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 评论信息表格数据
        commentList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          content: null,
          concat: null,
          type: 'PICTURE',
          status: 'SUBMIT',
          fkId: null,
          ip: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        showEdit: false
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询评论信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.type = 'PICTURE';
        listComment(this.queryParams).then(response => {
          this.commentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          name: null,
          content: null,
          concat: null,
          type: 'PICTURE',
          status: 'SUBMIT',
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          ip: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.queryParams.type = 'PICTURE';
        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
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getComment(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改评论信息";
          this.showEdit = true;
        });
      }, /** 修改按钮操作 */
      handleAdd(row) {
        this.reset();
        const id = row.id || this.ids
        getComment(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看评论信息";
          this.showEdit = false;
        });
      },
      /** 提交按钮 */
      submitForm(status) {
        let formData = new FormData();
        formData.append("id", this.form.id);
        formData.append("status", status);

        approvalComment(formData).then(response => {
          this.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      },

      getStatusName(status) {
        let statusInfo = {
          SUBMIT: '待审核',
          PASS: '审核通过',
          NO_PASS: '审核未通过'
        };
        return statusInfo[status];
      },
      handleDel(row) {
        console.log(row);
        this.$confirm('确定删除该评论吗？', '警告').then(() => {
          delComment(row.id).then(() => {
            this.$message.success("删除成功！");
            this.handleQuery();
          }).catch((err) => {
            this.$message.error("删除失败！");
          })
        });
      }
    }
  };
</script>
