<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" @submit.native.prevent v-show="showSearch">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" @keyup.enter.native="handleQuery" clearable />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" @keyup.enter.native="handleQuery" clearable />
      </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-button type="danger" icon="el-icon-delete" :disabled="ids.length == 0" @click="del" v-hasPermi="['work:relate:remove']">解除{{ ids.length > 0 ? '(' + ids.length + ')' : '' }}</el-button>
      <el-button type="primary" icon="el-icon-download" @click="handleExport" v-hasPermi="['work:relate:export']">导出</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table :data="response.rows" border @selection-change="selects" height="calc(100vh - 270px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" align="center" width="80" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="关联方式" align="center" prop="way" />
      <el-table-column label="关联时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['work:relate:remove']">解除</el-button>
        </template>
      </el-table-column>
      <template slot="empty">
        <el-empty></el-empty>
      </template>
    </el-table>
    <pagination v-if="response.total > 0" :total="response.total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
export default {
  name: 'Relate',
  data() {
    return {
      ids: [],
      showSearch: true,
      response: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        orderByColumn: 'r.id',
        isAsc: 'desc'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.ajax({ url: '/work/relate/list', data: this.queryParams }).then((response) => {
        this.response = response;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    selects(rows) {
      this.ids = rows.map((item) => item.id);
    },
    op(tag, row) {
      if (tag == 'add') {
        this.iframe({ obj: edit, param: {}, title: '新增', width: '45%', height: '55%' });
      }
      if (tag == 'edit') {
        const id = row.id || this.ids[0];
        this.iframe({ obj: edit, param: { id: id }, title: '编辑', width: '50%', height: '50%' });
      }
    },
    del(row) {
      this.$confirm('是否确认解除选中关联用户?', '警告', { type: 'warning' }).then(() => {
        this.get({ url: '/work/relate/remove/' + (row.id || this.ids) }).then((response) => {
          this.$modal.msgSuccess('解除成功');
          this.getList();
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/work/relate/export', { ...this.queryParams }, '企业关联用户.xlsx');
    }
  }
};
</script>
