<template>
  <div class="problem-item">
    <NavBar />
    <div class="page-container">
      <van-search v-model="query.searchKey" class="van-hairline--bottom" placeholder="请输入搜索关键词" />
      <org-tree v-model="query.orgId"  @change="refreshData"></org-tree>
      <div class="search-flex">
        <select-cell
          style="border-right: 1px solid #f5f5f5;"
          title="状态"
          :isAll="true"
          :border="false"
          v-model="query.confirmStatus"
          :data-list="getDictItem('question_confirm_status')"
          @change="refreshData"/>
        <div class="btn-box">
          <van-button plain type="warning" size="small" @click="clickAdd">录入介绍信</van-button>
        </div>
      </div>
      <div class="card-list">
        <Scroll
          ref="Scroll"
          @refresh="refreshData"
          @loadMore="getDataList"
          :pullup="pullup">
          <empty  v-if="!dataList || dataList.length === 0" />
          <card v-else v-for="(v,i) in dataList" :key="v.id">
              <van-cell :title="v.orgName">
                <template #extra>
                  <van-button
                    style="width: 60px;"
                    v-if="v.orgId==orgId && v.confirmStatus==0"
                    hairline
                    size="mini"
                    type="info"
                    @click.stop="clickItem(v.id,'confirm')">
                    隐患确认
                  </van-button>
                  <van-button
                    style="width: 60px;"
                    v-else-if="v.submitorId== id && v.confirmStatus==1"
                    hairline
                    size="mini"
                    type="info"
                    @click.stop="clickItem(v.id,'confirmDissent')">
                    异议审批
                  </van-button>
                  <van-button
                    style="width: 60px;"
                    v-else-if="v.orgId==orgId && v.confirmStatus==2 && v.reformStatus!=11"
                    hairline
                    size="mini"
                    type="info"
                    @click.stop="clickItem(v.id,'reform')">
                    整改
                  </van-button>
                  <van-button
                    style="display: block;width: 60px;"
                    v-else
                    hairline
                    size="mini"
                    type="info"
                    @click.stop="clickItem(v.id,'detail')">
                    详情
                  </van-button>
                </template>
                <template #label>
                  <div class="info-box">
                    <div class="info-desc">创建机构：<span>{{v.questionDesc}}</span></div>
                    <div class="info-desc">接待机构：<span>{{v.questionDesc}}</span></div>
                    <div class="info-item">介绍信有效期：<span>{{v.submitTime}}~{{v.submitTime}}</span></div>
                  </div>
                </template>
              </van-cell>
            </card>
        </Scroll>
      </div>
    </div>
  </div>
</template>
<script>
import NavBar from '@/components/NavBar'
import OrgTree from '@/components/orgTree'
import Scroll from '@/components/scroll/scroll'
import Card from '@/components/card'
import dateCell from '@/components/dateCell'
import selectCell from '@/components/selectCell'
import {dataList} from './api'
import {mapGetters} from "vuex";
import {formatDate} from "@/filters/filter";
export default {
  components: {
    NavBar,
    OrgTree,
    Scroll,
    Card,
    dateCell,
    selectCell,
  },
  data() {
    return {
      query:{
        submitTime:`${formatDate(new Date(),'YYYY-MM-DD')}`,
        orgId:null,
        confirmStatus:null,
        pageSize:10,
        pageNum:1,
      },
      planList:[],
      loading:false,
      finished:false,
      columns:[
        {
          field: "index",
          key: "index",
          title: "序号",
          width: 50,
          align: "center",
          renderBodyCell: ({ row, column, rowIndex }, h) => {
            return ++rowIndex;
          },
        },
        { field: "orgName", key: "a", title: "单位名称",align: "center"},
        { field: "shouldFinish", key: "b", title: "应完成数", align: "center" },
        { field: "finish", key: "c", title: "已完成数",align: "center" },
        { field: "finishRate", key: "d", title: "完成率", align: "center" },
      ],
      dataList:[],
      pullup:false,
      dicts:['question_confirm_status','question_reform_status']
    }
  },
  mounted() {
    this.initData();
  },
  computed:{
    ...mapGetters(['orgId','id','dictionary']),
  },
  methods: {
    clickAdd(){
      this.$router.push({
        path:'/introAdd',
        query:{type:'add'}
      });
    },
    refreshData(){
      this.pullup = true;
      this.query.pageNum = 1;
      this.total = 0;
      this.dataList = [];
      this.getDataList();
    },
    //初始化数据
    initData(){
      this.query.orgId = this.orgId;
      this.query.submitTime = formatDate(new Date(),'YYYY-MM-DD');
      this.getDataList();
    },
    //获取数据列表
    getDataList(){
      if(!this.query.orgId) return this.$toast('请选择机构');
      if( this.dataList.length !== 0 && this.dataList.length >= this.total) {
        this.pullup = false;
        this.$toast('已加载完毕');
        return;
      }
      let data = {
        ...this.query,
      }
      dataList(data).then(res=>{
        if(res.total === '0'){
          this.pullup = false;
          this.$toast('已加载完毕');
          return
        }
        this.total = res.total;
        if(this.dataList.length < res.total) {
          this.dataList = [...this.dataList,...res.rows] ;
          this.pullup = true;
          this.query.pageNum++;
          this.$refs.Scroll.refresh();
        }
      })
    },
    clickItem(id,type){
      this.$router.push({
        path:'/problemDetail',
        query:{id,type}
      });
    }
  }
}
</script>
<style lang="scss">
.card {
  >.van-cell{
    padding: 0;
  }
}
.van-cell-group{
  margin-bottom: 20px;
}
.van-cell-group:last-child{
  margin-bottom: 0;
}
.vue-table-root{
  tr,td,th{
    font-size: 25px!important;
    color:#666!important;
  }
}
</style>
<style lang="scss" scoped>
.problem-item{

}
.app-container{

}
.card-list{
  padding:0 20px 20px 20px;
  height: calc(100vh - 514px);
  overflow:  auto;
}
.card-num{
  display: flex;
  align-items: center;
  font-size: 28px;
  color: #009dff;
}
.search-flex{
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #f5f5f5;
  >div{
    width: 50%;
  }
}
.btn-box{
  width: 100%;
  height: 100%;
  padding: 30px;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}
.info-box{
  color:#555;
}
.info-desc{
  padding-top: 10px;
  min-height: 50px;
  line-height: 36px;
  max-height: 250px;
  display: -webkit-box;
  -webkit-line-clamp: 3; /* 限制显示为3行 */
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  >span{
    color:#999;
  }
}
.info-item{
  height: 50px;
  line-height: 50px;
  >span{
    color:#999;
  }
}
</style>
