<template>
  <div>
    <div class="topBox">
      <NavBar :go="{type:'push',path:'/menu'}" />

      <van-row>
        <van-col span="24">
          <org-tree v-model="cascaderValue" :name="'sss'" @changeItem="getDataList"></org-tree>
        </van-col>
      </van-row>
      <van-row>
        <van-col span="12"
          ><van-field
            v-model="fieldValue"
            is-link
            label-width="3em"
            clearable
            :disabled="showStatus"
            label="状态"
            placeholder=""
            @click="showStatus = true"
          />
          <van-popup v-model="showStatus" round position="bottom">
            <van-picker
              title="状态"
              show-toolbar
              :columns="columns"
              @confirm="onConfirm"
              @cancel="onCancel"
              @change="onChange"
              :close-on-click-overlay="false"
            />
          </van-popup>
        </van-col>
        <van-col span="12">
          <van-field
            v-model="currentDate"
            clearable
            label-width="3em"
            label="年份"
            placeholder=""
            :disabled="showDate"
            @click="showDate = true"
          />
          <van-popup v-model="showDate" round position="bottom">
            <van-datetime-picker
              v-model="presentDate"
              @cancel="onCancel"
              @confirm="onDateConfirm"
              type="year-month"
              title="年份"
            />
          </van-popup>
        </van-col>
      </van-row>

    </div>

    <!-- //卡片内容区域 -->
    <div class="navBarclas">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <van-panel :title="item.title" v-for="item in taskList" :key="item.id" class="card" status="状态">
          <template #header>
            <div class="titleClass">
              <div class="title">{{ item.title }}</div>
              <div>
                <van-button plain type="info" v-if="isSign(item)" size="mini" @click="trainSign(item.id)"
                  >演练登记</van-button
                >
                <van-button plain type="info" v-if="isSignature(item)" size="mini" @click="signature(item.id)"
                  >签名</van-button
                >
                <van-button plain type="info" v-if="isSignature(item)" size="mini" @click="signature(item.id)"
                  >评价</van-button
                >
              </div>
            </div>
          </template>
          <div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>单位名称</div>
              <div>{{ item.orgName }}</div>
            </div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>演练状态</div>
              <div>{{ item.statusText }}</div>
            </div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>演练项目</div>
              <div>{{ item.typeText }}</div>
            </div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>演练时间</div>
              <div>{{ item.startDate }}</div>
            </div>
            <div class="mainItem">
              <div>签名情况</div>
              <div class="condition" @click="signatureCondition(item.id)">{{ item.signNums }}</div>
            </div>
          </div>
        </van-panel>
      </van-list>
    </div>
    <!-- 卡片弹框 -->
    <van-dialog v-model="conditionShow" title="签名情况" show-cancel-button>
      <div class="conditionCls">
        <div class="title">已签名人员({{ participationList.num }}人):</div>
        <div class="people">{{ participationList.list }}</div>
        <div class="title">未签名人员({{ absenceList.num }}人):</div>
        <div class="people">{{ absenceList.list }}</div>
      </div>
    </van-dialog>
  </div>
