<template>
  <div class="app-container">
    <!-- 当vue的搜索框form表单有且只有一个输入框时，点击回车，会触发刷新整个页面，
      可使用 @submit.native.prevent 阻止页面刷新 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      @submit.native.prevent
    >
      <el-form-item label="姓名">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入姓名"
          clearable
          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">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableData" :row-class-name="getClass">
      <el-table-column label="编号" prop="customerId" align="center" />
      <el-table-column
        label="姓名"
        prop="customerName"
        align="center"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="手机号"
        prop="customerTel"
        align="center"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="公司名称" prop="customerCompany" align="center" />
      <el-table-column label="公司邮箱" prop="customerEmail" align="center" />
      <el-table-column label="职位" prop="customerPosition" align="center" />

      <el-table-column label="是否订阅资讯" align="center">
        <template slot-scope="scope">
          <el-switch
            disabled
            v-model="scope.row.subscribe"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</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-view"
            @click="handleLook(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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"
    />

    <!-- 详情弹窗 width="500px"-->
    <el-dialog :title="title" :visible.sync="dlgVisible" append-to-body>
      <Detail
        :type="type"
        :loading="dlgLoading"
        :infoData="infoData"
        @save="handleSave"
        @cancel="handleCancel"
      ></Detail>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  editCustomerMsg,
  editCustomerMsgStatus,
  delCustomerMsg,
} from "@/api/userDemandManage";

import Detail from "./components/detail.vue";

export default {
  name: "UserDemandManage",
  components: { Detail },
  dicts: [],
  data() {
    return {
      // 是否展示筛选项
      showSearch: true,
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerName: "",
      },
      // 数据量
      total: 0,
      // 表格数据
      tableData: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 弹窗相关
      dlgVisible: false,
      title: "",
      infoData: {},
      dlgLoading: false,
      type: "1",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 给表格行添加样式
    getClass({ row, rowIndex }) {
      if (row.status !== "1") {
        return "unReadClass";
      }
      return "";
    },
    // 搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        customerName: "",
      };
      this.getList();
    },
    // 查询列表
    getList() {
      this.loading = true;
      getList(this.queryParams)
        .then((res) => {
          console.log(res);
          this.tableData = res.rows;
          this.total = res.total;
        })
        .finally(() => (this.loading = false));
    },
    // 新增
    handleAdd() {
      this.dlgVisible = true;
      this.title = "新增";
      this.infoData = {};
    },
    // 查看
    handleLook(row) {
      this.dlgVisible = true;
      this.title = "详情";
      this.dlgLoading = true;
      this.type = "1";
      getDetail(row.customerId)
        .then((res) => {
          console.log(res);
          this.infoData = res.data;
        })
        .finally(() => (this.dlgLoading = false));
      this.changeStatus(row);
    },
    // 修改
    handleUpdate(row) {
      this.dlgVisible = true;
      this.title = "编辑";
      this.dlgLoading = true;
      this.type = "2";
      getDetail(row.customerId)
        .then((res) => {
          console.log(res);
          this.infoData = res.data;
        })
        .finally(() => (this.dlgLoading = false));
      this.changeStatus(row);
    },
    // 确认新增/修改
    handleSave(data) {
      this.dlgLoading = true;
      editCustomerMsg(data)
        .then((res) => {
          this.$message.success(res.msg);
          this.handleCancel();
          this.getList();
        })
        .finally(() => (this.dlgLoading = false));
    },
    // 关闭弹窗
    handleCancel() {
      this.dlgVisible = false;
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除数据")
        .then(function () {
          return delCustomerMsg(row.customerId);
        })
        .then((res) => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 修改状态(是否已读)
    changeStatus(row) {
      if (row.status === "1") return;
      let params = {
        customerIds: row.customerId,
        status: "1",
      };
      editCustomerMsgStatus(params).then(() => this.getList());
    },
  },
};
</script>
