<template>
    <div class="app-container">
      <van-nav-bar
        left-arrow
        fixed
        placeholder
        @click-left="$router.back(-1)"
        @click-right="goAdd"
      >
        <template #title>
          <p style="font-weight: bold;">任务发布</p>
        </template>
        <template #right>
          <van-icon name="add" size="18" v-if="allowCUD"/>
        </template>
      </van-nav-bar>
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="getList"
      >
        <van-swipe-cell v-for="(item,index) in applicationList" :key="index">
          <van-cell :value="item.taskSubjectStatus" center>
            <template #title>
              <p class="van-ellipsis">{{item.title}}</p>
            </template>
            <template #icon>
              <van-icon name="../../../static/images/onlineHome/icon_zjd11.png" size="30" color="#539FFD" style="margin-right: 10px;" />
            </template>
            <template #label>
              <p>
                <b>上报{{(item.reportedNumber/item.distributeDeptNumber*100).toFixed(0)}}%</b>
                <van-progress :percentage="(item.reportedNumber/item.distributeDeptNumber*100).toFixed(0)" stroke-width="6" :show-pivot="false" style="width: 25%;display: inline-block;position:relative;top: -1px;" />
                <i style="margin-right: 0.5rem;"></i><span>{{item.reportDeadline}}</span>
              </p>
            </template>
          </van-cell>
          <template #right>
            <van-row>
              <van-col>
                <van-button color="#FFA63E" square :to="{name:'taskCloseCaseList', query: {id:item.id}}" type="info" class="delete-button" >
                    上报<br/>情况
                </van-button>
              </van-col>
              <van-col>
                <van-button color="#7DDA4F" v-if="allowCUD && item.taskSubjectStatus=='待下发'" square text="发布" :to="{name:'taskRelease', query: {id:item.id}}" type="danger" class="delete-button" />
              </van-col>
              <van-col>
                <van-button square text="修改" v-if="allowCUD && item.taskSubjectStatus=='待下发'" :to="{name:'taskLssueModify', query: {id:item.id}}" type="info" class="delete-button" />
              </van-col>
              <van-col>
                <van-button color="#FF4646" square text="删除" v-if="allowCUD && item.taskSubjectStatus=='待下发'" @click="deleteList(item.id,index)" type="danger" class="delete-button" />
              </van-col>
            </van-row>
          </template>
        </van-swipe-cell>
      </van-list>
    </div>
</template>

<script>
import { getList , removeList } from "@/api/onlineHome/homestead/task";
export default {
  name: "taskList",
  data() {
    return {
      applicationList:[],
      houseApplyStatus:[],
      taskSubjectStatusStatus:[],
      loading: false,
      finished: false,
      queryParams:{
        pageNum:1,
        pageSize:10,
        orderByColumn:'createTime',
        isAsc:'desc'
      }
    };
  },
  created() {
    this.houseGetDicts("task_subject_status").then((res) => {
      this.taskSubjectStatusStatus = res.data;
    });
  },
  computed: {
    allowCUD: function () {
      return this.$store.getters.businessLevel == '2'
    },
  },
  methods: {
    goAdd(){
      window.location = 'taskLssueAdd';
    },
    getList(){
      setTimeout(() => {
        getList(this.queryParams).then(response => {
          console.log(response)
          for (var i = 0; i < response.rows.length; i++) {
            response.rows[i].taskSubjectStatus = this.selectDictLabel(this.taskSubjectStatusStatus, response.rows[i].taskSubjectStatus)

            response.rows[i].title=response.rows[i].title.replace(/<[^>]+>/g,"").replace(/&nbsp;/ig,"");//截取html标签
            if(response.rows[i].distributeDeptNumber == null || response.rows[i].distributeDeptNumber == ''){
              response.rows[i].distributeDeptNumber = 1;
            }
            if(response.rows[i].reportedNumber == null){
              response.rows[i].reportedNumber = 1;
            }
            this.applicationList.push(response.rows[i]);
          }
          if(this.applicationList.length >= response.total){
            this.finished = true;
            return;
          }else{
            this.loading = false;
            this.queryParams.pageNum += 1 ;
          }
        });
      }, 1000);
    },
    deleteList(id,index){
      this.$dialog.confirm({
        message: '您确认删除吗？',
      })
      .then(() => {
        // on confirm
        this.applicationList.splice(index,1)
        removeList(id).then(res => {
          if(res.code = 200){
            this.$toast.success('删除成功');
          }
        });
      })
      .catch(() => {
        // on cancel
      });
    }
  },
}
</script>

<style scoped lang="scss">
  .app-container {
    padding: 0.2rem 3%;
  }
  /deep/.van-cell__title{
    flex: 0.7;
    width: 50%!important;
  }
  /deep/.van-cell__title span{
    font-family: Arial;
    font-weight: normal;
    float: right;
  }
  /deep/.van-cell__value{
    flex: 0.3;
    color: #1D6FE9;
    font-weight: bold;
  }
  /deep/.van-swipe-cell{
    margin-bottom: 0.2rem;
    border-radius: 0.2rem;
    overflow: hidden;
    box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);
  }
  /deep/van-ellipsis{
    font-weight: bold;
  }
  .delete-button {
    height: 100%;
  }
  .van-row{
    height: 100%;
  }
  .van-col{
    height: 100%;
  }
</style>
