<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校" prop="school">
        <el-input
          v-model="queryParams.school"
          placeholder="请输入学校"
          clearable
          @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">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['app:user: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="['app:user: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="['app:user: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="['app:user:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="username" />
<!--      <el-table-column label="邮箱" align="center" prop="email" />-->
      <el-table-column
        prop="avatarUrl"
        header-align="center"
        align="center"
        label="头像"
      >
        <template slot-scope="scope" class="demo-image__preview">
          <img
            :src="scope.row.avatarUrl"
            width="40"
            height="40"
            class="head_pic"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否为会员" align="center" prop="isMember" >
        <template slot-scope="scope">
          {{ scope.row.isMember == 0 ? '是' : scope.row.isMember == 1 ? '否' : '未知' }}
        </template>
        </el-table-column>
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          {{ scope.row.sex == 0 ? '男' : scope.row.sex == 1 ? '女' : '未知' }}
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="学校" align="center" prop="schoolName" />
      <el-table-column label="学历" align="center" prop="education" />
      <el-table-column label="专业" align="center" prop="major" />
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
          >
            详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['app:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:user:remove']"
          >删除</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"
    />

    <!-- 添加或修改app用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" 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="nickname">
          <el-input v-model="form.nickname" 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="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="头像" prop="avatarUrl">
          <el-input v-model="form.avatarUrl" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历" />
        </el-form-item>
        <el-form-item label="学校" prop="school">
          <el-input v-model="form.school" placeholder="请输入学校" />
        </el-form-item>
        <el-form-item label="专业" prop="major">
          <el-input v-model="form.major" placeholder="请输入专业" />
        </el-form-item>
        <el-form-item label="学历开始时间" prop="startTime">
          <el-date-picker clearable
                          v-model="form.startTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择学历开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="学历结束时间" prop="endTime">
          <el-date-picker clearable
                          v-model="form.endTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择学历结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="在校经历" prop="experience">
          <el-input v-model="form.experience" placeholder="请输入在校经历" />
        </el-form-item>
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="行业" prop="industry">
          <el-input v-model="form.industry" placeholder="请输入行业" />
        </el-form-item>
        <el-form-item label="在职时间" prop="jobTime">
          <el-date-picker clearable
                          v-model="form.jobTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择在职时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="职位名称" prop="jobName">
          <el-input v-model="form.jobName" placeholder="请输入职位名称" />
        </el-form-item>
        <el-form-item label="技能id" prop="skillId">
          <el-input v-model="form.skillId" placeholder="请输入技能id" />
        </el-form-item>
        <el-form-item label="工作内容">
          <editor v-model="form.jobContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="出生年月" prop="birthday">
          <el-date-picker clearable
                          v-model="form.birthday"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择出生年月">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="共享资源" prop="shareResource">
          <el-input v-model="form.shareResource" placeholder="请输入共享资源" />
        </el-form-item>
        <el-form-item label="是否是技术人员，0是1否" prop="isTech">
          <el-input v-model="form.isTech" placeholder="请输入是否是技术人员，0是1否" />
        </el-form-item>
        <el-form-item label="创业想法" prop="idea">
          <el-input v-model="form.idea" placeholder="请输入创业想法" />
        </el-form-item>
        <el-form-item label="是否是合伙人,0是1否" prop="isPartner">
          <el-input v-model="form.isPartner" placeholder="请输入是否是合伙人,0是1否" />
        </el-form-item>
        <el-form-item label="业余爱好" prop="hobby">
          <el-input v-model="form.hobby" placeholder="请输入业余爱好" />
        </el-form-item>
        <el-form-item label="生活城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入生活城市" />
        </el-form-item>
        <el-form-item label="其他信息" prop="other">
          <el-input v-model="form.other" placeholder="请输入其他信息" />
        </el-form-item>
        <el-form-item label="是否是会员0是1否" prop="isMember">
          <el-input v-model="form.isMember" placeholder="请输入是否是会员0是1否" />
        </el-form-item>
        <el-form-item label="订单记录" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单记录" />
        </el-form-item>
        <el-form-item label="开始时间" prop="orderStartTime">
          <el-date-picker clearable
                          v-model="form.orderStartTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="orderEndTime">
          <el-date-picker clearable
                          v-model="form.orderEndTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择结束时间">
          </el-date-picker>
        </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-drawer
      title="移动用户详情"
      :visible.sync="drawer"
      direction="rtl"
      size="50%"
      class="user-info"
    >
      <el-form :model="userDetail" ref="drawerForm" label-width="100px" >
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称" class="userId">
              <el-tag type="success" >{{ userDetail.username }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号">
              <el-tag type="success">{{ userDetail.phone }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" class="userId">
              <el-tag type="success">  {{ userDetail.sex == 0 ? '男' : userDetail.sex == 1 ? '女' : '未知' }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否会员">
              <el-tag type="success">{{ userDetail.isMember == 0 ? '是' : userDetail.isMember == 1 ? '否' : '未知' }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学校">
              <el-tag type="success">{{ userDetail.schoolName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学历">
              <el-tag type="success">{{ userDetail.education }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="专业" >
              <el-tag type="success">{{ userDetail.major }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册时间">
              <el-tag type="success">{{ userDetail.createTime }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" class="userId">
              <el-tag type="success">{{ userDetail.companyName }}</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技能">
              <el-tag type="success" v-for="skill in userSkillList" :key="skill.id">{{ skill.name }}</el-tag>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="demo-drawer__footer" >
        <el-button @click="drawer = false">关闭</el-button>
        <el-button type="primary" @click="handleDynamicDetail(userDetail.username)" :loading="loading">查看动态</el-button>
      </div>
    </el-drawer>

  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/app/user";

export default {
  name: "User",
  data() {
    return {
      // 抽屉
      drawer: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // app用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        username: null,
        password: null,
        nickname: null,
        email: null,
        phone: null,
        address: null,
        avatarUrl: null,
        education: null,
        school: null,
        major: null,
        startTime: null,
        endTime: null,
        experience: null,
        companyName: null,
        industry: null,
        jobTime: null,
        jobName: null,
        jobType: null,
        skillId: null,
        jobContent: null,
        type: null,
        sex: null,
        birthday: null,
        shareResource: null,
        isTech: null,
        idea: null,
        isPartner: null,
        hobby: null,
        city: null,
        other: null,
        isMember: null,
        orderId: null,
        orderStartTime: null,
        orderEndTime: null
      },
      // 表单参数
      form: {},
      userDetail: {},
      userSkillList: [],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询app用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        username: null,
        password: null,
        nickname: null,
        email: null,
        phone: null,
        address: null,
        createTime: null,
        updateTime: null,
        avatarUrl: null,
        education: null,
        school: null,
        major: null,
        startTime: null,
        endTime: null,
        experience: null,
        companyName: null,
        industry: null,
        jobTime: null,
        jobName: null,
        jobType: null,
        skillId: null,
        jobContent: null,
        type: null,
        sex: null,
        birthday: null,
        shareResource: null,
        isTech: null,
        idea: null,
        isPartner: null,
        hobby: null,
        city: null,
        other: null,
        isMember: null,
        orderId: null,
        orderStartTime: null,
        orderEndTime: null
      };
      this.resetForm("form");
    },
    /** 查询用户详情 */
    handleDetail(row) {
      this.drawer = true
      getUser(row.id).then(response => {
        this.userDetail = response.data;
        this.userSkillList = response.data.userSkillList;
        console.log(this.userSkillList)
      })
    },


    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加app用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改app用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除app用户编号为"' + ids + '"的数据项？').then(function() {
        return delUser(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('app/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    /** 跳转到该用户的动态列表 */
    handleDynamicDetail(row) {
      this.drawer = false;
      this.$router.push({
        path: "/app/content/dynamic",
        query: {
          username: row.username,
        },
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.user-info{
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}
.rotate-text {
  animation: rotate 5s infinite linear;
}

@keyframes rotate {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
.demo-drawer__footer{
  text-align: center;
  .el-button{
    margin-left: 20px;
    margin-top: 50%;
  }

}
</style>
