<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="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">
            <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>
      <p class="todo_num2">待处理村庄：<span>{{numVillage}}</span>个</p>
      <p class="todo_num3">待处理任务：<span>{{allCount}}</span>个</p>

    </div>

    <div class="main">

      <div class="left_main">
        <el-table :data="list" style="width: 100%;" v-if="list.length > 0" @expand-change="townExpanded">

          <el-table-column type="expand">
            <template slot-scope="props">
              <div class="openTable">
                <el-table :data="props.row.villageList" style="width: 100%;background-color: transparent;">
                  <el-table-column prop="orgName" label="村" show-overflow-tooltip />
                  <el-table-column prop="numBook" label="合作社" show-overflow-tooltip />
                  <el-table-column prop="voucherCount" label="记账笔数" show-overflow-tooltip >
                    <template slot-scope="scope">
                      {{scope.row.voucherCount}} <span class="num">7</span>
                    </template>
                  </el-table-column>
                  <el-table-column prop="contracCount" label="合同数量" show-overflow-tooltip />
                  <el-table-column prop="otherCount" label="其他" show-overflow-tooltip />
                  <el-table-column prop="allCount" label="任务总数" show-overflow-tooltip />
                  <el-table-column label="操作">
                    <template slot-scope="scope">
                      <div class="setting">
                        <el-dropdown trigger="click" v-if="!scope.row.handleUser" @command="(e) => setTaskHandleUser(scope.row, e)">
                          <el-button type="primary" size="mini" icon="el-icon-edit" @click="getVillageTaskUserList(scope.row)"></el-button>

                          <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item v-for="(item, index) in villageTaskUserList" :key="index" :command="item">{{item.nickName}} - {{item.num}}</el-dropdown-item>
                          </el-dropdown-menu>
                        </el-dropdown>
                        <template v-else>
                          <img src="../../assets/images/toBeAllocated_icon_delete.png" alt="" @click="clearTaskHandleUser(scope.row)">
                          <span class="name">{{scope.row.handleNick}}</span>
                        </template>
                      </div>
                    </template>
                  </el-table-column>
                  <el-table-column prop="endAt" label="截至日期" show-overflow-tooltip />
                </el-table>
              </div>
            </template>
          </el-table-column>

          <el-table-column prop="townName" label="镇/街" show-overflow-tooltip />
          <el-table-column prop="numVillage" label="村账数量" show-overflow-tooltip />
          <el-table-column prop="numBook" label="合作社数量" show-overflow-tooltip />
          <el-table-column prop="voucherCount" label="记账笔数" show-overflow-tooltip >
            <template slot-scope="scope">
              {{scope.row.voucherCount}} <span class="num">7</span>
            </template>
          </el-table-column>
          <el-table-column prop="contracCount" label="合同数量" show-overflow-tooltip />
          <el-table-column prop="otherCount" label="其他" show-overflow-tooltip />
          <el-table-column prop="allCount" label="任务总数" show-overflow-tooltip />
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-dropdown trigger="click" v-if="!scope.row.expanded">
                <el-button type="primary" size="mini" icon="el-icon-edit" @click="getTownTaskUserList(scope.row)"></el-button>

                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item v-for="(item, index) in townTaskUserList" :key="index" :command="item">{{item.nickName}} - {{item.num}}</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <template v-else>
                <el-button type="danger" size="mini" icon="el-icon-close" @click="resetTaskUser(scope.row)"></el-button>
                <el-button type="success" size="mini" icon="el-icon-upload2" @click="setupTaskUser(scope.row)"></el-button>
              </template>
            </template>
          </el-table-column>

        </el-table>

        <img src="../../assets/images/toBeAllocated_icon_none.png" alt="" class="table_none" v-else>

      </div>

      <div class="right_main">

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

        <div class="right_bottom_main">

          <div class="title">
            <i></i>
            <p>任务看板</p>
          </div>

          <div class="right_top_block">
            <el-row>
              <el-col :span="8">姓名</el-col>
              <el-col :span="8" align="center">未完成</el-col>
              <el-col :span="8" align="center">任务期限</el-col>
            </el-row>
            <el-row v-for="(item,index) in taskUserList">
              <el-col :span="8">{{item.nickName}}</el-col>
              <el-col :span="8">{{item.num}}
                <template v-if="item.addNum > 0">
                  <span class="num">{{item.addNum}}</span><span class="num2">{{item.num + item.addNum}}</span>
                </template>
              </el-col>
              <el-col :span="8" align="center">
                <span class="no_yes" v-if="item.addNum === 0">未选中</span>
                <div class="num_input" v-else>
                  <span @click="reduceNum(item)">-</span>
                  <p>{{item.days}}</p>
                  <span @click="addNum(item)">+</span>
                </div>
              </el-col>
            </el-row>
          </div>

        </div>

      </div>

    </div>


  </div>
