<template>
  <div class="manage-container">
    <div class="manage-button">
      <el-button type="primary" size="small" @click="addData">
        {{ $t('defalult.tj') }}
      </el-button>
      <el-button type="primary" size="small" @click="exportData">
        {{ $t('defalult.dc') }}
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="flowData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="name"
        :label="$t('staff.xm')"
        width="100"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="phone"
        label="手机号"
        width="150"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="company"
        label="公司"
        width="250"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="position"
        label="职位"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="email"
        :label="$t('staff.yx')"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="添加时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column fixed="right" :label="$t('defalult.cz')" align="center">
        <template #default="{ row }">
          <el-button type="text" @click="editData(row)">
            {{ $t('defalult.bj') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ $t('defalult.sc') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageIndex"
      :layout="layout"
      :page-size="queryForm.size"
      :total="total"
      style="text-align: right"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
    <add-data ref="data"></add-data>
  </div>
</template>
<script>
import { getList, doDelete, intentionExport } from '@/api/intention';
import addData from './add';
import ajax from '@/api/download';
export default {
  name: 'Index',
  components: {
    addData,
  },
  data() {
    return {
      flowData: [],
      layout: 'total, sizes, prev, pager, next, jumper',
      total: 0,
      background: true,
      listLoading: true,
      elementLoadingText: '正在加载...',
      queryForm: {
        page: 1,
        size: 20,
      },
    };
  },
  computed: {
    height() {
      return 500;
    },
  },
  created() {
    this.fetchData();
  },
  methods: {
    //导出
    async exportData() {
      const url = await intentionExport([]);
      ajax.downloadFile(url, { fileName: '意向用户.xlsx' });
    },
    async fetchData() {
      this.listLoading = true;
      const { data } = await getList(this.queryForm);
      this.flowData = data.items;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 500);
    },
    // 删除
    handleDelete(row) {
      const that = this;
      if (row.id) {
        this.$confirm('你确定要删除当前项吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const { msg } = doDelete({ customerId: row.id });
            this.$message({
              type: 'success',
              message: msg == undefined ? '删除成功' : msg,
            });
            setTimeout(function () {
              that.fetchData();
            }, 1000);
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            });
          });
      }
    },
    search() {
      this.fetchData();
    },
    resetForm() {
      this.$refs.queryForm.resetFields();
    },
    editData(row) {
      this.$refs['data'].showEdit(row);
    },
    addData(row) {
      this.$refs['data'].showEdit(row);
    },
    handleQuery() {
      this.queryForm.pageIndex = 1;
      this.fetchData();
    },
    handleSizeChange(val) {
      this.queryForm.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.queryForm.pageIndex = val;
      this.fetchData();
    },
  },
};
</script>

<style lang="scss" scoped>
.manage-button {
  margin-bottom: 30px;
}
</style>

<style lang="scss">
.el-submenu__title:hover {
  background-color: rgba(#1890ff, 0.085) !important;
  color: hsla(208, 100%, 55%, 0.95) !important;
}
</style>
