<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">
            <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="el-dropdown-link select_main">
          <div class="select_address">
            <span class="sanjiao-right"></span>
            <p>{{townName}}</p>
          </div>
<!--          <div class="select_address"></div>-->
        </div>
        <el-dropdown-menu slot="dropdown" style="width: 16vw;text-align: center;">
          <el-dropdown-item v-for="(item,index) in townList">
            <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>{{total}}</span>个</p>

    </div>

    <div class="main">

      <div class="left_main">
        <el-table :data="list" style="width: 100%;">
          <el-table-column prop="orgName" label="村" />
          <el-table-column prop="bookName" label="组织名称" show-overflow-tooltip />
          <el-table-column prop="voucherCount" label="记账笔数" >
            <template slot-scope="scope">
              {{scope.row.voucherCount}} <span class="num">7</span>
            </template>
          </el-table-column>
          <el-table-column prop="contracCount" label="合同数量" />
          <el-table-column prop="otherCount" label="其他" />
          <el-table-column prop="allCount" label="任务总数" />
          <el-table-column prop="distriDate" :formatter="dateFormatter" label="下发日期" sortable />
          <el-table-column prop="endAt" label="截止日期" sortable />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click="finishTask(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>

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

    </div>


  </div>
</template>

<script>
import {cityTree} from "@/api/misc/misc_dept";
import {ENV} from "@/utils/env";
import {accountingTodoTaskList, finishTask} from "@/api/agentcenter/accounting_task";
import {date, date_format} from "@/utils/date";
import RightYearMonthSelector from "@/components/framework/RightYearMonthSelector.vue";

export default {
  name: "todo",
  components: {RightYearMonthSelector},
  data() {
    return {
      deptOptions: [],
      list: [],
      total: 0,
      parms: {
        countyCode: null,
        townCode: null,
        orderYear: date_format('%Y'),
        orderMonth: date_format('%m'),
      },
    }
  },
  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.getTaskList();
        }
      });
    },
    chooseCity(cmd) {
      this.parms.countyCode = cmd;
      this.parms.townCode = null;
      this.getTaskList();
    },
    chooseTown(cmd) {
      this.parms.townCode = cmd;
      this.getTaskList();
    },
    getTaskList() {
      if(!this.parms.countyCode || !this.parms.orderYear || !this.parms.orderMonth)
        return;
      accountingTodoTaskList(this.parms).then((resp) => {
        this.list = resp.rows;
        this.total = resp.total;
      })
    },
    chooseMonth(year, month) {
      this.getTaskList();
    },
    dateFormatter(row, column, cellValue, index) {
      return cellValue ? date(cellValue, 'yyyy-MM-dd HH:mm:ss', 'yyyy-MM-dd') : '';
    },
    finishTask(row) {
      finishTask({
        taskId: row.id,
      }).then((resp) => {
        this.$message.success('完成成功');
        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;
  }
  .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;
    }
  }
  .num{
    color: #3976ff;
    display: inline-block;
    background-color: #e2ebff;
    width: 1.8vw;
    border-radius: 5px;
    margin-left: 5px;
  }
  ::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;
  }
  .selected{
    display: flex;
    height: 4vh;
    p{
      width: 8vw;
      line-height: 4vh;
      text-align: center;
    }
  }
  .header_main{
    display: flex;
    align-items: center;
    position: relative;
    .select_main{
      display: flex;
      align-items: center;
      & .select_address:nth-child(1){
        margin-right: 0vw;
       /* border-top-right-radius: initial;
        border-bottom-right-radius: initial;*/
      }
      & .select_address:nth-child(2){
        margin-right: 0vw;
        border-top-left-radius: initial;
        border-bottom-left-radius: initial;
      }
    }
    .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;
      .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;
      }
    }
  }

  .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);
    }

    .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);
    }

  }

</style>
