<template>
  <!-- 代办事项 -->
  <div class="box">
    <el-row style="padding-bottom:15px;">
      <el-col :span="13" style="margin:0px 10px;">
        <!-- 搜索工作栏 -->
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="提醒时间" prop="createTime">
            <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <!-- 列表 -->
        <el-table v-loading="loading" border stripe ref="tables" :data="list" height="560px">
          <el-table-column label="提醒时间" align="center" prop="createTime" width="180">
            <template v-slot="scope">
              <span> {{dayjs(scope.row.createTime).format('YYYY-MM-DD HH:mm:ss')}}</span>
            </template>
          </el-table-column>
          <el-table-column label="类型" align="center" prop="templateType" min-width="100">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.SYSTEM_NOTIFY_TEMPLATE_TYPE" :value="scope.row.templateType" />
            </template>
          </el-table-column>
          <el-table-column label="内容" align="center" prop="templateContent" />
          <!-- <el-table-column label="是否已读" align="center" prop="readStatus" width="80">
            <template v-slot="scope">
              <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.readStatus" />
            </template>
          </el-table-column> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
            <template v-slot="scope">
              <el-button size="mini" type="text" @click="handleShowDetail(scope.row)">查看详情</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <pagination v-show="total > 0" :total="total" :autoScroll="false" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
      <el-col :span="10">
        <el-calendar v-model="selectDate" ref="calendar" :id="refVal" v-if="isShow" :range="['2004-01-01', '2050-12-29']">
          <template slot="dateCell" slot-scope="{date, data}">
            <div :class="data.isSelected ? 'is-selected' : ''">
              {{ data.day.split('-').slice(1).join('-') }} <i class="el-icon-star-on" style="color:red" v-if="handelFilter(data)"></i>
              <div style="text-align:center;margin-top:15px">
                {{ data.isSelected ? '✔️' : ''}}
              </div>
            </div>
          </template>
        </el-calendar>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getMyNotifyMessagePage, updateAllNotifyMessageRead, updateNotifyMessageRead } from "@/api/system/notify/message";
import dayjs from 'dayjs'
import { set } from 'vue';
export default {
  name: "SystemMyNotify",
  data() {
    return {
      refVal: 'refVal',
      isShow: true,
      monthList: [],
      selectDate: new Date(),
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的站内信列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        readStatus: null,
        createTime: []
      },
    };
  },
  created() {
    this.getList();
    this.handleGetList(new Date(), 1)
  },
  watch: {
    selectDate: {
      handler(val) {
        if (val) {
          this.handleGetList(val)
        }
      },
      deep: true,
      // immediate: true
    }
  },
  methods: {
    dayjs,
    handleShowDetail(row) {
      const { billType, billId } = row.extraData
      console.log('billType..', billType);
      if (billType === 'CLSQ') {
        this.$router.push({ path: "/bs/expenseApply/business", query: { id: billId, flag: 'show', title: '出差申请单查看', typeStr: 'business' } });
      } else if (billType === 'RCSQ') {
        this.$router.push({ path: "/bs/expenseApply/business", query: { id: billId, flag: 'show', title: '日常申请单查看', typeStr: 'daily' } });
      } else if (billType === 'CLBX') {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: billId, flag: 'show', title: '出差报销单查看', typeStr: 'business' } });
      } else if (billType === 'RCBX') {
        this.$router.push({ path: "/bs/expenseClaim/account", query: { id: billId, flag: 'show', title: '日常报销单查看', typeStr: 'daily' } });
      }
    },
    handelFilter(obj) {
      const { day } = obj
      let flag = this.monthList.find(item => dayjs(item.createTime).format('YYYY-MM-DD') == day) || null
      if (flag) {
        return true
      } else {
        return false
      }
    },
    handleGetList(date, type) {
      getMyNotifyMessagePage({
        pageNo: 1,
        pageSize: 999,
        createTime: [dayjs(date).startOf('month').format('YYYY-MM-DD 00:00:00'), dayjs(date).endOf('month').format('YYYY-MM-DD 23:59:59')]
      }).then(response => {
        this.monthList = response.data.list || []
      });
      this.queryParams.createTime = [dayjs(date).format('YYYY-MM-DD 00:00:00'), dayjs(date).format('YYYY-MM-DD 23:59:59')]
      // if (!type) {
      // } else {
      //   this.queryParams.createTime = [dayjs(date).startOf('month').format('YYYY-MM-DD 00:00:00'), dayjs(date).endOf('month').format('YYYY-MM-DD 23:59:59')]
      // }
      this.getList()
    },
    handleShowData(date, data) {
      console.log('date..', date, data);
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getMyNotifyMessagePage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    initCheckBox(val) {
      this.selectDate = new Date()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.createTime = []
      this.selectDate = ''
      this.isShow = false
      setTimeout(() => {
        this.isShow = true
        this.$nextTick(() => {
          let todayBtn = document.querySelector(`#${this.refVal} .el-calendar__button-group .el-button-group>button:nth-child(2)`);
          todayBtn.addEventListener('click', () => {
            this.initCheckBox();
          });
        })
      }, 200)
      this.handleQuery();
    },
    handleUpdateList() {
      let list = this.$refs["tables"].selection;
      if (list.length === 0) {
        return;
      }
      this.handleUpdate(list.map(v => v.id))
    },
    handleUpdateSingle(row) {
      this.handleUpdate([row.id])
    },
    handleUpdate(ids) {
      updateNotifyMessageRead(ids).then(response => {
        this.$modal.msgSuccess("标记已读成功！");
        this.getList();
      });
    },
    handleUpdateAll() {
      updateAllNotifyMessageRead().then(response => {
        this.$modal.msgSuccess("全部已读成功！");
        this.getList();
      });
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-calendar {
  height: 665px;
}
::v-deep .el-calendar__header {
  padding-top: 0px !important;
}
::v-deep .el-calendar__body {
  padding-bottom: 0px;
  height: 620px;
}
::v-deep .el-calendar-table {
  height: 100%;
}
.is-selected {
  color: #1989fa;
}
</style>

