<template>
  <div class="record-warp">
    <div class="record-main">
      <div class="compare_out">
        <div class="user" @click="isClikcObj(true)">
          <img style="width: 100px" :src="userImg" /><span style="position: absolute"
            >用户对比</span
          >
        </div>
        <div class="user" @click="isClikcObj(false)">
          <img style="width: 100px" :src="planImg" /><span style="position: absolute"
            >计划对比</span
          >
        </div>
      </div>
      <!-- <img src="../../../assets/report/lv.png" alt=""> -->
      <el-row>
        <el-col :span="24">
          <div class="search-head">
            <el-input
              v-show="isUser"
              v-model="name"
              clearable
              placeholder="请输入姓名"
            ></el-input>
            <div v-show="isUser">&nbsp;&nbsp;&nbsp;&nbsp;</div>
            <el-input
              v-show="isUser"
              v-model="studentNumber"
              clearable
              placeholder="请输入学号"
            ></el-input>
            <div v-show="isUser">&nbsp;&nbsp;&nbsp;&nbsp;</div>
            <el-input
              v-show="!isUser"
              v-model="planName"
              clearable
              placeholder="请输入计划名称"
            ></el-input>
            <div v-show="!isUser">&nbsp;&nbsp;&nbsp;&nbsp;</div>
            <el-cascader
              placeholder="请选择组织架构"
              v-model="group"
              :options="groupData"
              :props="{ checkStrictly: true }"
              clearable
            ></el-cascader>
            &nbsp;&nbsp;&nbsp;&nbsp;
            <el-button type="primary" round icon="el-icon-search" @click="searchTarget"
              >搜索</el-button
            >
            <el-button type="info" round @click="clearSearch">清空</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row class="add_user_class">
        <el-col :span="24">
          <div class="search-head"></div>
        </el-col>
      </el-row>

      <div class="table-content">
        <el-table
          v-show="isUser"
          :data="tableData"
          :row-style="{ height: '0px' }"
          :cell-style="{ padding: '5px' }"
          :header-cell-style="{ background: '#F8F8F8', color: '#606266' }"
        >
          <el-table-column
            v-if="isUser"
            show-overflow-tooltip
            prop="userName"
            label="姓名"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="isUser"
            prop="userNo"
            label="学号"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="isUser"
            prop="totalScore"
            label="总分"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="isUser"
            prop="gender"
            label="性别"
            align="center"
            width=""
            :formatter="genderFun"
          >
          </el-table-column>
          <el-table-column
            v-if="isUser"
            prop="orgName"
            label="所属组织架构"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column label="操作" width="400" align="center">
            <template slot-scope="scope">
              <div style="display: flex; align-items: center">
                <div class="detail_button_out" v-if="isUser">
                  <div class="detail_button" @click="addUser(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 个人报告列表 </span>
                  </div>
                </div>
                <div class="detail_button_out" v-if="!isUser">
                  <div class="detail_button" @click="planReport(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 专业报告对比 </span>
                  </div>
                </div>
                <div class="detail_button_out" v-if="!isUser">
                  <div class="detail_button" @click="planUser(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 个人报告列表 </span>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="table-content">
        <el-table
          v-show="!isUser"
          :data="tableData"
          :row-style="{ height: '0px' }"
          :cell-style="{ padding: '5px' }"
          :header-cell-style="{ background: '#F8F8F8', color: '#606266' }"
        >
          <!-- <el-table-column prop="name" label="姓名" align="center" width=""  show-overflow-tooltip>
          </el-table-column> -->
          <!-- <el-table-column
           v-if="isUser"
            show-overflow-tooltip
            prop="userName"
            label="姓名"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column v-if="isUser" prop="userNo" label="学号" align="center" width="">
          </el-table-column>
          <el-table-column v-if="isUser" prop="totalScore" label="总分" align="center" width="">
          </el-table-column>
          <el-table-column
          v-if="isUser"
            prop="gender"
            label="性别"
            align="center"
            width=""
            :formatter="genderFun"
          >
          </el-table-column>
          <el-table-column
          v-if="isUser"
            prop="orgName"
            label="所属组织架构"
            align="center"
            width=""
         
          >
          </el-table-column> -->
          <el-table-column
            v-if="!isUser"
            show-overflow-tooltip
            prop="planName"
            label="计划名称"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="!isUser"
            prop="createDate"
            label="创建时间"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="!isUser"
            prop="planEndTime"
            label="结束时间"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="!isUser"
            prop="createUserName"
            label="创建人"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column
            v-if="!isUser"
            prop="planStatus"
            label="状态"
            :formatter="forMaPlan"
            align="center"
            width=""
          >
          </el-table-column>
          <el-table-column label="操作" width="400" align="center">
            <template slot-scope="scope">
              <div style="display: flex; align-items: center">
                <div class="detail_button_out" v-if="isUser">
                  <div class="detail_button" @click="addUser(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 个人报告列表 </span>
                  </div>
                </div>
                <div class="detail_button_out" v-if="!isUser &&scope.row.planStatus=='3'">
                  <div class="detail_button" @click="planReport(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 专业报告对比 </span>
                  </div>
                </div>
                <div class="detail_button_out" v-if="!isUser">
                  <div class="detail_button" @click="planUser(scope.row)">
                    <img src="../../../assets/img/table/search.png" />
                    <span> 个人报告列表 </span>
                  </div>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- small -->
      <el-pagination
        class="pag_class"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="pageNum"
        layout="total, sizes, prev, pager, next"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
      >
      </el-pagination>
      <!-- <el-pagination small background layout="total prev, pager, next" :total="36">
      </el-pagination> -->
    </div>
    <CompareUser ref="register" @search="searchTarget" />
    <ComparePlanUser ref="planUser" @search="searchTarget" />
    <ComparePlanProfession ref="planProfession" @search="searchTarget" />
  </div>
