<template>
  <div class="app-container">
    <div style="width: 100%;color:#fff;padding:15px 0;background: linear-gradient(134deg,#7ac943 1%, #22b7f2);">
      <div style="width: 95%;margin: 0 auto;display: flex;align-items: center;">
        <van-image
          width="60"
          height="60"
          round
          src="https://img.yzcdn.cn/vant/leaf.jpg"
        />
        <div style="margin-left: 15px;">
          <p style="margin-top: 10px;font-size: 16px;">{{nickName}}
            <span style="font-size: 16px;" v-if="businessLevel=='1'">·调查员</span>
            <span style="font-size: 16px;" v-if="businessLevel=='2'">·审核员</span>
          </p>
        </div>
        <van-icon name="setting-o" style="margin-left: auto;" @click="gotoLink" size="25"/>
      </div>
    </div>
    <div style="background:#F5F5F5;width: 95%;margin: 20px auto 0;border-radius: 15px;overflow: hidden;">
      <van-grid :column-num="3" :border="false">
        <van-grid-item>
          <van-badge :content="total">
            <img
              width="50"
              height="50"
              :src="require('../../assets/images/housesteadSurvey/task1.png')"
              @click="active=1,checkBadge()"
            />
          </van-badge>
          <p id="basetext" :style="{color:(active==1?'#7AC943':'#000'),fontSize:'14px',marginTop:'5px'}">全部任务</p>
        </van-grid-item>
        <van-grid-item>
          <van-badge :content="done">
            <img
              width="50"
              height="50"
              :src="require('../../assets/images/housesteadSurvey/task2.png')"
              @click="active=2,checkBadge()"
            />
          </van-badge>
          <p :style="{color:(active==2?'#7AC943':'#000'),fontSize:'14px',marginTop:'5px'}">已完成</p>
        </van-grid-item>
        <van-grid-item>
          <van-badge :content="todo">
            <img
              width="50"
              height="50"
              :src="require('../../assets/images/housesteadSurvey/task3.png')"
              @click="active=3,checkBadge()"
            />
          </van-badge>
          <p :style="{color:(active==3?'#7AC943':'#000'),fontSize:'14px',marginTop:'5px'}">待调查</p>
        </van-grid-item>
      </van-grid>
    </div>
    <div style="margin:20px;">
      <div style="float:left;font-size: 16px;">任务列表</div>
      <div style="float:left;font-size: 16px;margin-left: 25%;">
        <span @click="queryAllxz()">{{deptName}}</span>
        <div id="dropList" v-if="showDropList"  style="width: 28vw; position: absolute; z-index: 99; left: 55%; margin-left: -16vw; border: 1px solid #E2E0E0;" >
          <van-cell id="vanCell"  v-for="(item, index) in xzList" :key="index" @click="xzChange(item)" style="position: relative; z-index: 999;">
            {{item.deptName}}
          </van-cell>
        </div>

      </div>
      <div style="float:right;font-size: 16px;">
        <van-checkbox v-model="checked"   @change="checkChange">仅我</van-checkbox>
      </div>
    </div>
    <div v-if="active==1" style="height:calc( 100vh - 350px);margin-top: 14%;overflow-y:auto;">
      <van-pull-refresh v-model="isLoadingtask" @refresh="onRefreshtask" :immediate-check="false">
      <van-cell  v-for="(item,index) in totalList" :key="item.id" size="small" @click.native="setCookies(item)" style="border-radius: 16px;
    box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16); width:95%;margin:0px auto 20px;height: 110px;">
        <template #title>
          <div>{{item.rwmc}}  <span :style="{'color':chooseColor(item),'background':chooseColor2(item),'display':'inline-block','padding':'4px 2px','line-height':'1','border-radius':'5px'}">{{item.zjdwcsl}}/{{item.zjdsl}}</span></div>
        </template>
        <template #default>
          <van-circle
            v-model="item.reportZrzNumber"
            :rate="item.reportNhNumber"
            :stroke-width="100"
            :speed="100"
            size="70%"
            layer-color="#ebedf0"
            :color="chooseColor(item)"
            :text="item.zjdwcsl!=0?(item.zjdwcsl == item.zjdsl?100+'%':(((item.zjdwcsl/item.zjdsl)*100).toFixed(2))+'%'):0+'%'"
          />
        </template>
        <template #label>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="clock" color="#22b7f2" style="margin-right:5px;"/>{{item.jhkssj}}~{{item.jhjssj}}</p>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="manager" color="#22b7f2" style="margin-right:5px;"/>{{item.rwzxr}}</p>
        </template>
      </van-cell>
      </van-pull-refresh>
    </div>
    <div v-if="active==2" style="height:calc( 100vh - 350px);margin-top: 14%;overflow-y:auto;">
      <van-pull-refresh v-model="isLoadingtask" @refresh="onRefreshtask">
      <van-cell  v-for="(item,index) in doneList" :key="item.id" size="small" @click.native="setCookies(item)" style="border-radius: 16px;
    box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16); width:95%;margin:0px auto 20px;height: 110px;">
        <template #title>
          <div>{{item.rwmc}}     <span :style="{'color':chooseColor(item),'background':chooseColor2(item),'display':'inline-block','padding':'4px 2px','line-height':'1','border-radius':'5px'}">{{item.zjdwcsl}}/{{item.zjdsl}}</span></div>
        </template>
        <template #default>

          <van-circle
            v-model="item.reportZrzNumber"
            :rate="item.reportNhNumber"
            :stroke-width="100"
            :speed="100"
            size="70%"
            layer-color="#ebedf0"
            :color="chooseColor(item)"
            :text="item.zjdwcsl!=0?(item.zjdwcsl == item.zjdsl?100+'%':(((item.zjdwcsl/item.zjdsl)*100).toFixed(2))+'%'):0+'%'"
          />
        </template>
        <template #label>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="clock" color="#22b7f2" style="margin-right:5px;"/>{{item.jhkssj}}~{{item.jhjssj}}</p>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="manager" color="#22b7f2" style="margin-right:5px;"/>{{item.rwzxr}}</p>
        </template>
      </van-cell>
      </van-pull-refresh>
    </div>
    <div v-if="active==3" style="height:calc( 100vh - 350px);margin-top: 14%;overflow-y:auto;">
      <van-pull-refresh v-model="isLoadingtask" @refresh="onRefreshtask">
      <van-cell  v-for="(item,index) in todoList" :key="item.id" size="small" @click.native="setCookies(item)" style="border-radius: 16px;
    box-shadow: 0px 3px 5px 0px rgba(0,0,0,0.16); width:95%;margin:0px auto 20px;height: 110px;">
        <template #title>
          <div>{{item.rwmc}}   <span :style="{'color':chooseColor(item),'background':chooseColor2(item),'display':'inline-block','padding':'4px 2px','line-height':'1','border-radius':'5px'}">{{item.zjdwcsl}}/{{item.zjdsl}}</span></div>
        </template>
        <template #default>
          <van-circle
            v-model="item.reportZrzNumber"
            :rate="item.reportNhNumber"
            :stroke-width="100"
            :speed="100"
            size="70%"
            layer-color="#ebedf0"
            :color="chooseColor(item)"
            :text="item.zjdwcsl!=0?(item.zjdwcsl == item.zjdsl?100+'%':(((item.zjdwcsl/item.zjdsl)*100).toFixed(2))+'%'):0+'%'"
          />
        </template>
        <template #label>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="clock" color="#22b7f2" style="margin-right:5px;"/>{{item.jhkssj}}~{{item.jhjssj}}</p>
          <p style="display: flex;align-items: center;margin-top: 5px;"><van-icon name="manager" color="#22b7f2" style="margin-right:5px;"/>{{item.rwzxr}}</p>
        </template>
      </van-cell>
      </van-pull-refresh>
    </div>

  </div>
