<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 class="inp" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" @keyup.enter.native="handleQuery" clearable class="inp" />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="queryParams.idCard" placeholder="请输入身份证" @keyup.enter.native="handleQuery" clearable class="inp" />
      </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:user:remove']">删除{{ ids.length > 0 ? '(' + ids.length + ')' : '' }}</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 label="姓名" align="center" prop="name" width="110" />
      <el-table-column label="性别" align="center" prop="sex" width="80" />
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="手机号" align="center" prop="phone" width="130" />
      <el-table-column label="实名认证" align="center" prop="isAuthentication" width="130">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isAuthentication == 0">未实名</el-tag>
          <el-tag type="success" v-if="scope.row.isAuthentication == 1">已实名</el-tag>
          <el-tag type="danger" v-if="scope.row.isAuthentication == 2">实名不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="签署合同" align="center" prop="isContract" width="130">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isContract == 0">未签署</el-tag>
          <el-tag type="success" v-if="scope.row.isContract == 1">已签署</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="关联企业数" align="center" prop="isCompany" width="120" />
      <el-table-column label="账号状态" align="center" prop="state" width="100">
        <template slot-scope="scope">
          <div class="switch">
            <el-switch v-model="scope.row.state" :active-value="0" :width="50" :inactive-value="1" @change="op('change', scope.row)"></el-switch>
            <span class="zc" v-if="scope.row.state == 0">正常</span>
            <span class="ty" v-else>禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="注册日期" align="center" prop="createTime" width="160" />
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-document" @click="op('contract', scope.row)" v-if="scope.row.isContract == 1">查看合同</el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="op('detail', scope.row)" v-hasPermi="['work:user:list']">详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="del(scope.row)" v-hasPermi="['work:user: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>
import edit from './edit';
import contract from './contract';
export default {
  name: 'Users',
  data() {
    return {
      ids: [],
      showSearch: true,
      response: {},
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        phone: null,
        idCard: null,
        state: null,
        orderByColumn: 'id',
        isAsc: 'desc'
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.ajax({ url: '/work/user/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) {
      const id = row.id || this.ids[0];
      if (tag == 'detail') {
        this.iframe({ obj: edit, param: { id: id, detail: true }, title: '查看详情', width: '55%', height: '60%' });
      }
      if (tag == 'contract') {
        this.iframe({ obj: contract, param: { id: id }, title: '查看合同', width: '55%', height: '85%' });
      }
      //账号状态
      if (tag == 'change') {
        let text = row.state === 0 ? '启用' : '禁用';
        this.$confirm('确认要' + text + '该用户吗？', '警告', { type: 'warning' })
          .then(() => {
            this.post({ url: '/work/user/edit', data: { id: row.id, state: row.state } })
              .then((response) => {
                this.$modal.msgSuccess(text + '成功');
              })
              .catch(() => {
                row.state = row.state === 0 ? 1 : 0;
              });
          })
          .catch(() => {
            row.state = row.state === 0 ? 1 : 0;
          });
      }
    },
    del(row) {
      this.$confirm('是否确认删除选中数据?', '警告', { type: 'warning' }).then(() => {
        this.get({ url: '/work/user/remove/' + (row.id || this.ids) }).then((response) => {
          this.$modal.msgSuccess('删除成功');
          this.getList();
        });
      });
    }
  }
};
</script>