</template>

<script>
import { oSessionStorage } from "../../../utils/utils";
import compareUser from "../../../components/CompareUser.vue";
import comparePlanUser from "../../../components/ComparePlanUser.vue";
import comparePlanProfession from "../../../components/ComparePlanProfession.vue";
import { basePath } from "../../../utils/http";
import userImgActive from "../../../assets/report/lv.png";
import userImgDefault from "../../../assets/report/white.png";
export default {
  name: "userManage",
  components: {
    CompareUser: compareUser,
    ComparePlanUser: comparePlanUser,
    ComparePlanProfession: comparePlanProfession,
  },
  data() {
    return {
      //判断是用户还是计划对比
      isUser: true,
      //用户背景图
      userImg: userImgActive,
      //计划背景图
      planImg: userImgDefault,

      basePath: basePath,
      //上传文件的弹出款的标志
      fileVisible: false,
      //判断点击的是重置密码还是点击删除
      isDelete: false,
      //重置密码需要调用个userId
      userId: "",
      //充值密码使用userName
      userName: "",
      //重置密码弹出框
      resetVisible: false,
      //根据名称搜索
      planName: "",
      name: "",
      //搜索的用户编号
      studentNumber: "",
      //搜索的组织架构
      // 搜索需要的性别
      sex: "",

      group: "",
      ppData: [],
      groupData: [],
      options: [
        {
          value: "1",
          label: "选项1",
          children: [
            {
              value: "11",
              label: "选项11",
              children: [{ value: "111", label: "选项111" }],
            },
          ],
        },
      ],

      centerDialogVisible: false,
      editUserFlag: false,
      startTime: "",
      endTime: "",
      value1: null,
      total: 0,
      pageSize: 10,
      pageNum: 1,
      keyword: "",
      tableData: [],
      userInfo: {}, //用户信息

      invitationCode: "",
      roleType: "0",
      type: 0,
      view: {
        name: "",
        studentNumber: "",
        sex: "",
        orgName: "",
      },
      channel: "",
      channelList: [{ name: "渠道天成", id: "1" }],
    };
  },
  created() {},
  mounted() {
    this.userInfo = JSON.parse(oSessionStorage.getItem("userInfo"));

    this.getChannel();
    if (!this.userInfo) {
      //如果用户信息不存在跳转登陆页
      this.$router.push({ path: "/" });
    }
    this.searchTarget();
  },
  methods: {
    isClikcObj(val) {
      this.isUser = val;

      if (val) {
        this.userImg = userImgActive;
        this.planImg = userImgDefault;
      } else {
        this.planImg = userImgActive;
        this.userImg = userImgDefault;
      }
      this.searchTarget();
    },
    planReport(val) {
      this.$refs.planProfession.open(val);
    },
    forMaPlan(val) {
      if (val.planStatus == "1") {
        return "未开始";
        //
      } else if (val.planStatus == "2") {
        //
        return "进行中";
      } else if (val.planStatus == "3") {
        //
        return "已完成";
      }
    },
    //文件上传成功
    fileSuccess(res) {
      if (res.code == 200) {
        this.searchTarget();
        this.$message({
          message: res.msg,
          type: "success",
        });
      } else {
        this.$message({
          message: res.msg,
          type: "error",
        });
      }
      //查询列表
    },
    //文件上传失败
    fileError() {
      this.$message({
        message: "上传失败",
        type: "error",
      });
    },
    cancle() {
      this.resetVisible = false;
    },
    //提交文本
    submitCom() {
      //判断当前是否是
      if (this.isDelete) {
        this.deleteUser();
        //调用删除方法
      } else {
        this.resetFun();
        // 调用重置密码方法
      }
    },
    //性别---
    genderFun(val) {
      let sex = val.gender;
      if (sex == "1") {
        return "男";
      } else if (sex == "0") {
        return "女";
      } else {
        return sex;
      }
    },
    //每页多少条
    handleSizeChange(val) {
      this.pageSize=val;
      this.searchTarget();
      console.log(`每页 ${val} 条`);
      //将首页重置为1时---且总条数变化
      //设置为当前总条数
    },
    //获取组织架构方法--------------------开始-----------------------
    getChannel() {
      this.$http.get(`/org/findAllOrgByPOrgNo`, {}, (res) => {
        //  this.$toast.success({message:'成功'});
        if (res && res.code == 200) {
          //将值赋值给list
          if (res.data.length > 0) {
            let resAdd = this.addPro(res.data);
            this.ppData = JSON.parse(JSON.stringify(resAdd));
            let forRes = this.arrToTree(resAdd);
            // console.log('格式化的结构')
            // console.log(forRes)
            let resultRes = this.deleteChildren(forRes);
            console.log("格式化的结构且去掉children");
            console.log(resultRes);
            this.groupData = resultRes;
          } else {
            this.groupData = [];
          }
          // this.channelList = res.data;
        } else {
          this.$message.error(res.msg);
        }
      });
    },
    //z增加
    addPro(val) {
      let data = JSON.parse(JSON.stringify(val));
      for (let i = 0; i < val.length; i++) {
        data[i].value = val[i].orgNo;
        data[i].label = val[i].orgName;
      }
      return data;
    },
    //非递归方式：将平铺数据转换为树形结构数据
    arrToTree(arr) {
      let data = arr.filter((item) => {
        item.children = arr.filter((e) => {
          return item.orgNo === e.parentOrgNo;
        });
        return this.userInfo.orgNo == item.orgNo
        // return !item.parentOrgNo;
      });
      return data;
    },
    //去除转换树形结构数据后存在的空children
    deleteChildren(arr) {
      let childs = arr;
      for (let i = childs.length; i--; i > 0) {
        if (childs[i].children) {
          if (childs[i].children.length) {
            this.deleteChildren(childs[i].children);
          } else {
            delete childs[i].children;
          }
        }
      }
      return arr;
    },
    //获取组织架构方法--------------------结束-----------------------
    formatterDelete(row) {
      if (row.state == 0) {
        return "已删除";
      } else {
        return "正常";
      }
    },
    resetUser(flag, val) {
      this.resetVisible = true;
      this.isDelete = flag;
      this.userId = val.id;
      this.userName = val.userName;
      //当前用户id

      //重置密码
    },
    editUser(row) {
      this.editRegisterUser(row);
    },
    viewUser(row) {
      this.view.name = row.userName;
      this.view.studentNumber = row.userNo;
      this.view.sex = row.gender == "0" ? "女" : "男";
      this.view.orgName = row.orgName;
      this.centerDialogVisible = true;
    },
    planUser(val) {
      this.$refs.planUser.open(val);
    },
    addUser(val) {
      this.addRegisterUser(val);
    },
    clearSearch() {
      this.name = "";
      //搜索的用户编号
      this.studentNumber = "";
      //搜索的组织架构
      // 搜索需要的性别
      this.planName = "";
      this.group = "";
      this.searchTarget();
    },

    addRegisterUser(val) {
      this.$refs.register.open(val);
    },
    editRegisterUser(val) {
      this.$refs.register.edit(val);
    },

    handleCurrentChange(val) {
      this.pageNum = val;
      this.searchList();
    },
    //根据现有情况进行搜索
    searchList() {
      let url;
      if (this.isUser) {
        url = `/user/findUserListByPage?pageSize=${this.pageSize}&pageNum=${
          this.pageNum
        }${this.group.length == 0 ? `&orgNo=${this.userInfo.orgNo}` : `&orgNo=${this.group[this.group.length - 1]}`}${
          this.name == "" ? "" : `&userName=${this.name}`
        }${this.studentNumber == "" ? "" : `&userNo=${this.studentNumber}`}`;
      } else {
        url = `/plan/findListByPage?pageSize=${this.pageSize}&pageNum=${this.pageNum}${
          this.planName == "" ? "" : `&planName=${this.planName}`
        }${this.group.length == 0 ? `&orgNo=${this.userInfo.orgNo}` : `&orgNo=${this.group[this.group.length - 1]}`}`;
      }
      this.$http.get(url, {}, (res) => {
        console.log(res);
        if (res && res.code == 200) {
          console.log(res.data.content);
          this.tableData = res.data.content;
          this.total = res.data.totalElements;
        } else {
          // this.$toast.fail(res.msg);
          this.$message.error(res.msg);
        }
      });
    },
    searchTarget() {
      this.pageNum = 1;
      this.searchList();
    },
    //跳转首页
    goHome() {
      this.$router.push({ path: "/home" });
    },
    //跳转记录页
    goRecord() {
      this.$router.push({ path: "/record" });
    },
    //退出登陆
    logout() {
      oSessionStorage.removeItem("userInfo");
      oSessionStorage.removeItem("token");
      this.$router.push({ path: "/" });
    },

    //点击获取月出勤次数
    getTimes(param) {
      this.monthTimes = param;
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../styles/theme.less";
.pag_class {
  text-align: end;
}

.pag_class /deep/.el-input__inner {
  width: 100% !important;
}
.pag_class /deep/.el-pagination--small span:not([class*="suffix"]) {
  height: 22px;
  line-height: 28px !important;
}

.record-warp {
  position: relative;
  width: 100%;
  height: 80vh;
  .compare_out {
    line-height: 40px;
    position: absolute;

    top: -42px;
    display: flex;
    // background-color: #00bf78;
    .user {
      cursor: pointer;
      position: relative;
      span {
        top: -5px;
        left: 19px;
        font-weight: 700;
      }
      // background-image: image("../.");
      // background-color: #00bf78;
      // background-image: url("../../../assets/report/lv.png");
      // background-size: cover;
      // background-repeat: no-repeat;
    }
  }

  .record-main {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    overflow-y: auto;

    .search-head {
      width: 100%;
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
      align-items: center;

      .zc-title {
        color: #606266;
        font-size: 14px;
      }
    }

    .table-content {
      margin: 10px 0;
    }
  }
}

.add_user_class {
  margin-top: 10px;
}

.user_out {
  display: flex;
  width: 100%;
  align-items: center;
  border: 1px solid #eeeeef;
  line-height: 50px;
  margin-top: 10px;
}

.user_out_sub {
  flex: 1;
  background-color: #fafafa;
  text-align: center;
  // color:#ffffff
}

.user_out_subNext {
  padding-left: 10px;
  flex: 3;
}
.detail_button_out {
  flex: 1;
  display: flex;
  justify-content: space-around;
  .detail_button {
    display: flex;
    align-items: center;
    cursor: pointer;
    letter-spacing: 2px;
    img {
      width: 15px;
      margin-right: 5px;
    }
    span {
      color: #00bf78;
      // font-weight: 600;
    }
  }
}

//弹出框样式

.dig_update /deep/.el-cascader {
  position: relative;
  font-size: 14px;
  line-height: 40px;
  width: 100%;
}
.dig_update /deep/.el-dialog {
  box-shadow: none !important;
  background: transparent !important;
}
.demo-ruleForm /deep/ .el-form-item {
  margin-right: 10px;
  vertical-align: top;
  display: flex !important;
  flex-direction: column;
}
.demo-ruleForm /deep/.el-dialog__header {
  background-color: #ffffff;
  padding-left: 140px !important;
}
.demo-ruleForm /deep/.el-form-item__label {
  text-align: left;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.demo-ruleForm /deep/.el-input {
  width: 100% !important;
}

.demo-ruleForm /deep/.el-input__inner {
  width: 100% !important;
  background-color: #f7f7f7;
  border: 0px;
}

.dig_button {
  margin-top: 40px;
  display: flex;
  width: 100%;
  justify-content: space-around;
}

.demo-ruleForm {
  background-color: #ffffff;
  // border-radius: 20px;
  margin-right: -10px;
  margin-top: -10px;
  padding-right: 100px;
  padding-left: 100px;
  border-radius: 20px;
  padding-top: 20px;
  padding-bottom: 20px;
  .dig_title {
    margin-bottom: 30px;
    text-align: center;
    font-weight: 700;
  }
}
</style>
