<template>
  <div class="app-container">

    <div class="header_main">

      <el-dropdown trigger="click" placement="bottom-start">
        <div class="select_address el-dropdown-link">
          <span class="sanjiao-right"></span>
          <p>{{cityName}}</p>
        </div>
        <el-dropdown-menu slot="dropdown" style="width: 8vw;text-align: center;">
          <el-dropdown-item v-for="(item,index) in deptOptions" :command="item.orgCode">
            <div class="selected" @click="chooseCity(item.orgCode)">
              <p>{{item.deptName}}</p>
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>

      <el-dropdown trigger="click" placement="bottom-start">
        <div class="select_address el-dropdown-link">
          <span class="sanjiao-right"></span>
          <p>{{townName}}</p>
        </div>
        <el-dropdown-menu slot="dropdown" style="width: 8vw;text-align: center;">
          <el-dropdown-item v-for="(item,index) in townList" :command="item.orgCode">
            <div class="selected" @click="chooseTown(item.orgCode)">
              <p>{{item.deptName}}</p>
            </div>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>

      <p class="todo_num">剩余镇：<span>{{ countySummary.numTown }}</span>个</p>
      <p class="todo_num1">剩余村庄：<span>{{ countySummary.numVillage }}</span>个</p>
      <p class="todo_num1">剩余合作社：<span>{{ countySummary.numBook }}</span>个</p>
      <p class="todo_num2">任务异常数：<span>{{ countySummary.abnormalCount }}</span>个</p>

    </div>

    <div class="main">

      <div class="left_main">
        <el-collapse accordion>
          <el-collapse-item v-for="(item,index) in list" :key="index">
            <template slot="title">
              <div class="left_main_tt">
                <p>东沙镇</p>
                <div class="tt_label">
                  <p>异常：{{item.numExcept}}</p>
                  <p>剩余村庄：{{item.numVillage}}</p>
                  <p>剩余合作社：{{item.num}}</p>
                </div>
              </div>
            </template>
            <el-table :data="item.villageList" style="width: 100%;">

              <el-table-column type="expand">
                <template slot-scope="props">
                  <div class="openTable">
                    <el-row v-for="(task,index) in props.row.taskList" :key="index">
                      <el-col :span="9">
                        <div class="tab_main">
                          {{task.bookName}}
                          <span class="tab_red" v-if="task.abnormalCount > 0">异常:{{task.abnormalCount}}</span>
                        </div>
                      </el-col>
                      <el-col :span="2">记账：{{task.voucherCount}}</el-col>
                      <el-col :span="2">合同：{{task.contracCount}}</el-col>
                      <el-col :span="2">其他：{{task.otherCount}}</el-col>
                      <el-col :span="9" style="text-align: right"><el-button type="primary" size="mini" @click="gotoRemoteSite(task)">进入账套</el-button></el-col>
                    </el-row>
                  </div>
                </template>
              </el-table-column>

              <el-table-column prop="orgName" label="村" show-overflow-tooltip />
              <el-table-column prop="num" label="合作社" show-overflow-tooltip />
              <el-table-column label="处理进度" show-overflow-tooltip>
                <template slot-scope="scope">
                  {{scope.row.numProcessFinish}}/{{scope.row.num}}
                </template>
              </el-table-column>
              <el-table-column prop="numExcept" label="异常任务" show-overflow-tooltip >
                <template slot-scope="scope">
                  {{scope.row.numExcept || '无'}}
                </template>
              </el-table-column>
              <el-table-column prop="value5" label="任务进度" show-overflow-tooltip >
                <template slot-scope="scope">
                  {{scope.row.finishCount}}/{{scope.row.allCount}}
                </template>
              </el-table-column>
              <el-table-column prop="handleNick" label="会计" show-overflow-tooltip />
              <el-table-column prop="distriDate" label="下发日期" show-overflow-tooltip />
              <el-table-column prop="endAt" label="截至日期" show-overflow-tooltip />
              <el-table-column label="操作" show-overflow-tooltip >
                <template slot-scope="scope">
                  <p class="backButton" @click="revokeVillageTask(scope.row)">撤回</p>
                </template>
              </el-table-column>

            </el-table>
          </el-collapse-item>
        </el-collapse>

      </div>

      <RightYearMonthSelector class="right_main" :year.sync="parms.orderYear" :month.sync="parms.orderMonth" @dateChanged="chooseMonth"/>

    </div>


  </div>
</template>

