<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
<!--      <el-form-item label="留言标题" prop="question">-->
<!--        <el-input v-model="queryParams.question" placeholder="请输入留言标题" clearable @keyup.enter.native="handleQuery" />-->
<!--      </el-form-item>-->
      <el-form-item label="留言人姓名" prop="ansName">
        <el-input v-model="queryParams.ansName" placeholder="请输入留言人姓名" clearable @keyup.enter.native="handleQuery" />
      </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" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:LEAVEMESSAGE:edit']">回复</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:LEAVEMESSAGE:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="LEAVEMESSAGEList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="留言人姓名" align="center" prop="ansName" />
<!--      <el-table-column label="留言标题" align="center" prop="question" :show-overflow-tooltip="true" />-->
      <el-table-column label="留言详情" align="center" prop="questionDetail" :show-overflow-tooltip="true" />
      <el-table-column label="状态" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0"><el-tag type="danger">未回复</el-tag></span>
          <span v-if="scope.row.type == 1"><el-tag>已回复</el-tag></span>
        </template>
      </el-table-column>
      <el-table-column label="留言时间" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}-{h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}-{h}:{i}') }}</span>
        </template>
      </el-table-column>
      <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" @click="handleUpdate(scope.row)" v-hasPermi="['business:LEAVEMESSAGE:edit']">回复</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:LEAVEMESSAGE: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="750px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="留言标题" prop="questionDetail">
              <el-input v-model="form.questionDetail" placeholder="请输入链接" type="textarea" :disabled="true" autosize />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="留言详情" prop="questionDetail">
              <el-input v-model="form.questionDetail" placeholder="请输入链接" type="textarea" :disabled="true" autosize />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="回复内容" prop="replyContent">
              <template slot-scope="scope">
                <el-input v-if="form.type == 0" v-model="form.replyContent" type="textarea" rows="5" placeholder="请输入内容" />
                <el-input v-else-if="form.type == 1" disabled v-model="form.replyContent" rows="5" type="textarea" />
              </template>
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listLEAVEMESSAGE, getLEAVEMESSAGE, delLEAVEMESSAGE, addLEAVEMESSAGE, updateLEAVEMESSAGE } from '@/api/portal/LEAVEMESSAGE/LEAVEMESSAGE';

export default {
  name: 'LEAVEMESSAGE',
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留言反馈表格数据
      LEAVEMESSAGEList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        userId: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        deptId: null,
        question: null,
        questionDetail: null,
        questionTime: null,
        ansName: null
      },
      // 表单参数
      form: {
        replyContent: null
      },
      // 表单校验
      rules: {
        question: [{ required: true, message: '留言标题不能为空', trigger: 'blur' }],
        questionDetail: [{ required: true, message: '留言详情不能为空', trigger: 'blur' }],
        questionTime: [{ required: true, message: '留言时间不能为空', trigger: 'blur' }],
        ansName: [{ required: true, message: '留言人姓名不能为空', trigger: 'blur' }],
        replyContent: [{ required: true, message: '回复内容不能为空', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询留言反馈列表 */
    getList() {
      this.loading = true;
      listLEAVEMESSAGE(this.queryParams).then(response => {
        this.LEAVEMESSAGEList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        delFlag: null,
        deptId: null,
        question: null,
        questionDetail: null,
        questionTime: null,
        ansName: null,
        replyContent: null
      };
      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;
      getLEAVEMESSAGE(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) {
            this.form.type = 1;
            updateLEAVEMESSAGE(this.form).then(response => {
              this.$modal.msgSuccess('回复成功');
              this.open = false;
              this.getList();
            });
          } else {
            addLEAVEMESSAGE(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除留言反馈编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLEAVEMESSAGE(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'business/LEAVEMESSAGE/export',
        {
          ...this.queryParams
        },
        `LEAVEMESSAGE_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
