<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      v-show="showSearch"
      :inline="true"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理状态" prop="name">
        <el-select
          v-model="queryParams.status"
          placeholder="用户状态"
          clearable
          size="small"
          style="width: 240px"
          @clear="delete queryParams.status"
        >
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="需求内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入需求内容"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          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">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      
    </el-row> -->
    <right-toolbar
      :showSearch.sync="showSearch"
      @queryTable="getList"
    ></right-toolbar>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="数据编号" prop="id" align="center" />
      <el-table-column
        label="需求内容"
        prop="content"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="姓名" prop="name" align="center" />
      <el-table-column label="单位" prop="company" align="center" />
      <el-table-column label="联系方式" prop="mobile" align="center" />
      <el-table-column label="邮箱" prop="email" align="center" />
      <el-table-column label="提交网页地址" prop="area_domain" align="center" />
      <el-table-column label="处理状态" prop="status" align="center">
        <template slot-scope="scope">
          {{
            statusOptions.find((item) => item.value == scope.row.status).label
          }}
        </template>
      </el-table-column>
      <el-table-column
        label="处理结果"
        prop="handle_content"
        align="center"
        show-overflow-tooltip
      />
      <el-table-column label="处理时间" prop="handle_created_at" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handle_created_at) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="(form = scope.row), (open = true)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="scope.row.status != 2"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >处理</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.page_num"
      :limit.sync="queryParams.page_size"
      @pagination="getList"
    />
    <el-dialog title="查看" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" label-width="100px" disabled>
        <el-form-item label="姓名:">
          <el-input v-model="form.name" />
        </el-form-item>
        <el-form-item label="单位:">
          <el-input v-model="form.company" />
        </el-form-item>
        <el-form-item label="联系方式:">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item label="邮箱:">
          <el-input v-model="form.email" />
        </el-form-item>
        <el-form-item label="网页地址:">
          <el-input v-model="form.area_domain" />
        </el-form-item>
        <el-form-item label="需求内容:">
          <el-input
            v-model="form.content"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
          />
        </el-form-item>
        <el-form-item label="处理状态:">
          <el-select v-model="form.status">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理结果:">
          <el-input
            v-model="form.handle_content"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 6 }"
          />
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {
  messageList,
  messageDelete,
  messageHandle,
} from "@/api/website/message";
export default {
  data() {
    return {
      loading: true,
      showSearch: true,
      statusOptions: [
        {
          label: "未处理",
          value: 1,
        },
        {
          label: "已处理",
          value: 2,
        },
      ],
      queryParams: {
        name: undefined,
        status: undefined,
        content: undefined,
        page_num: 1,
        page_size: 10,
      },
      total: 0,
      dataList: [],
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
    };
  },
  methods: {
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleQuery() {
      this.queryParams.page_num = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      messageList(this.queryParams).then((res) => {
        this.dataList = res.data.data;
        this.total = res.data.count;
        this.loading = false;
      });
    },
    handleUpdate(row) {
      this.$prompt('请输入数据编号为"' + row.id + '"的处理结果', "处理", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputType: "textarea",
        // inputPattern: /^.{5,20}$/,
        // inputErrorMessage: "长度必须介于 5 和 20 之间",
      })
        .then(({ value }) => {
          messageHandle({ id: row.id, content: value }).then((response) => {
            this.getList();
            this.$modal.msgSuccess("处理成功");
          });
        })
        .catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除编号为"' + row.id + '"的数据项？')
        .then(function () {
          return messageDelete({ id: row.id });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
  created() {
    this.getList();
  },
};
</script>