<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="appealList" @selection-change="handleSelectionChange">
      <!-- <el-table-column :label="$t('systemappeal.订单号')" align="center" prop="ddId" /> -->
      <el-table-column :label="$t('systemappeal.昵称')" align="center" prop="nickName" />
      <el-table-column :label="$t('systemappeal.内容')" align="center" prop="content" />
      <el-table-column :label="$t('systemappeal.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:appeal:edit']"
          >{{$t('systemappeal.查看')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBack(scope.row)"
            v-hasPermi="['system:appeal:add']"
          >{{$t('systemappeal.回复')}}</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item :label="$t('systemappeal.订单号')" prop="ddId">
          <el-input v-model="form.ddId" :placeholder="$t('systemappeal.请输入订单号')" disabled />
        </el-form-item>
        <el-form-item v-show="false" label="用户类型" prop="userType">
          <el-select v-model="form.userType" placeholder="请选择用户类型">
            <el-option
              v-for="dict in dict.type.info_user_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('systemappeal.使用情况')" prop="useCase">
          <el-input v-model="form.useCase" :placeholder="$t('systemappeal.使用情况')" />
        </el-form-item>
        <el-form-item :label="$t('systemappeal.昵称')" prop="nickName">
          <el-input v-model="form.nickName" :placeholder="$t('systemappeal.请输入昵称')" />
        </el-form-item>
        <el-form-item :label="$t('systemappeal.内容')" prop="content">
          <el-input v-model="form.content" type="textarea" :placeholder="$t('systemappeal.请输入内容')" />
        </el-form-item>
        <el-form-item :label="$t('systemappeal.上传图片')" prop="imgs">
          <image-upload v-model="form.imgs"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-show="showSubmit" type="primary" @click="submitForm">{{$t('systemappeal.确定')}}</el-button>
        <el-button @click="cancel">{{$t('systemappeal.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAppeal, getAppeal, delAppeal, addAppeal, updateAppeal } from "@/api/system/appeal";
import Bus from '../../../utils/Bus.js'
export default {
  name: "Appeal",
  dicts: ['info_user_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 留言信息表格数据
      appealList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示确定保存按钮
      showSubmit: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ddId: null,
        userType: null,
      },
      // 表单参数
      form: {},
      dd:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    Bus.$on("order",(e)=>{
      this.dd = e;
      console.log('=================',e)
      this.getList(e);
    })
  },
  methods: {
    /** 查询留言信息列表 */
    getList(e) {
      this.loading = true;
      this.queryParams.ddId = e.ddid;
      this.queryParams.userType = e.type;
      listAppeal(this.queryParams).then(response => {
        this.appealList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ddId: null,
        userType: null,
        nickName: null,
        content: null,
        imgs: null,
        feedBack:null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList(this.dd);
    },
    /** 重置按钮操作 */
    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 = this.$t('systemappeal.添加留言信息');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAppeal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('systemappeal.修改留言信息');
      });
    },
    /** 查看按钮操作 */
    handleDetail(row) {
      this.reset();
      this.showSubmit = false;
      const id = row.id || this.ids
      getAppeal(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('systemappeal.查看留言信息');
      });
    },
    /** 回复按钮操作 */
    handleBack(row) {
      this.reset();
      this.showSubmit = true;
      const id = row.id || this.ids
      getAppeal(id).then(response => {
        this.form = response.data;
        this.form.content = "";
        this.form.imgs = "";
         this.form.feedBack = "";
        this.form.id = null;
        this.form.nickName = this.$t('systemappeal.客服');
        this.open = true;
        this.title = this.$t('systemappeal.回复留言信息');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAppeal(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('systemappeal.修改成功'));
              this.open = false;
              this.getList(this.dd);
            });
          } else {
            addAppeal(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('systemappeal.回复成功'));
              this.open = false;
              this.getList(this.dd);
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除留言信息编号为"' + ids + '"的数据项？').then(function() {
        return delAppeal(ids);
      }).then(() => {
        this.getList(this.dd);
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/appeal/export', {
        ...this.queryParams
      }, `appeal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
