<template>
  <div class="container"  style="padding: 30px!important; ">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="86px"
    >
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否企业" prop="companyStatus">
        <el-select v-model="queryParams.companyStatus">
          <el-option label="请选择" value="">请选择</el-option>
          <el-option label="是" value="2">是</el-option>
          <el-option label="否" value="0">否</el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否测试者" prop="testerStatus">
        <el-select v-model="queryParams.testerStatus">
          <el-option label="请选择" value="">请选择</el-option>
          <el-option label="是" value="2">是</el-option>
          <el-option label="否" value="0">否</el-option>
        </el-select>
      </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"
          v-hasPermi="['system:UserList:add']"
          >创建新用户</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:UserList:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:UserList:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:UserList:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="UserListList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column
        label="注册时间"
        align="center"
        prop="loginDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否企业" align="center" prop="isCompany" />
      <el-table-column label="认证企业名" align="center" prop="companyName" />
      <el-table-column label="是否测试者" align="center" prop="isTester" />
      <el-table-column label="认证测试者" align="center" prop="cur" />
      <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-key"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:UserList:edit']"
            >查看详情</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:UserList:edit']"
            >修改</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"
    />

    <!-- 添加或修改用户列表对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门ID" />
        </el-form-item>
        <el-form-item label="用户账号" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户账号" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户类型" prop="userType">
          <el-input v-model="form.userType" placeholder="请输入用户类型" />
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入用户邮箱" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-input v-model="form.sex" placeholder="请输入用户性别" />
        </el-form-item>
        <el-form-item label="头像地址" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像地址" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="帐号状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入帐号状态" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="最后登录IP" prop="loginIp">
          <el-input v-model="form.loginIp" placeholder="请输入最后登录IP" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="loginDate">
          <el-date-picker
            clearable
            size="small"
            v-model="form.loginDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后登录时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="备注" prop="companyStatus">
          <el-input v-model="form.companyStatus" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="备注" prop="testerStatus">
          <el-input v-model="form.testerStatus" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看详情对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="detailOpen"
      width="700px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-tabs v-model="activeTab">
        <el-tab-pane label="基本资料" name="baseInfo">
          <baseInfo :user="user" />
        </el-tab-pane>
        <el-tab-pane label="认证信息" name="certificationInfo">
          <certificationInfo
            :user="user"
            :cResponse="cResponse"
            v-if="userGetFlag"
          />
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUserList,
  getUserList,
  delUserList,
  addUserList,
  updateUserList,
  exportUserList,
} from "@/api/system/UserList";
import baseInfo from "./baseInfo";
import certificationInfo from "./certificationInfo";
import { getCertifications } from "@/api/system/user";

export default {
  name: "UserList",
  components: { baseInfo, certificationInfo },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户列表表格数据
      UserListList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 最后登录时间时间范围
      daterangeLoginDate: [],
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 更新时间时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        loginIp: null,
        loginDate: null,
        companyStatus: null,
        testerStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" },
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
      },
      activeTab: "baseInfo",
      detailOpen: false,
      user: {},
      cResponse: {},
      userGetFlag: false,
    };
  },
  created() {
    this.getList();
    if (this.$route.params && this.$route.params.activeTab)
      this.activeTab = this.$route.params.activeTab;
  },
  methods: {
    /** 查询用户列表列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeLoginDate && "" != this.daterangeLoginDate) {
        this.queryParams.params["beginLoginDate"] = this.daterangeLoginDate[0];
        this.queryParams.params["endLoginDate"] = this.daterangeLoginDate[1];
      }
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && "" != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] =
          this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listUserList(this.queryParams).then((response) => {
        this.UserListList = this.makeATStatus(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    makeATStatus(responseRows) {
      if (responseRows && responseRows.length > 0) {
        var newArr = responseRows.map(function (item, idnex) {
          item.isCompany = item.companyStatus == 2 ? "是" : "否";
          item.isTester = item.testerStatus == 2 ? "是" : "否";

          if (item.companyStatus == 2) {
            if (item.testerStatus == 2) {
              item.cur = "测试公司";
            } else {
              item.cur = "";
            }
          } else {
            if (item.testerStatus == 2) {
              item.cur = "个人测试者";
            } else {
              item.cur = "";
            }
          }
          return item;
        });
        return newArr;
      } else {
        return responseRows;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        delFlag: null,
        loginIp: null,
        loginDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        companyStatus: null,
        testerStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeLoginDate = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      getUserList(userId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户列表";
      });
    },
    /** 查看详情操作 */
    handleDetail(row) {
      this.userGetFlag = false;
      this.reset();
      const userId = row.userId || this.ids;
      this.user = row;
      getCertifications(userId).then((response) => {
        this.cResponse = response;
        this.userGetFlag = true;
        this.detailOpen = true;
      });
      this.title = "查看详情";
    },
    /** 查询所有认证信息 */
    getCertificationList(uId) {},
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.userId != null) {
            updateUserList(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUserList(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$confirm(
        '是否确认删除用户列表编号为"' + userIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delUserList(userIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有用户列表数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportUserList(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
