<!--积分管理-->
<template>
  <div class="app-container points-record">
    <div class="page-one-box" v-if="pageType == 1">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键字" prop="searchValue">
          <el-input
            v-model="queryParams.searchValue"
            placeholder="姓名/学号"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="创建时间" prop="dateRange" style="margin-left:10px;">
          <el-date-picker
            :clearable="false"
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <p class="inquire-button-one" @click="handleQuery">查询</p>
          <p class="reset-button-one" @click="resetQuery">重置</p>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" border :data="recordList" @sort-change="handleSelectionChange">
        <el-table-column label="姓名" align="left" prop="nickName" />
        <el-table-column label="学号" align="left" prop="userName" />
        <el-table-column label="总积分" align="left" sortable="custom" prop="totalPoints" />
        <el-table-column label="信用分" align="left" sortable="custom" prop="creditScore" />
        <el-table-column label="奖励分" align="left" sortable="custom" prop="bonusPoints" />
        <el-table-column label="操作" align="left" class-name="small-padding fixed-width" width="240" v-if="tableButtonType">
          <template slot-scope="scope">
            <div class="table-button-box">
              <p class="table-button-null"></p>
              <p class="table-button-p"
                 v-hasPermi="['points:record:creditscore']"
                 @click="goPage(2,scope.row)"
              >信用分明细</p>
              <p class="table-button-p"
                 v-hasPermi="['points:record:bonuspoints']"
                 @click="goPage(3,scope.row)"
              >奖励分明细</p>
              <p class="table-button-null"></p>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-sizes="[20, 30, 40, 50]"
        v-show="total>0"
        :total="total"
        layout="total, prev, pager, next, sizes, jumper"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <credit-score-list v-if="pageType == 2" :propsData="propsData"></credit-score-list>
    <reward-points-list v-if="pageType == 3" :propsData="propsData"></reward-points-list>
  </div>
</template>

<script>
  import { listRecord, getRecord, delRecord, addRecord, updateRecord } from "@/api/exam/record";
  import creditScoreList from "./creditScoreList.vue";
  import rewardPointsList from "./rewardPointsList.vue";
  export default {
    name: "Record",
    components: {
      creditScoreList,
      rewardPointsList
    },
    data() {
      return {
        tableButtonType:this.hasPermiDom(['points:record:creditscore','points:record:bonuspoints']),
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 用户积分记录表格数据
        recordList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize:20,
          searchValue: null,
          points: null,
          joinUserId: null,
          mode: null,
          deptName: null,
          deptId: null,
          userId: null,
        },
        pointsMode:[],
        // 日期范围
        dateRange: [],
        //页面状态
        pageType:1,
        //所选用户id
        propsData:{},
      };
    },
    created() {
      this.getList();
      this.getDicts("points_mode").then(response => {
        this.pointsMode = response.data;
      });
    },
    methods: {
      goPage(type,row){
        console.log("type",type)
        console.log("row",row)
        if(this.pageType!=type){
          if(type == 1){
            this.pageType = type;
            this.getList();
          }else {
            this.propsData.userId = row.id;
            this.propsData.allBonusPoints = row.allBonusPoints;
            this.propsData.consumeBonusPoints = row.consumeBonusPoints;
            this.propsData.bonusPoints = row.bonusPoints;

            this.pageType = type;
          }
        }
      },
      /** 查询用户积分记录列表 */
      getList() {
        this.loading = true;
        if(this.dateRange&&this.dateRange.length>0)
        {
          this.queryParams.beginTime=this.dateRange[0]
          this.queryParams.endTime=this.dateRange[1]
        }
        else
        {
          this.queryParams.beginTime=null;
          this.queryParams.endTime=null
        }

        if(this.pageType){
          this.queryParams.pointsType = 2
        }

        listRecord(this.queryParams).then( response => {
          this.recordList =  response.rows;
          this.total =  response.total;
          this.loading = false;
        });
      },
      // 行为字典翻译
      modeFormat(row, column) {
        return this.selectDictLabel(this.pointsMode, row.mode);
      },
      pointsFormat(row,a)
      {
        return row.recordType.code===1?`+ ${row.points}`:`- ${row.points}`
      },

      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          points: null,
          joinUserId: null,
          mode: null,
          deptName: null,
          deptId: null,
          createTime: null,
          userId: null,
          createBy: null,
          updateTime: null,
          updateBy: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        // this.resetForm("queryForm");
        this.$set(this,'queryParams',{
          pageNum: 1,
          pageSize:20,
          searchValue:"",
        });
        this.queryParams.searchValue='';
        this.dateRange=[];
        this.handleQuery();
      },
      // 排序监听
      handleSelectionChange(type) {
        if(type.order == 'ascending'){
          this.queryParams.order = type.prop;//降
          this.queryParams.orderType = 'asc';//升ascending
        }else if(type.order == 'descending'){
          this.queryParams.order = type.prop;//降
          this.queryParams.orderType = 'desc';//降
        }else{
          this.queryParams.order = null;//无
          this.queryParams.orderType = null;//无
        }
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加用户积分记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getRecord(id).then( response => {
          this.form =  response.data;
          this.open = true;
          this.title = "修改用户积分记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateRecord(this.form).then( response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRecord(this.form).then( response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除用户积分记录?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('exam/record/export', {
          ...this.queryParams
        }, `exam_record.xlsx`)
      }
    }
  };
</script>
<style scoped lang="scss">
  .points-record {
    display: flex!important;
    flex-direction: column;
    box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
    overflow: hidden;
    .page-one-box{
      flex:1;
      display: flex!important;
      flex-direction: column;
      overflow: hidden;
      padding:20px!important;
    }
    .el-row{
      margin-bottom:20px;
    }
    .button-box{
      width:250px;
      display: flex;
    }
    .form-box{
      display:flex;
      .null-p{
        flex:1;
      }
    }
  }
</style>