</template>

<script>
  import {addTask, complete, delTask, exportTask, getTask, listTask,getTownInfo, publish, updateTask, userList} from "@/api/homesteadSurvey/index";
  import {changeDept} from "@/api/homesteadSurvey/zjdzd";
  import {getInfo} from "../../api/login";
  import CircleProccess from "@/components/circleProccess.vue";
  import {updateUserPwd} from "../../api/onlineHome/my";
  import {Dialog} from "vant";
  import {sysConfig} from "../../api/homesteadSurvey";
  import axios from "axios";
    export default {
      name: "homesteadIndex",
      components: {
        CircleProccess
      },
      data() {
        return {
          currentRate1:0,
          currentRate2:0,
          currentRate3:0,
          active: 1,
          show: false,
          loading: false,
          finished: false,
          checked:true,
          height:200,
          phone:null,
          activeKey:0,
          currentRate: 0,
          deptName:"全部乡镇",
          showDropList:false,
          total:0,
          totalList:[],
          xzList:[],
          done:0,
          doneList:[],
          todo:0,
          todoList:[],
          gradientColor:{

          },
          // 查询参数
          queryParams: {
            rwwczt: null,
            rwfbzt:"PUBLISHED",
            rwzxr:null
          },
          // 切换查询参数
          switchParams: {
            rwwczt: null,
            rwfbzt:"PUBLISHED",
            rwzxr:null
          },
          nickName:"",
          text:0,
          isLoadingtask:false,
          countqb:0,
          countyhc:0,
          countwhc:0,

          //宅调人员列表
          taskExecutortions:[],
          //当前用户级别
          businessLevel:1,
          // 字典列表json
          dictTypeList:null,
        };
      },
      created(){
        userList().then(response => {
          this.taskExecutortions = response.data
        })
        if(this.checked){
          setTimeout(() => {
            let userId = this.$store.state.user.userId;
            console.info(userId);
            this.nickName = this.$store.state.user.nickName;
            this.$set(this.queryParams, "rwzxr", userId);
            this.$set(this.queryParams, "params", {deptId:100});
            this.getList();
          },1000)

        }else{
          this.$set(this.queryParams, "rwzxr", null);
          this.getList();
        }
        getTownInfo(100).then(response => {
          this.xzList = response.data
          let qbxz = {deptId:100,deptName:"全部乡镇"}
          this.xzList.unshift(qbxz);
        })

        axios.get('../static/dictyType.json').then(res => {
          this.dictTypeList = res.data
        })

     },
      mounted(){

        // 监听屏幕方向
        let self = this;
        window.addEventListener(
          "onorientationchange" in window ? "orientationchange" : "resize",
          function() {
            if (window.orientation === 90 || window.orientation === -90) {
              // 横屏
            }
          },
          false
        );
        if(this.$cookies.get("url")==null||this.$cookies.get("url")==""){
          this.getBaseUrl()
        }
      },
      methods: {
        getBaseUrl(){
          let params={
            configKey: "system.attachment.url"}
          sysConfig(params).then(response => {
            this.$cookies.set("url",response.rows[0].configValue)
          })
        },
        qiehuan(deptId){
          changeDept(deptId).then(response => {
            this.$router.push({name:'homesteadList'})
          });
        },
        chooseValue(item){
            let a = ((item.confirmZjdzdNumber/item.reportZjdzdNumber)*100).toFixed()
          return a
        },
        chooseColor(item){
          let a = ((item.confirmZjdzdNumber/item.reportZjdzdNumber)*100).toFixed()
            if(a < 50){
              return  '#FA5353'
            }else if(a < 100){
              return '#22B7F2'
            }else{
              return '#85d824'
            }
          return  '#FA5353'
        },
        chooseColor2(item){
          let a = ((item.confirmZjdzdNumber/item.reportZjdzdNumber)*100).toFixed()
            if(a < 50){
              return  'rgba(250,83,83,0.22)'
            }else if(a < 100){
              return 'rgba(34,183,242,0.22)'
            }else{
              return 'rgba(122,201,67,0.22)'
            }
          return  '#FA5353'
        },
        taskExecutorChange(row){
          if(row.taskExecutor!=null&&row.taskExecutor!=""&&row.taskExecutor.length>1){
            var actions = [];
            row.taskExecutor.map(res => {
              for(let i = 0;i<this.taskExecutortions.length;i++){
                if(res == this.taskExecutortions[i].userName){
                  actions.push(this.taskExecutortions[i].nickName)
                }
              }
            })
            return actions.join(',');
          }else{
            var actions = [];
            Object.keys(this.taskExecutortions).some((key) => {
              if (this.taskExecutortions[key].userName == ('' + row.taskExecutor)) {
                actions.push(this.taskExecutortions[key].nickName);
                return true;
              }
            })
            return actions.join(',');
          }
        },
        onRefreshtask(){
          this.isLoadingtask = false;
          let _this = this
          if(this.active==1){
            let params = {
              "rwfbzt":"PUBLISHED",
              "params":{deptId:this.$store.state.user.loginDeptId},
              "pageNum": this.countyhc+1,
              "pageSize":10,
            }
            listTask(params).then((response) => {
              if(response.rows.length>0&&this.doneList.length<response.total){
                response.rows.map(res => {
                  this.doneList.unshift(res)
                })
                this.countyhc++
              }
            });
          }else if(this.active==2){
            let params = {
              "rwwczt": "PUBLISHED",
              "rwfbzt":"PUBLISHED",
              "params":{deptId:this.$store.state.user.loginDeptId},
              "pageNum": this.countwhc+1,
              "pageSize":10,
            }
            listTask(params).then((response) => {
              if(response.rows.lenght>0&&this.todoList.length<response.total){
                response.rows.map(res => {
                  this.todoList.unshift(res)
                })
                this.countwhc++
              }
            });
          }else{
            let params = {
              "rwwczt": "UNPUBLISHED",
              "rwfbzt":"PUBLISHED",
              "params":{deptId:this.$store.state.user.loginDeptId},
              "pageNum": this.countqb+1,
              "pageSize":10,
            }
            listTask(params).then((response) => {
              if(response.rows.length>0&&this.totalList.length<response.total){
                response.rows.map(res => {
                  this.totalList.unshift(res)
                })
                this.countqb++
              }
            });
          }
        },
        checkChange(e){
          if(e){
            let userId = this.$store.state.user.userId;
            this.$set(this.queryParams, "rwzxr", userId);
            this.getList();
          }else{
            this.$set(this.queryParams, "rwzxr", null);
            this.getList();
          }
          this.queryNum();
        },
        setCookies(item){
       /*   if(this.$cookies.get("upload")==null||this.$cookies.get("upload")==0){
            Dialog.confirm({
              title: '提示',
              message: '当前未选择批量上传，可能会由于网络原因上传图片缓慢，是否继续？',
            })
              .then(() => {
                // on confirm
                this.$router.push({name:'homesteadList'})
                this.$cookies.set("item",JSON.stringify(item));
              })
              .catch(() => {
                // on cancel
              });
          }else{
            this.$router.push({name:'homesteadList'})
            this.$cookies.set("item",JSON.stringify(item));
          }*/
          let userId = this.$store.state.user.userId;
          let businessLevel = this.$store.getters.businessLevel;
          if(businessLevel === "2" || item.rwzxrIds.includes(userId)){
            localStorage.setItem("surveyItem",JSON.stringify(item));
            this.qiehuan(item.deptId);
          }else{
            this.$dialog.alert({
              message: "没有该任务的调查权限！",
            });
          }

        },
        queryAllxz(){
          if (this.showDropList) {
            this.showDropList = false;   // div显示会阻挡下边的选择框和输入框，showDropList控制下拉框是否显示
          } else {
            this.showDropList = true;
          }
        },
        xzChange(val){
          this.deptName = val.deptName;
          this.showDropList = false;
          if(val.deptId != 100){
            this.$set(this.queryParams, "parentId", val.deptId);
            this.$set(this.switchParams, "parentId", val.deptId);
          }else{
            this.$set(this.queryParams, "parentId", null);
            this.$set(this.switchParams, "parentId", null);
          }
          this.getList();
          this.queryNum();
        },
        checkBadge(){
          this.getList();
          this.queryNum();
        },
        queryNum(){
          if(this.checked){
            let userId = this.$store.state.user.userId;
            this.$set(this.switchParams, "rwzxr", userId);
            listTask(this.switchParams).then(response => {
              this.done = 0;
              this.todo = 0;
              this.total = response.total;
              response.rows.map(res => {
                if(res.rwwczt == "FINISHED"){
                  this.done+=1
                }else{
                  this.todo+=1
                }
                // if(res.rwzxr!=null&&res.rwzxr!=''){
                //   res.rwzxr = res.rwzxr.split(',')
              })

            });
          }else{
            this.$set(this.switchParams, "rwzxr", null);
            listTask(this.switchParams).then(response => {
              this.done = 0;
              this.todo = 0;
              this.total = response.total;
              response.rows.map(res => {
                if(res.rwwczt == "FINISHED"){
                  this.done+=1
                }else{
                  this.todo+=1
                }
              })
            });
          }
        },
        getList() {
          this.totalList = []
          this.doneList = [];
          this.todoList = [];
          if(this.active==1) {
            this.$set(this.queryParams, "rwfbzt", "PUBLISHED");
            this.$set(this.queryParams, "rwwczt", null);
            this.$set(this.queryParams, "pageNum",  this.countyhc + 1);
            this.$set(this.queryParams, "pageSize",  10);
          }else if(this.active==2) {
            this.$set(this.queryParams, "rwwczt", "FINISHED");
            this.$set(this.queryParams, "rwfbzt", "PUBLISHED");
            this.$set(this.queryParams, "pageNum",  this.countwhc + 1);
          }else{
            this.$set(this.queryParams, "rwwczt", "UNFINISHED");
            this.$set(this.queryParams, "rwfbzt", "PUBLISHED");
            this.$set(this.queryParams, "pageNum",  this.countqb + 1);
          }
          listTask(this.queryParams).then(response => {
            if(this.active==1) {
              this.done = 0;
              this.todo = 0;
              this.total = response.total;
            }
            response.rows.map(res => {
              console.info(this.active);
              if(this.active==1) {
                if(res.rwwczt == "FINISHED"){
                  this.done+=1
                  this.doneList.push(res);
                }else{
                  this.todo+=1
                  this.todoList.push(res);
                }
                // if(res.rwzxr!=null&&res.rwzxr!=''){
                //   res.rwzxr = res.rwzxr.split(',')
                // }
                this.totalList = response.rows;
              }else if(this.active==2) {
                this.doneList.push(res);
              }else{
                this.todoList.push(res);
              }
            })

          });
        },
        gotoLink(){
          this.$router.push('/homesteadSurvey/settle')
        },
      },
    }
</script>

<style  scoped>
  >>> .van-cell__title{
    flex:3
  }
  >>> .sss {
    display: flex;
  }
  >>> .title:before
  {
    content:"";
    width: 6px;
    height: 20PX;
    background: #7ac943;
    border-radius: 3px;
    position:absolute;
    left:0;
    bottom:0;
  }
</style>