</template>
<script>
import NavBar from '@/components/NavBar'
import { Col, Row, Cascader, Dialog, DatetimePicker, Icon, Picker } from 'vant'
import { gettrainingList, getsignUserList } from '@/api/training.js'
import { deptTreeList } from '@/api/toConsult.js'
import { Toast } from 'vant'
import OrgTree from '@/components/orgTree'
import { newDateMonth } from '@/utils/date.js'
export default {
  data() {
    return {
      orgName: '',
      orgShow: false,
      presentDate: '', //默认时间
      pageNum: 1,
      loading: false, //加载状态
      finished: false, //是否全部加载完毕
      participationList: {
        list: [], // 参与人员
        num: 0 //人数
      },
      absenceList: {
        list: [],
        num: 0
      }, // 缺席人员
      orgName: JSON.parse(sessionStorage.getItem('SET_USER_ORGNAME')) || '', //机构名称
      cascaderValue: '', //机构ID
      show: false, //机构弹框显示隐藏

      fieldNames: {
        text: 'name',
        value: 'id',
        children: 'children'
      },
      taskList: [], //列表数据
      typeValue: ' ', //类型值
      typeName: '全部', //类型名称
      value1: JSON.parse(sessionStorage.getItem('SET_USER_ID')) || '', //输入框model

      typeStatus: false, //类型显示隐藏
      showStatus: false, //状态显示隐藏
      showDate: false, //月份显示隐藏
      fieldValue: '全部', //状态名称
      statusValue: ' ', //状态值
      columns: ['全部', '待记录', '待签名', '已完成'], //状态数组
      currentDate: newDateMonth(), //月份值
      conditionShow: false //机构弹框显示隐藏
    }
  },
  components: {
    NavBar,
    OrgTree,
    Dialog,
    Icon,
    DatetimePicker,
    Picker,
    Col,
    Row,
    Cascader
  },

  filters: {
    statusFilter: function (value) {
      let str = ''
      // 0待调阅，1调阅中，2已调阅，3已超期
      switch (value) {
        case 0:
          str = '待调阅'

          break
        case 1:
          str = '调阅中'
          break

        case 2:
          str = '已调阅'
          break

        case 3:
          str = '已超期'
          break
      }
      return str
    }
  },

  created() {
    this.presentDate = new Date(+newDateMonth().split('-')[0], +newDateMonth().split('-')[1] - 1)
  },
  mounted() {
    this.cascaderValue = JSON.parse(window.sessionStorage.getItem('SET_USER_ORGID')) + ''
    console.log(this.cascaderValue)
    this.init()
  },
  methods: {
    //机构搜索
    getDataList(val) {
      this.cascaderValue = val.id
      this.orgName = val.name
      this.selectListAppHandler()
    },
    //判断是否展示签名按钮
    isSignature(list) {
      return true
      // let falg = false
      // let timefalg = false
      // //value1 当前登陆用户ID
      // //任务处于待记录状态，且任务在规定完成时间范围内，登录人所在机构和角色与任务培训角色、培训机构一致才显示
      // if (list.startDate && list.endDate && list.status == 1) {
      //   let date = new Date().getTime()
      //   let startDate = Date.parse(new Date(list.startDate))
      //   let endDate = Date.parse(new Date(list.endDate))

      //   if (date >= startDate && date <= endDate) {
      //     console.log(list.id, 'ssss')
      //     timefalg = true
      //   }
      // }
      // list.userList?.forEach(item => {
      //   console.log(this.value1, 'sss')
      //   if (this.value1 == item.userId && item.sign === 0) {
      //     console.log(list.id, 'fff')
      //     falg = true
      //   }
      // })

      // if (falg && timefalg) {
      //   return true
      // }
    },
    //判断是否展示登记按钮
    isSign(list) {
      return true
      // let falg = false
      // let timefalg = false
      // //value1 当前登陆用户ID
      // let date = new Date().getTime()
      // let startDate = Date.parse(new Date(list.startDate))
      // let endDate = Date.parse(new Date(list.endDate))
      // //当前时间再时间范围内且签名是待签名状态且签名列表存在当前登录人未签名的情况才可以进行签名
      // if (list.startDate && list.endDate && list.status == 0) {
      //   if (date >= startDate && date <= endDate) {
      //     timefalg = true
      //   }
      // }
      // //cascaderValue  当前机构ID
      // //当前用户角色ID
      // let roleL = JSON.parse(sessionStorage.getItem('SET_USER_ROLELIST'))
      // roleL.forEach(i => {
      //   list.taskRoleList?.forEach(item => {
      //     if (this.cascaderValue == list.orgId && i.roleId == item.roleId) {
      //       falg = true
      //     }
      //   })
      // })
      // if (date > endDate) {
      //   //当前时间大于结束时间表示已超期

      //   timefalg = false
      //   falg = false
      // }
      // if (falg && timefalg) {
      //   return true
      // }
    },
    //清空查询条件
    clearSearch() {
      this.fieldValue = ''
      this.statusValue = ''
      this.cascaderValue = ''
      this.currentDate = ''
      this.typeValue = ''
      this.typeName = ''
      this.orgName = ''

      this.selectListAppHandler()
    },

    //初始化
    init() {
      this.selectListAppHandler()
    },
    selectListAppHandler(type = 0, callback = () => {}) {
      let obj = {
        isAppSelect: 1,
        pageNum: this.pageNum,
        pageSize: 10
      }
      if (!type) {
        obj.pageNum = 1
        this.pageNum = 1
      }
      if (this.statusValue) {
        obj.status = this.statusValue
      }
      if (this.cascaderValue) {
        obj.orgId = this.cascaderValue
      }
      if (this.currentDate) {
        obj.date = this.currentDate + '-01'
      }
      if (this.typeValue) {
        obj.type = this.typeValue
      }

      //获取任务列表
      gettrainingList(obj).then(res => {
        let { code, rows, msg } = res
        if (code == 200) {
          if (type) {
            this.taskList.push(...rows)
            if (rows.length == 0 || rows.length < 10) {
              //已加载完全部数据
              this.finished = true
            }
            callback()
          } else {
            this.finished = false
            this.taskList = rows
          }
        }
      })
    },

    // 开始调阅事件
    startMonitorHandler(taskId) {
      this.taskId = taskId
      this.showDialog = true
    },
    //已调阅事件
    endMonitorHandler(status, taskId) {
      // 0待调阅，1调阅中，2已调阅，3已超期
      //跳转详情
      if (status <= 1) return
      this.$router.push('/consultInfo/' + taskId)
    },

    //搜索选择状态时触发
    onConfirm(value, index) {
      this.fieldValue = value

      switch (value) {
        case '待记录':
          this.statusValue = '0'

          break
        case '待签名':
          this.statusValue = '1'
          break

        case '已完成':
          this.statusValue = '2'
          break
        case '全部':
          this.statusValue = ' '
          break
      }
      this.selectListAppHandler()
      this.showStatus = false
    },

    //月份选中触发
    onDateConfirm() {
      this.currentDate = this.newDate(this.presentDate)

      this.showDate = false
      this.selectListAppHandler()
    },
    //日期转换
    newDate(time) {
      var date = new Date(time)
      var y = date.getFullYear()
      var m = date.getMonth() + 1
      m = m < 10 ? '0' + m : m
      var d = date.getDate()
      d = d < 10 ? '0' + d : d
      return y + '-' + m
    },
    //onLoad下拉刷新
    onLoad() {
      if (this.pageNum == 1) {
        this.pageNum = 2
      }
      this.loading = true
      this.selectListAppHandler(1, () => {
        this.pageNum++
        this.loading = false
        console.log(1)
      })
    },
    //查看签名情况
    signatureCondition(id) {
      this.conditionShow = true
      getsignUserList(id).then(res => {
        console.log(res, 'ssss')
        let { meg, code, data } = res
        data.map(item => {
          if (item.sign !== 1) {
            this.absenceList.list.push(item.userName)
            this.absenceList.num++
          } else {
            this.participationList.list.push(item.userName)
            this.participationList.num++
          }
        })
        this.absenceList.list = this.absenceList.list.join(',')
        this.participationList.list = this.participationList.list.join(',')
      })
    },
    //培训登记跳转
    trainSign(id) {
      this.$router.push('/addRehearsalTask/' + id)
    },
    //签名
    signature(id) {
      this.$router.push('/personnelSignature/' + id + '_edit')
    },
    //跳转详情
    goInfo(id) {
      this.$router.push('/personnelSigninfo/' + id + '_info')
    },
    onCancel() {
      this.show = false

      this.showDate = false
      this.showStatus = false
    }
  }
}
</script>
<style lang="scss" scoped>
.popup {
  height: 40vh;
}
.navBarclas {
  height: calc(100vh - 380px);
  overflow: scroll;
}
.btnf_box {
  background-color: #fff;
}
.card {
  margin: 20px;
  margin-bottom: 0px;
  box-shadow: 0 8px 12px #ebedf0;
}
.btn {
  float: right;
  margin-top: 24px;
  margin-right: 20px;
  box-sizing: border-box;
}
.titleClass {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 20px;
  border-bottom: 1px solid #ccc;

  .title {
    font-size: 30px;
    flex: 1;
    line-height: 50px;
  }
}
.mainItem {
  display: flex;
  font-size: 28px;
  padding: 20px;
  justify-content: space-between;
  .condition {
    color: #1989fa;
    text-decoration: underline;
  }
}
.conditionCls {
  .title {
    color: #1989fa;
    margin-left: 30px;
    // margin-top: 30px;
  }
  .people {
    margin-left: 80px;
    margin-bottom: 30px;
    margin-top: 30px;
  }
}
.topBox {
  overflow: hidden;
}
:deep.van-field--disabled{
  color: #323233;
}
:deep.van-field--disabled .van-field__label{
  color: #323233;
}
:deep .van-field__control[disabled]{
			color: #323233;
			-webkit-text-fill-color: #323233;
		}
</style>