<script>
import RightYearMonthSelector from "@/components/framework/RightYearMonthSelector.vue";
import {date_format} from "@/utils/date";
import {cityTree} from "@/api/misc/misc_dept";
import {ENV} from "@/utils/env";
import {managerProcessingTaskList, managerCountyProcessingTaskSummary, revokeTask} from "@/api/agentcenter/task";

export default {
  name: "processing",
  components: {RightYearMonthSelector},
  data() {
    return {
      deptOptions: [],
      list: [],
      total: 0,
      parms: {
        countyCode: null,
        townCode: null,
        orderYear: date_format('%Y'),
        orderMonth: date_format('%m'),
      },
      countySummary: {
        numTown: 0,
        numBook: 0,
        abnormalCount: 0,
        numVillage: 0,
      }
    }
  },
  created() {
    this.getDeptTree();
  },
  methods:{
    getDeptTree() {
      cityTree({rootId: ENV.UserDeptId()}).then((resp) => {
        this.deptOptions = resp.data;
        if(this.deptOptions.length)
        {
          this.parms.countyCode = this.deptOptions[0].orgCode;
          this.getSummary();
          this.getTaskList();
        }
      });
    },
    getTaskList() {
      if(!this.parms.countyCode || !this.parms.orderYear || !this.parms.orderMonth)
        return;
      managerProcessingTaskList(this.parms).then((resp) => {
        this.list = resp.rows;
        this.total = resp.total;
      })
    },
    chooseCity(cmd) {
      this.parms.countyCode = cmd;
      this.parms.townCode = null;
      this.getTaskList();
      this.getSummary();
    },
    chooseTown(cmd) {
      this.parms.townCode = cmd;
      this.getTaskList();
    },
    chooseMonth(year, month) {
      this.getSummary();
      this.getTaskList();
    },
    gotoRemoteSite(task) {
      window.open(`/api/open/book/${task.orgCode}/${task.bookId}`, '_blank');
    },
    getSummary() {
      if(!this.parms.countyCode || !this.parms.orderYear || !this.parms.orderMonth)
        return;
      managerCountyProcessingTaskSummary(this.parms).then((resp) => {
        this.countySummary = resp.data;
      })
    },
    revokeVillageTask(row) {
      revokeTask({
        orderYear: row.orderYear,
        orderMonth: row.orderMonth,
        orgCode: row.orgCode,
      }).then((resp) => {
        this.$message.success('撤回成功');
        this.getSummary();
        this.getTaskList();
      });
    },
  },
  mounted(){

  },
  computed: {
    cityName() {
      if(!this.deptOptions.length)
        return '';
      let dept = this.deptOptions.find((x) => x.orgCode === this.parms.countyCode);
      return dept ? dept.deptName : '';
    },
    townList() {
      if(!this.deptOptions.length)
        return [];
      let dept = this.deptOptions.find((x) => x.orgCode === this.parms.countyCode);
      return dept ? dept.children || [] : [];
    },
    townName() {
      if(!this.townList.length)
        return '';
      let dept = this.townList.find((x) => x.orgCode === this.parms.townCode);
      return dept ? dept.deptName : '';
    },
  },
}
</script>

