<template>
  <div>
    <van-nav-bar
      title="我的事项"
      fixed
      placeholder
      left-arrow
      @click-left="goOnlineHomeIndex"
    />
    <van-tabs v-model="activeName" title-active-color="#1D6FE9" color="#1D6FE9" line-width="20px" swipeable @click="getList(0)">
      <van-tab title="待办" name="1" url="#1">
      </van-tab>
      <van-tab title="已办" name="2" url="#2">
      </van-tab>
    </van-tabs>
    <van-pull-refresh v-model="refreshing" @refresh="getList(0)">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :immediate-check="false"
        @load="getList('+1')"
      >
        <van-cell-group @click="goDetail(item)" v-for="(item,index) in taskList" :key="index" style="width: 96%;margin:2%;border-radius: 6px;overflow: hidden;padding-top: 10px;padding-bottom: 10px;box-shadow: 0px 3px 6px 0px rgba(0,0,0,0.16);">
      <van-cell>
        <template #title>
          <van-row style="">
            <van-col span="23" :offset="1">

              <h3 style="display: inline-block;line-height: 30px;margin-left: 6px;width: 100%;overflow: hidden;">
                <van-image
                  height="20"
                  width="20"
                  style="vertical-align: middle;margin-right: 10px"
                  src="../../../static/images/onlineHome/done.png"></van-image>{{item.projectName}}</h3>
            </van-col>
          </van-row>
        </template>
      </van-cell>
      <van-cell>
        <template #title>
          <van-row>
            <van-col span="6" :offset="1">
              <p style="color: #878787">{{item.createTime?item.createTime.substring(0,10):item.startTime.substring(0,10)}}</p>
            </van-col>
            <van-col span="10" :offset="1">
              <p style="color: #878787">{{item.formData.activityBusinessType}}</p>
            </van-col>
            <van-col span="5" :offset="1">
              <p style="font-size: 14px;font-weight:bold;text-align: right;color: #1D6FE9">{{activeName=='1'?'待审批':'已审批'}}</p>
            </van-col>
          </van-row>
        </template>
      </van-cell>
    </van-cell-group>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
    import onlineHomeIndex from "../onlineHomeIndex";
    import {ListDone, ListTodo} from "../../api/onlineHome/done";

    export default {
      components: {
        onlineHomeIndex
      },
        name: "done",
      data(){
        return{
          taskList:[],
          activeName:this.$route.query.activeName?this.$route.query.activeName:'1',
          total:0,
          queryParams: {
            pageNum: 1,
            pageSize: 100,
          },
          activityBusinessTypeOptions:[],
          refreshing: false,
          loading: false,
          finished: false,
        }
      },
      created() {
        this.getDicts("activity_business_type").then((response) => {
          this.activityBusinessTypeOptions = response.data;
          if(this.$route.query.activeName){
            this.activeName = this.$route.query.activeName
          }
          this.getList();
        });

        this.$nextTick(() => this.activeName = (location.hash || '#1').substr(1));
        /*let self = this;
        window.onpopstate = function (event) {
          console.log(event, location);
          let state = event.state;
          if(state)
          {
            let activeName = state.type || '1';
            self.$nextTick(() => self.activeName = activeName);
          }
        };*/
      },
      methods: {
        goOnlineHomeIndex(){
          this.$router.push({name:'onlineHomeWorkbench'})
        },
        getList(target) {
          if(!target)
          {
            this.refreshing = true;
            this.finished = false;
            this.queryParams.pageNum = 1;
            this.taskList = []
          }
          else if(typeof(target) === 'number')
            this.queryParams.pageNum = target;
          else {
            this.queryParams.pageNum = eval(this.queryParams.pageNum + target.toString())
          }
          this.$set(this.queryParams, "systemType", '11'); // 4
          if(this.activeName=='1'){
            ListTodo(this.queryParams).then((response) => {
              console.log(response)
              if(response.rows.length === 0)
              {
                this.finished = true;
                return;
              }
              response.rows.map(res => {
                if(res.tableName?res.tableName.indexOf('house')>0:""){
                  res.tableName = '来自农村宅基地管理系统'
                }else if(res.tableName?res.tableName.indexOf('sys_seal')>0:""){
                  res.tableName = '来自银农直联审批管理系统'
                }else if(res.tableName?res.tableName.indexOf('yinnong')>0:""){
                  res.tableName = '来自银农直联审批管理系统'
                }
                if(this.activityBusinessTypeOptions){
                  this.activityBusinessTypeOptions.map(t => {
                    if(t.dictValue == res.formData.activityBusinessType){
                      res.formData.activityBusinessType = t.dictLabel
                      this.taskList.push(res)
                    }
                  });
                }
              })
            }).finally(() => {
              this.loading = false;
              this.refreshing = false;
            });
          }else{
            ListDone(this.queryParams).then((response) => {
              console.log(response)
              if(response.rows.length === 0)
              {
                this.finished = true;
                return;
              }
              response.rows.map(res => {
                if(res.tableName?res.tableName.indexOf('house')>0:""){
                  res.tableName = '来自农村宅基地管理系统'
                }else if(res.tableName?res.tableName.indexOf('sys_seal')>0:""){
                  res.tableName = '来自银农直联审批管理系统'
                }else if(res.tableName?res.tableName.indexOf('yinnong')>0:""){
                  res.tableName = '来自银农直联审批管理系统'
                }
                if(this.activityBusinessTypeOptions){
                  this.activityBusinessTypeOptions.map(t => {
                    if(t.dictValue == res.formData.activityBusinessType){
                      res.formData.activityBusinessType = t.dictLabel
                      this.taskList.push(res)
                    }
                  });
                }
              })
            }).finally(() => {
              this.loading = false;
              this.refreshing = false;
            });
          }
        },
        goDetail(item){
          console.log(item)
          let type = item.formData.processKey;
          //history.pushState({ type: this.activeName}, null);
          switch (type) {
            case 'baseApply':
            case 'landscope':
            case 'accepting':
              this.$router.push({name:'approvalForm',query: {id:item.formData.id,taskId:item.taskId,instanceId:item.formData.instanceId,type:item.type}})
                  break;
            case 'baseApplyWLHT':
            case 'landscopeWLHT':
            case 'acceptingWLHT':
              this.$router.push({name:'proposerLite',query: {id:item.formData.applyProposerId || item.formData.id,taskId:item.taskId,instanceId:item.formData.instanceId,type:item.type, processKey: item.formData.processKey}})
              break;
            case 'house_mortgage':
              this.$router.push({name:'mortgageDetail',query: {id: item.formData.id,taskId:item.taskId,instanceId:item.formData.instanceId,type:item.type}})
              break;
            case 'house_circulation':
              this.$router.push({name:'circulationDetail',query: {id: item.formData.id,taskId:item.taskId,instanceId:item.formData.instanceId,type:item.type}})
              break;

            case 'yinnong_transfer':
              this.$router.push({name:'approvalApproval',query: {id:item.formData.id,taskId:item.taskId,type:item.type}})
              break;
            case 'yinnong_cash':
              if(item.formData.cashType == '10'){
                this.$router.push({name:'approvalApproval10',query: {id:item.formData.id,taskId:item.taskId,type:item.type}})
                break;
              }
              if(item.formData.cashType == '11'){
                this.$router.push({name:'approvalApproval11',query: {id:item.formData.id,taskId:item.taskId,type:item.type}})
                break;
              }
              if(item.formData.cashType == '12'){
                this.$router.push({name:'approvalApproval12',query: {id:item.formData.id,taskId:item.taskId,type:item.type}})
                break;
              }
              default:
                console.log("Unknown processKey: " + type);
                break;
          }
        }
      },
    }
</script>

<style scoped>

</style>
