<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <el-button @click="openDialog" :loading="loading">分值统计日设置</el-button>
        <el-button :loading="loading">导出通讯录N/A</el-button>
      </el-col>
    </el-row>

    <!-- 列表区域 -->
    <el-table :data="crews" style="width: 100%" v-loading="loading">
      <el-table-column type="index" width="40"></el-table-column>
      <el-table-column label="头像" width="110">
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="'http://localhost:9102/img' + scope.row.headUrl"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="70"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="110"></el-table-column>
      <el-table-column prop="deptId" label="部门" width="80"></el-table-column>
      <el-table-column prop="roleName" label="角色" width="80"></el-table-column>
      <el-table-column label="对应资产">
        <template slot-scope="scope">
          <span
            v-for="asset in assets[scope.$index]"
            :key="asset"
            style="margin-right: 10px"
          >{{asset}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="80">
        <template slot-scope="scope">
          <el-button type="primary" @click="toDetail(scope.row.id)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div>
      <!-- 打分提示 -->
      <span class="tips" v-if="currentDate!=null">请于本月{{currentDate}}日前完成上月的打分</span>
      <span class="tips" v-else>请设置打分截止日期</span>

      <!-- 页码区域 -->
      <el-pagination
        v-if="total > 0"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[20, 50, 100]"
        :page-size="20"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        style="float: right"
      ></el-pagination>
    </div>

    <!-- 设置打分截止日期的dialog -->
    <el-dialog title="设置打分截止日期" :visible.sync="dialogVisible" width="330px">
      <el-form ref="form" :model="settingForm" :rules="rules" label-width="120px">
        <el-form-item label="打分截止日期" prop="date">
          <!-- 这里要限制数字类型 并且要小于等于28 -->
          <el-input v-model="settingForm.date" placeholder="请输入截止日期" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="editDeadline">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      crews: [],
      assets: [],
      settingForm: {
        date: ""
      },
      currentDate: 0,
      loading: false,
      dialogVisible: false,
      //分页相关
      pageIndex: 1,
      pageSize: 20,
      total: 0,
      rules: {
        date: [{ required: true, message: "请输入截止时间", trigger: "blur" }]
      }
    };
  },
  methods: {
    getCrews() {
      this.loading = true;
      request({
        url: "/user/getUserByPage",
        method: "post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      })
        .then(response => {
          this.crews = response.data.user.records;
          this.total = response.data.user.total;
          this.assets = response.data.asset;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //获取截止日期
    getDeadline() {
      request({
        url: "/user/getScoringDeadline",
        method: "post"
      })
        .then(response => {
          this.currentDate = response.data;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //修改截止日期
    editDeadline() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true;
          request({
            url: "/user/setScoringDeadline",
            method: "post",
            params: {
              date: this.settingForm.date
            }
          })
            .then(response => {
              this.getDeadline();
              this.loading = false;
              this.dialogVisible = false;
            })
            .catch(error => {
              this.loading = false;
            });
        }
      });
    },

    //打开设置截止日期的dialog
    openDialog() {
      this.settingForm.date = this.currentDate;
      this.dialogVisible = true;
    },

    //到详情页面
    toDetail(id) {
      if (id != null) {
        this.$router.push("/crew/" + id);
      } else {
        this.$message({
          message: "发生错误",
          type: "error"
        });
      }
    },

    //页码规格变更
    handleSizeChange(val) {
      this.pageSize = val;
      this.getCrews();
    },

    //页码页数变更
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getCrews();
    }
  },
  mounted() {
    this.getCrews();
    this.getDeadline();
  }
};
</script>

<style scoped>
.tips {
  line-height: 32px;
}
</style>