</template>

<script>
  import RightYearMonthHorizontalSelector from "@/components/framework/RightYearMonthHorizontalSelector.vue";
  import {cityTree} from "@/api/misc/misc_dept";
  import {ENV} from "@/utils/env";
  import RightYearMonthSelector from "@/components/framework/RightYearMonthSelector.vue";
  import {date_format} from "@/utils/date";
  import {distribTask, managerUndistribTaskList} from "@/api/agentcenter/task";
  import {taskUserList} from "@/api/agentcenter/task_user";

  export default {
    name: "processed",
    components: {RightYearMonthSelector, RightYearMonthHorizontalSelector},
    data() {
      return {
        deptOptions: [],
        list: [],
        total: 0,
        parms: {
          countyCode: null,
          townCode: null,
          orderYear: date_format('%Y'),
          orderMonth: date_format('%m'),
        },
        numBook: 0,
        numVillage: 0,
        allCount: 0,
        villageTaskUserList: [],
        townTaskUserList: [],
        taskUserList: [],
      }
    },
    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.getTaskUserList({orgCode: this.parms.countyCode});
            this.getTaskList();
          }
        });
      },
      chooseCity(cmd) {
        this.parms.countyCode = cmd;
        this.parms.townCode = null;
        this.getTaskList();
        this.getTaskUserList({orgCode: this.parms.countyCode});
      },
      chooseTown(cmd) {
        this.parms.townCode = cmd;
        this.getTaskList();
        this.getVillageTaskUserList({orgCode: this.parms.townCode});
      },
      addNum(item){
        item.days++;
      },
      reduceNum(item){
        if(item.days <= 0){return;}
        item.days--;
      },
      getTaskList() {
        if(!this.parms.countyCode || !this.parms.orderYear || !this.parms.orderMonth)
          return;
        managerUndistribTaskList(this.parms).then((resp) => {
          resp.rows.forEach((x) => {
            x.expanded = false;
          })
          this.list = resp.rows;
          this.total = resp.total;
          this.numBook = resp.numBook;
          this.numVillage = resp.numVillage;
          this.allCount = resp.allCount;
        })
      },
      chooseMonth(year, month) {
        this.getTaskList();
      },
      getVillageTaskUserList(row) {
        this.villageTaskUserList = [];
        taskUserList({
          orgCode: row.orgCode,
        }).then((resp) => {
          this.villageTaskUserList = resp.rows;
        })
      },
      getTownTaskUserList(row) {
        this.villageTaskUserList = [];
        taskUserList({
          orgCode: row.townCode,
        }).then((resp) => {
          this.townTaskUserList = resp.rows;
        })
      },
      getTaskUserList() {
        if(!this.parms.countyCode)
          return;
        this.taskUserList = [];
        taskUserList({
          orgCode: this.parms.countyCode,
        }).then((resp) => {
          resp.rows.forEach((x) => {
            x.days = 0;
            x.addNum = 0;
          });
          this.taskUserList = resp.rows;
        })
      },
      setTaskHandleUser(row, user) {
        this.$set(row, 'handleUser', user.userName);
        this.$set(row, 'handleNick', user.nickName);
        let globalUser = this.taskUserList.find((x) => x.userName === user.userName);
        globalUser.addNum++;
      },
      clearTaskHandleUser(row) {
        let globalUser = this.taskUserList.find((x) => x.userName === row.handleUser);
        globalUser.addNum--;
        this.$set(row, 'handleUser', '');
        this.$set(row, 'handleNick', '');
        if(!globalUser.addNum)
          globalUser.days = 0;
      },
      townExpanded(row, expandedRows) {
        let expand = expandedRows.includes(row);
        this.$set(row, 'expanded', expand);
      },
      resetTaskUser(row) {
        row.villageList.forEach((x) => {
          this.$set(x, 'handleUser', '');
          this.$set(x, 'handleNick', '');
        });
        this.taskUserList.forEach((x) => {
          x.addNum = 0;
          x.days = 0;
        })
      },
      setupTaskUser(row) {
        let arr = [];
        for(let v of row.villageList)
        {
          if(!v.handleUser)
            continue;
          let globalUser = this.taskUserList.find((x) => x.userName === v.handleUser);
          if(!globalUser.days)
          {
            this.$message.error('请设置任务期限');
            return;
          }
          let item = {
            handleUser: v.handleUser,
            orgCode: v.orgCode,
            days: globalUser.days,
          };
          arr.push(item);
        }
        if(!arr.length)
        {
          this.$message.error('请设置记账人');
          return;
        }
        let data = {
          orderYear: this.parms.orderYear,
          orderMonth: this.parms.orderMonth,
          items: arr,
        };
        distribTask(data).then((resp) => {
          this.$message.success('分配成功');
          this.getTaskUserList({orgCode: this.parms.countyCode});
          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;
  }

  .setting{
    display: flex;
    align-items: center;
  }
  .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-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;
  }

  .num{
    color: #3976ff;
    display: inline-block;
    background-color: #e2ebff;
    width: 1.8vw;
    border-radius: 5px;
    margin-left: 5px;
    text-align: center;
  }

  .num2{
    color: #ffffff;
    display: inline-block;
    background-color: #3976ff;
    width: 1.8vw;
    border-radius: 5px;
    margin-left: 5px;
    text-align: center;
  }

  .no_yes{
    color: #9a9a9a;
  }

  .num_input{
    display: flex;
    align-items: center;
    justify-content: center;
    span{
      background-color: #3976ff;
      color: #ffffff;
      width: 25px;
      height: 25px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 8px;
      cursor: pointer;
    }
    p{
      width: 3vw;
      border: 1px solid #3976ff;
      margin: 0 .3vw;
      color: #3976ff;
      height: 25px;
      line-height: 25px;
      border-radius: 8px;
    }
  }

  .table_none{
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .name{
    color: #666666;
    display: inline-block;
    background-color: #e2ebff;
    width: 3.5vw;
    border-radius: 5px;
    margin-left: 5px;
  }
  .openTable{
    background-color: #eef3fd;
    padding: 0 2.5vw 2vh;
    ::v-deep .el-table__body{
      background-color: #ffffff;
      border-radius: 15px;
      overflow: hidden;
    }
  }

  .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_num2{
      margin-left: 2vw;
      span{
        font-size: 24px;
        color: #3976ff;
      }
    }
    .todo_num3{
      margin-left: 2vw;
      span{
        font-size: 24px;
        color: #ef0027;
      }
    }
  }

  .main{
    display: flex;
    .left_main{
      width: 72%;
      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);
      position: relative;
    }

    .right_main{
      width: 27.43%;
      .right_top_main{
        margin-top: 15px;
        border-radius: 8px;
        box-shadow: 4px 0px 10px rgba(0, 0, 0, 0.13);
        background: #fff;
        padding: 15px 20px;
        .right_top_block{
          display: flex;
          align-items: center;
        }
      }
      .right_bottom_main{
        margin-top: 15px;
        border-radius: 8px;
        box-shadow: 4px 0px 10px rgba(0, 0, 0, 0.13);
        background: #fff;
        padding: 15px 20px;
        .right_top_block{
          ::v-deep .el-col-8{
            padding-top: 15px ;
          }
        }
      }
    }

  }

  .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: 15%;
    margin-top: 15px;
    padding: 15px 0;
    border-radius: 8px;
    height: 82px;
    position: relative;
    cursor: pointer;
    p{
      margin: 10px 0;
    }
  }

  .month_main{
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    margin-top: 15px;
    color: #414141;
    background-color: #f5f6f8;
    height: 82px;
    flex: 1;
    margin-left: 1vw;
    border-radius: 8px;
    p{
      /*margin-top: 3vh;*/
      width: 16%;
      position: relative;
      text-align: center;
      &.active{
        color: #3976ff;
      }
      &:hover{
        color: #3976ff;
        cursor: pointer;
      }
      i{
        display: block;
        width: 5px;
        height: 5px;
        background-color: #fc5659;
        position: absolute;
        top: -5px;
        right: 12px;
        border-radius: 100%;
      }
    }

  }

</style>