<style lang="scss" scoped>
  p{margin: 0;line-height: 1;}
  .app-container{
    background-color: #f5f6f8;
    width: 100%;
    height: 91vh;
  }

  .selected{
    display: flex;
    height: 4vh;
    p{
      width: 8vw;
      line-height: 4vh;
      text-align: center;
    }
  }
  .pickerTime{
    opacity: 0;
    position: absolute;
    left: 0px;
    top: 30px;
    width: 5px;
    height: 5px;
    z-index: -1;
  }
  .el-dropdown-link {
    cursor: pointer;
  }

  .title{
    display: flex;
    align-items: center;
    i{
      display: inline-block;
      width: 5px;
      height: 20px;
      background: #4d7cfe;
      border-radius: 5px;
      margin-right: 10px;
    }
    p{
      font-size: 16px;
    }
  }

  ::v-deep .el-collapse {
    border: none;
  }

  ::v-deep .el-collapse-item__header {
    border: none;
  }

  ::v-deep .el-collapse-item__content {
    background-color: #e2ebff;
  }

  ::v-deep .el-table__expanded-cell {
    background-color: #e2ebff;
  }

  ::v-deep .el-table {
    background-color: transparent;
  }

  ::v-deep .el-table .cell{
    text-align: center;
  }

  ::v-deep .el-table .el-table__cell{
    border: none!important;
  }
  ::v-deep .el-table::before{
    display: none;
  }
  ::v-deep .el-table .el-table__header-wrapper th{
    background-color: transparent;
  }
  ::v-deep .el-table tr{
    background-color: transparent;
  }

  .openTable{
    padding: 1vh 2vw;
    background-color: #ffffff;
    border-radius: 15px;
    overflow: hidden;
    width: calc(100% - 40px);
    margin: 0 auto;
    .num{
      color: #3976ff;
      display: inline-block;
      background-color: #e2ebff;
      width: 1.8vw;
      border-radius: 5px;
      margin-left: 5px;
    }
    .el-col{
      padding: 10px 0;
    }
    .tab_main{
      display: flex;
      span{
        color: #ffffff;
        background-color: #acabab;
        padding: 2px 0px;
        width: 4vw;
        display: inline-block;
        border-radius: 7px;
        margin-right: 1vw;
        text-align: center;
      }
      .tab_green{
        background-color: #209955;
      }
      .tab_red{
        background-color: #ef0027;
      }
    }
  }

  .header_main{
    display: flex;
    align-items: center;
    position: relative;
    .selected{
      width: 16vw;
      background-color: #ffffff;
      position: absolute;
      top: 4vh;
      left: 10vw;
      z-index: 9;
      border-radius: 8px;
      box-shadow: 0 10px 10px #dedfe1;
      padding: 1vh 0;
      div{
        display: flex;
        height: 4vh;
        p{
          width: 8vw;
          line-height: 4vh;
          text-align: center;
        }
      }
    }
    .select_address{
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 2vw;
      background-color: #ffffff;
      padding: 0px 0px;
      border-radius: 8px;
      box-shadow: 0 0 10px #dedfe1;
      width: 8vw;
      height: 4vh;
      overflow: hidden;
      &:nth-child(2){
        margin-right: 0vw;
      }
      &:nth-child(3){
        margin-right: 0vw;
        border-top-left-radius: initial;
        border-bottom-left-radius: initial;
      }
      .sanjiao-right {
        display: inline-block;
        border-left: 8px solid #000;
        border-left-color: initial;
        border-top: 5px solid transparent;
        border-bottom: 5px solid transparent;
        margin-right: 5px;
      }
      .select_time{
        text-align: center;
        width: 100%;
        height: 4vh;
        line-height: 4vh;
        background-color: #3976ff;
        color: #ffffff;
      }
    }
    .todo_num{
      margin-left: auto;
      span{
        font-size: 24px;
        color: #3976ff;
      }
    }
    .todo_num1{
      margin-left: 2vw;
      span{
        font-size: 24px;
        color: #3976ff;
      }
    }
    .todo_num2{
      margin-left: 2vw;
      span{
        font-size: 24px;
        color: #ef0027;
      }
    }
  }

  .main{
    display: flex;
    .left_main{
      width: 92%;
      margin-right: 0.57%;
      margin-top: 15px;
      border-radius: 8px;
      box-shadow: 4px 0px 10px rgba(0, 0, 0, 0.13);
      background: #fff;
      padding: 15px 20px;
      height: calc(91vh - 91px);
      .backButton{
        color: #3976ff;
        border-bottom: 1px solid #3976ff;
        display: inline-block;
        padding-bottom: 5px;
        cursor: pointer;
      }
      .left_main_tt{
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .tt_label{
          display: flex;
          align-items: center;
          p{
            margin-right: 2vw;
            &:nth-child(1){
              color: #ef0027;
            }
          }
        }
      }
    }

    .right_main{
      width: 7.43%;
      margin-top: 15px;
      border-radius: 8px;
      box-shadow: 4px 0px 10px rgba(0, 0, 0, 0.13);
      background: #fff;
      padding: 15px 20px;
      height: calc(91vh - 91px);
    }

  }

  .sanjiao-top {
    display: inline-block;
    border-bottom: 8px solid #414141;
    border-bottom-color: initial;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
  }

  .sanjiao-bottom {
    display: inline-block;
    border-top: 8px solid #414141;
    border-top-color: initial;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
  }

  .time_main{
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: #f5f6f8;
    color: #414141;
    width: 75%;
    margin: 0 auto;
    margin-top: 15px;
    padding: 15px 0;
    border-radius: 8px;
    position: relative;
    cursor: pointer;
    p{
      margin: 10px 0;
    }
  }

  .month_main{
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #414141;
    p{
      margin-top: 3vh;
      position: relative;
      &.active{
        color: #3976ff;
      }
      &:hover{
        color: #3976ff;
        cursor: pointer;
      }
      i{
        display: block;
        width: 5px;
        height: 5px;
        background-color: #fc5659;
        position: absolute;
        top: -5px;
        right: -5px;
        border-radius: 100%;
      }
    }

  }

</style>
