<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="goodsId">
        <el-select v-model="queryParams.goodsId" placeholder="请选择商品" clearable @change="handleQuery" filterable>
          <el-option v-for="(item, index) in goodsList" :key="index" :label="item.goodsName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="phoneNumber" label-width="120">
        <el-input style="width: 150px" v-model="queryParams.phoneNumber" placeholder="请输入客户手机号" clearable
                  @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-date-picker style="width: 250px" clearable v-model="queryCreateTime" type="daterange"
                        value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"
                        end-placeholder="结束日期" @change="getCreateTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="是否显示评论" prop="isShow" label-width="120">
        <el-radio-group v-model="queryParams.isShow">
          <el-radio-button label="Y">是</el-radio-button>
          <el-radio-button label="N">否</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否虚拟评论" prop="isVirtual" label-width="120">
        <el-radio-group v-model="queryParams.isVirtual">
          <el-radio-button label="Y">是</el-radio-button>
          <el-radio-button label="N">否</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="评分类型" prop="commentType">
        <el-radio-group v-model="queryParams.commentType">
          <el-radio-button label="0">好评</el-radio-button>
          <el-radio-button label="1">差评</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @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" @click="handleAdd" v-hasPermi="['goods:goodsComment:add']">
          新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain @click="handleShow">显示评论</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain @click="handleNoShow">隐藏评论</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <Page uri="/mapi/goods/goodsComment/list" :request-params="queryParams" ref="pagination" :selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed="left" />
      <el-table-column label="订单编号" align="center" prop="orderId" width="120" />
      <el-table-column label="关联商品" align="left" prop="skulist">
        <template slot-scope="scope">
          <div v-for="(sku_n, sku_i) in scope.row.skuList" :key="sku_i">
            <span style="color: #000">{{ sku_n.goodsName }}：</span>{{ sku_n.skuName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="评分" align="center" prop="commentScore" width="80" />
      <el-table-column label="评分类型 " align="center" prop="commentType" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.commentType == '0'" type="success">好评</el-tag>
          <el-tag v-if="scope.row.commentType == '1'" type="danger">差评</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="评论文案" align="center" prop="commentContent" show-overflow-tooltip />
      <el-table-column label="评论图片" align="center" prop="commentImgs">
        <template slot-scope="scope">
          <el-image v-for="(item, index) in getimgList(scope.row.commentImgs)" :key="index"
                    style="width: 50px; height: 50px" :src="item"
                    :preview-src-list="getimgList(scope.row.commentImgs)"></el-image>
          <!-- <image-preview :src="scope.row.commentImgs" :width="50" :height="50"/> -->
        </template>
      </el-table-column>
      <el-table-column label="评论来源" align="center" prop="commentSrcType" width="120" />
      <el-table-column label="是否显示评论" align="center" prop="isShow" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isShow == 'Y'">是</el-tag>
          <el-tag v-if="scope.row.isShow == 'N'" type="warning">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否虚拟评论" align="center" prop="isVirtual" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isVirtual == 'Y'">是</el-tag>
          <el-tag v-if="scope.row.isVirtual == 'N'" type="warning">否</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" @click="editreplyComment(scope.row)"
                     v-if="scope.row.isVirtual == 'Y'">评论编辑
          </el-button>
          <el-button type="text" @click="handleShow2(scope.row)" v-if="scope.row.isShow == 'N'">显示评论</el-button>
          <el-button type="text" @click="handleNoShow2(scope.row)" v-if="scope.row.isShow == 'Y'">隐藏评论</el-button>
          <el-button type="text" icon="el-icon-edit" @click="replyComment(scope.row)">评论回复</el-button>
          <el-button type="text" icon="el-icon-tickets" @click="showReplyComment(scope.row)">查看回复</el-button>
        </template>
      </el-table-column>
    </Page>

    <!-- 添加或修改商品评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" size="30%" append-to-body>
      <el-row :gutter="15">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-col :span="24">
            <el-form-item label="商品" prop="goodsId">
              <el-select v-model="form.goodsId" placeholder="请选择商品" clearable @change="getSkuList(form.goodsId)"
                         :disabled="form.id ? true : false" filterable>
                <el-option v-for="(item, index) in goodsList" :key="index" :label="item.goodsName" :value="item.id" />
              </el-select>
            </el-form-item>
            <el-form-item label="物料" prop="goodsSkuId">
              <el-select v-model="form.goodsSkuId" placeholder="请选择物料" clearable :disabled="form.id ? true : false"
                         filterable>
                <el-option v-for="(item, index) in skuList" :key="index" :label="item.goodsSkuName"
                           :value="item.goodsSkuId" />
              </el-select>
            </el-form-item>
            <el-form-item label="评分1-5分" prop="commentScore">
              <el-input-number v-model="form.commentScore" :min="1" :max="5" :step="1" />
            </el-form-item>
            <el-form-item label="评分类型 " prop="commentType">
              <el-radio-group v-model="form.commentType">
                <el-radio-button label="0">好评</el-radio-button>
                <el-radio-button label="1">差评</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="评论文案" prop="commentContent">
              <el-input v-model="form.commentContent" type="textarea" placeholder="请输入评论文案" :rows="5"
                        maxlength="255" show-word-limit />
            </el-form-item>
            <el-form-item label="评论图片">
              <image-upload v-model="form.commentImgs" :limit="9" />
            </el-form-item>
            <el-form-item label="是否显示" prop="isShow">
              <el-radio-group v-model="form.isShow">
                <el-radio-button label="Y">是</el-radio-button>
                <el-radio-button label="N">否</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--  评论回复  -->
    <el-dialog :title="replayCommentTitle" :visible.sync="addreplayCommentOpen" width="30%" :before-close="handleClose">
      <form ref="replayForm">
        <el-input type="textarea" :rows="5" placeholder="请输入回复内容" v-model="replayForm.commentContent"></el-input>
      </form>
      <span slot="footer" class="dialog-footer">
                <el-button @click="addreplayCommentOpen = false">取 消</el-button>
                <el-button type="primary" @click="addreplayCommentSubmit('replayForm')">提 交</el-button>
            </span>
    </el-dialog>

    <!-- 查询评论回复 -->
    <el-dialog title="回复列表" :visible.sync="ReplyCommentType" size="45%" append-to-body>
      <div style="padding: 20px">
        <el-table v-loading="ReplyCommentLoading" :data="ReplyCommentList">
          <el-table-column label="回复角色" align="center" prop="userId" width="120">
            <template slot-scope="scope">
              {{ scope.row.userId ? '用户' : '总部' }}
            </template>
          </el-table-column>
          <el-table-column label="回复来源" align="center" prop="commentSrcType" width="120" />
          <el-table-column label="回复文案" align="center" prop="commentContent" show-overflow-tooltip />
          <el-table-column label="回复时间" align="center" prop="createTime" width="180" />
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="100">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-edit" :disabled="scope.row.userId ? true : false"
                         @click="commentUpdate(scope.row)">修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="ReplyCommenttotal > 0" :total="ReplyCommenttotal" :page.sync="queryCommentReply.pageNum"
                    :limit.sync="queryCommentReply.pageSize" @pagination="getReplyCommentList" />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button style="margin-left: 5%" @click="ReplyCommentType = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listGoodsComment,
  getGoodsComment,
  delGoodsComment,
  addGoodsComment,
  updateGoodsComment,
  updateGoodsCommentStatus,
  noShowComment,
  showComment,
  getCommentgood,
  getCommentSku,
  getCommentReply,
  getQueryCommentReply
} from '@/api/core/goodsComment'

export default {
  name: 'GoodsComment',
  dicts: ['sys_normal_disable', 'sys_yes_no', 'sys_job_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评论表格数据
      goodsCommentList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        goodsSkuId: null,
        userId: null,
        goodsId: null,
        commentScore: null,
        commentType: null,
        commentContent: null,
        commentImgs: null,
        commentSrcType: null,
        isShow: null,
        isVirtual: null,
        phoneNumber: null,
        createTimeFrom: '',
        createTimeTo: ''
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsId: [{ required: true, message: '商品不能为空', trigger: 'change' }],
        goodsSkuId: [{ required: true, message: '物料不能为空', trigger: 'change' }],
        commentScore: [{ required: true, message: '评分不能为空', trigger: 'blur' }],
        commentType: [{ required: true, message: '评论类型不能为空', trigger: 'blur' }],
        commentContent: [{ required: true, message: '评论内容不能为空', trigger: 'blur' }],
        isShow: [{ required: true, message: '是否显示不能为空', trigger: 'blur' }]
      },
      goodsType: null,
      goodsList: [],
      skuList: [],
      queryCreateTime: null,
      addreplayCommentOpen: false,
      replayForm: {
        // usercomment: '',
        // commentImgs: '',
        commentContent: '',
        goodsCommentId: ''
      },
      replayrules: {
        commentContent: [{ required: true, message: '评论回复不能为空', trigger: 'blur' }]
      },

      replayCommentTitle: '',
      ReplyCommentType: false,
      queryCommentReply: {
        goodsCommentId: 0,
        pageNum: 1,
        pageSize: 15
      },
      ReplyCommentLoading: false,
      ReplyCommenttotal: 0,
      ReplyCommentList: []
    }
  },
  created() {
    this.goodsType = this.getUrlParam('goodsType')
    this.queryParams.goodsType = this.getUrlParam('goodsType')
    this.queryParams.goodsId = this.getUrlParam('goodsId')
    this.getList()
    this.getGoodsList()
  },
  methods: {
    getCreateTime() {
      if (this.queryCreateTime != null) {
        this.queryParams.createTimeFrom = this.queryCreateTime[0]
        this.queryParams.createTimeTo = this.queryCreateTime[1]
      }
    },
    /** 查询商品评论列表 */
    getList() {
      this.$nextTick(() => {
        this.$refs.pagination.handleSearch(true)
      })
    },
    getGoodsList() {
      getCommentgood({
        pageNum: 1,
        pageSize: 999,
        goodsType: this.goodsType
      }).then((response) => {
        this.goodsList = response.data
      })
    },
    getSkuList(id) {
      getCommentSku({
        pageNum: 1,
        pageSize: 999,
        goodsType: this.goodsType,
        goodsId: id
      }).then((response) => {
        this.skuList = response.data
      })
    },
    //处理图片数组
    getimgList(data) {
      return data ? data.split(',') : []
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.getList()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsSkuId: null,
        userId: null,
        orderId: null,
        commentScore: null,
        commentType: null,
        commentContent: null,
        commentImgs: null,
        commentSrcType: null,
        delFlag: null,
        createBy: null,
        createById: null,
        createTime: null,
        updateBy: null,
        updateById: null,
        updateTime: null,
        remark: null,
        isShow: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      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
      getGoodsComment(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改商品评论'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          // console.log(this.form)
          this.form.goodsCommentId = this.form.id
          this.form.goodsType = this.goodsType

          if (this.form.id != null) {
            updateGoodsComment(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addGoodsComment(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 显示按钮操作 */
    handleShow2(row) {
      const ids = []
      ids.push(row.id)
      this.$modal
        .confirm('是否确认将评论在APP显示？')
        .then(function() {
          return showComment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {
        })
    },
    /** 隐藏按钮操作 */
    handleNoShow2(row) {
      const ids = []
      ids.push(row.id)
      this.$modal
        .confirm('是否确认将评论在APP隐藏？')
        .then(function() {
          return noShowComment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {
        })
    },
    /** 显示按钮操作 */
    handleShow() {
      const ids = this.ids
      if (!ids || ids.length <= 0) {
        this.$modal.msgError('请选择需要操作的数据')
        return
      }
      this.$modal
        .confirm('是否确认将评论在APP显示？')
        .then(function() {
          return showComment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {
        })
    },
    /** 隐藏按钮操作 */
    handleNoShow() {
      const ids = this.ids
      if (!ids || ids.length <= 0) {
        this.$modal.msgError('请选择需要操作的数据')
        return
      }
      this.$modal
        .confirm('是否确认将评论在APP隐藏？')
        .then(function() {
          return noShowComment(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('操作成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'mapi/goods/goodsComment/export',
        {
          ...this.queryParams
        },
        `goodsComment_${new Date().getTime()}.xlsx`
      )
    },
    checkClose(done) {
      this.$confirm('是否关闭表单，关闭后数据将丢失？')
        .then(function() {
          done()
        })
        .then(() => {
          this.cancel()
        })
        .catch(() => {
        })
    },
    replyComment(row) {
      // this.replayForm.usercomment = row.commentContent;
      // this.replayForm.commentImgs = row.commentImgs;
      this.replayForm.goodsCommentId = row.id
      this.addreplayCommentOpen = true
      this.replayCommentTitle = '新增评论回复'
    },
    handleClose(done) {
      this.$confirm('是否关闭表单？')
        .then((_) => {
          done()
        })
        .catch((_) => {
        })
    },
    addreplayCommentSubmit(formName) {
      if (!this.replayForm.commentContent) {
        this.$modal.msgError('请输入回复内容')
        return
      }
      getCommentReply({
        goodsCommentId: this.replayForm.goodsCommentId,
        commentContent: this.replayForm.commentContent
      }).then((response) => {
        this.$modal.msgSuccess('回复成功')
        this.addreplayCommentOpen = false
        this.getList()
      }).finally(() => {
        this.replayForm = {
          commentContent: '',
          goodsCommentId: ''
        }
      })
      /*this.$refs[formName].validate((valid) => {
  if (valid) {
    getCommentReply({
      goodsCommentId: this.replayForm.goodsCommentId,
      commentContent: this.replayForm.commentContent
    }).then(response => {
      this.$modal.msgSuccess("回复成功");
      this.addreplayCommentOpen = false;
      this.getList();
    });
  }
});*/
    },
    showReplyComment(row) {
      this.queryCommentReply.goodsCommentId = row.id
      this.ReplyCommentList = []
      this.queryCommentReply.pageNum = 1
      this.ReplyCommentType = true
      this.getReplyCommentList()
    },
    getReplyCommentList() {
      this.ReplyCommentLoading = true
      getQueryCommentReply({
        goodsCommentId: this.queryCommentReply.goodsCommentId,
        pageNum: this.queryCommentReply.pageNum,
        pageSize: this.queryCommentReply.pageSize
      }).then((response) => {
        this.ReplyCommentList = response.rows
        this.ReplyCommenttotal = response.total
        this.ReplyCommentLoading = false
      }).finally(() => {
        this.ReplyCommentLoading = false
      })
    },
    commentUpdate(row) {
      this.replayCommentTitle = '编辑评论回复'
      let objform = Object.assign({}, row)
      this.replayForm.goodsCommentId = objform.goodsCommentId
      this.replayForm.commentContent = objform.commentContent
      this.addreplayCommentOpen = true
    },
    //评论编辑
    async editreplyComment(row) {
      this.getSkuList(row.goodsId)
      this.form = Object.assign({}, row)
      this.form.goodsSkuId = row.skuList[0].goodsSkuId
      this.open = true
      this.title = '编辑商品评论'
    }
  }
}
</script>
