<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="24">
          <form action="/">
            <van-search
              v-model="evaluateName"
              @input="selectListAppHandler()"
              clearable
              maxlength="50"
              placeholder="请输入搜索关键词"
            />
          </form>
        </van-col>
      </van-row>
      <van-row>
        <van-col span="10">
          <!-- <van-field
            v-model="fieldValue"
            label-width="3em"
            clearable
            :disabled="showStatus"
            label="状态"
            placeholder=""
            @click="showStatus = true"
            ><van-icon name="arrow-down" slot="button"
          /></van-field> -->
          <van-cell title="状态" @click="showStatus = true" is-link arrow-direction="down" :value="fieldValue" />

          <van-popup v-model="showStatus" round position="bottom">
            <van-picker
              title="状态"
              show-toolbar
              :columns="columns"
              @confirm="onConfirm"
              @cancel="onCancel"
              :close-on-click-overlay="false"
            />
          </van-popup>
        </van-col>
        <van-col span="14">
          <!-- <van-field
            v-model="currentDate"
            clearable
            label-width="3em"
            label="日期"
            placeholder=""
            :disabled="showDate"
            @click="showDate = true"
          >
            <van-icon name="arrow-down" slot="button"
          /></van-field> -->
          <van-cell title="日期" @click="showDate = true" is-link arrow-direction="down" :value="currentDate" />

          <van-popup v-model="showDate" round position="bottom">
            <van-datetime-picker
              v-model="presentDate"
              type="date"
              title="选择年月日"
              @cancel="onCancel"
              :formatter="formatter"
              @confirm="onDateConfirm"
              confirm-button-text="确定"
            />
          </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.evaluateName }}</div>
              <div>
                <van-button type="info" v-if="evaluateTure(item)" size="small" @click="evaluate(item.id)"
                  >评价</van-button
                >
              </div>
            </div>
          </template>
          <div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>评价状态</div>
              <div v-if="item.status == '0'" class="wj-title-blue">{{ item.status | statusList(this_) }}</div>
              <div v-else class="wj-title-orange">{{ item.status | statusList(this_) }}</div>
            </div>
            <div class="mainItem" @click="goInfo(item.id)">
              <div>评价周期</div>
              <div>{{ item.startTime }}-{{ item.endTime }}</div>
            </div>
          </div>
        </van-panel>
      </van-list>
    </div>
  </div>
</template>
<script>
import NavBar from '@/components/NavBar'
import { Col, Row, Cascader, Dialog, DatetimePicker, Icon, Picker } from 'vant'
import { getEvaluatePage } from '@/api/evaluateTask.js'
import { deptTreeList } from '@/api/toConsult.js'
import { Toast } from 'vant'
import OrgTree from '@/components/orgTree'

import { newDateMonth, newDateDay } from '@/utils/date.js'
export default {
  data() {
    return {
      orgName: '',
      orgShow: false,
      presentDate: '', //默认时间
      pageNum: 1,
      loading: false, //加载状态
      finished: false, //是否全部加载完毕
      evaluateName: '',
      this_: this,
      orgName: JSON.parse(sessionStorage.getItem('SET_USER_ORGNAME')) || '', //机构名称
      cascaderValue: '', //机构ID
      show: false, //机构弹框显示隐藏

      fieldNames: {
        text: 'name',
        value: 'id',
        children: 'children'
      },
      taskList: [], //列表数据

      showStatus: false, //状态显示隐藏
      showDate: false, //月份显示隐藏
      fieldValue: '全部', //状态名称
      statusValue: ' ', //状态值
      columns: ['全部'], //状态数组
      columnsList: [], //状态数组
      currentDate: newDateDay(), //年份
      conditionShow: false //机构弹框显示隐藏
    }
  },
  components: {
    NavBar,
    OrgTree,
    Dialog,
    Icon,
    DatetimePicker,
    Picker,
    Col,
    Row,
    Cascader
  },

  created() {
    this.presentDate = new Date(newDateDay())
  },
  mounted() {
    this.init()
    this.cascaderValue = JSON.parse(window.sessionStorage.getItem('SET_USER_ORGID')) + ''
  },
  filters: {
    statusList(val, this_) {
      if (this_.columnsList && this_.columnsList.length > 0 && val) {
        let list = this_.columnsList.filter(item => item.dictValue == val)
        if (list && list.length > 0) {
          return list[0].dictLabel
        } else {
          return ''
        }
      } else {
        return ''
      }
    }
  },
  methods: {
     //日期选择组件formatter
     formatter(type, val) {
      if (type === 'month') {
        return `${val}月`;
      } else if (type === 'day') {
        return `${val}日`;
      } else if (type === 'year') {
        return `${val}年`;
      }
      return val;
    },
    //机构搜索
    getDataList(val) {
      this.cascaderValue = val.id
      this.orgName = val.name
      this.selectListAppHandler()
    },

    //判断评价按钮是否展示
    evaluateTure(item) {
      return true
      if (item.status == 0) {
        return true
      }
    },

    //初始化
    init() {
      //获取数据字典
      this.getDictHandler('evaluate_status', res => {
        this.columnsList = res
        this.columns = res.map(item => item.dictLabel)
        this.columns.unshift('全部')
      })
      //获取分页列表
      this.selectListAppHandler()
    },
    selectListAppHandler(type = 0, callback = () => {}) {
      let obj = {
        evaluateName: this.evaluateName,
        pageNum: this.pageNum,
        pageSize: 10
      }
      if (!type) {
        obj.pageNum = 1
        this.taskList = []
        this.pageNum = 1
      }
      if (this.statusValue) {
        obj.status = this.statusValue
      }

      obj.orgId = this.cascaderValue || JSON.parse(window.sessionStorage.getItem('SET_USER_ORGID')) + '' || ''

      if (this.currentDate) {
        obj.date = this.currentDate
      }

      //获取任务列表
      getEvaluatePage(obj).then(res => {
        let { code, rows, total } = res
        if (code == 200) {
          if (type) {
            this.taskList.push(...rows)

            if (this.taskList.length >= total) {
              //已加载完全部数据
              this.finished = true
              
            } else {
              callback()
            }
          } else {
            this.taskList.push(...rows)
            this.finished = false
            this.loading = false
            if (this.taskList.length >= total) {
              //已加载完全部数据
              this.finished = true
              
            }

            // this.taskList = rows || []
          }
        } else {
          this.finished = true
          this.taskList = []
        }
      })
    },

    //搜索选择状态时触发
    onConfirm(value, index) {
      this.fieldValue = value
      this.columnsList.forEach(item => {
        if (value == item.dictLabel) {
          this.statusValue = item.dictValue
        }
      })
      if (value == '全部') {
        this.statusValue = ' '
      }

      this.selectListAppHandler()
      this.showStatus = false
    },

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

      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 + '-' + d
    },

    //onLoad下拉刷新
    onLoad() {
      if (this.pageNum == 1) {
        this.pageNum = 2
      }
      this.loading = true
      this.selectListAppHandler(1, () => {
        this.pageNum++
        this.loading = false
      })
    },

    //评价
    evaluate(id) {
      this.$router.push('/evaluate/' + id + '_add')
    },

    //跳转详情
    goInfo(id) {
      this.$router.push('/evaluate/' + id + '_info')
    },

    onCancel() {
      this.show = false
      this.typeStatus = 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;
}
.van-cell__value {
  color: black;
  text-align: left;
}
</style>
