<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
          v-model="taskDate"
          :picker-options="pickerOptions"
          class="filter-item"
          type="daterange"
          format="yyyy年MM月dd日"
          value-format="yyyy-MM-dd"
          align="left"
          unlink-panels
          range-separator="至"
          start-placeholder="任务开始时间"
          end-placeholder="任务结束时间"
        />
        <el-select class="filter-item" v-model="taskTypeId" clearable placeholder="请选择任务类型">
          <el-option
            v-for="item in taskType"
            :key="item.id"
            :label="item.label"
            :value="item.id">
          </el-option>
        </el-select>
        <el-cascader
              v-model="depid"
              :options="depData"
              :props="props"
              placeholder="请选择部门"
              class="filter-item"
              clearable
              filterable
              change-on-select
            />
            <el-input v-model="keyword" placeholder="请输入关键字" class="filter-item"/>
            <el-input v-model="tasker" placeholder="请输入任务人" class="filter-item"/>
            <!-- <el-input v-model="parentname" placeholder="请输入父级任务名称" class="filter-item"/> -->
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="btn_search">搜索</el-button>
      <el-button v-permission="'HY_add'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add(0)">添加</el-button>
      <el-button v-permission="'HY_import'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_import">导入</el-button>
      <el-button v-permission="'HY_deletes'" type="primary" class="filter-item" icon="el-icon-plus" @click="btn_deletes">批量删除</el-button>
    </div>

    <el-table 
    v-loading="loading" 
    :data="dataLists" 
    border
    row-key="F_ID"
    default-expand-all
    :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    @selection-change="changeSelects">
       <el-table-column type="selection" width="40" fixed></el-table-column>
      <el-table-column prop="F_DeptName" label="部门名称" align="center" min-width=""/>
      <el-table-column prop="F_ParentName" label="父级任务名称" align="center" min-width=""/>
      <el-table-column prop="F_Name" label="任务名称" align="center" min-width=""/>
      <el-table-column prop="F_TaskerName" label="任务人" align="center" min-width=""/>
      <el-table-column prop="F_Money" label="目标金额" align="center" min-width=""/>
      <el-table-column label="任务进度" align="center" >
          <template slot-scope="scope">
              <el-progress style="margin-top:10px;" :show-text='false' :percentage="(scope.row.F_Taskprogress-0)>100?100:(scope.row.F_Taskprogress-0)"></el-progress>
              <span>{{formatNum(scope.row.F_Taskprogress)}}</span>
          </template>
      </el-table-column>
      <el-table-column prop="F_StartTime" label="任务开始时间" align="center" min-width=""/>
      <el-table-column prop="F_EndTime" label="任务结束时间" align="center" min-width=""/>
      <el-table-column label="任务类型" align="center" min-width="">
          <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.F_Type==0">日任务</el-tag>
              <el-tag type="info" v-else-if="scope.row.F_Type==1">周任务</el-tag>
              <el-tag  v-else-if="scope.row.F_Type==2">月任务</el-tag>
          </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" min-width="">
          <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.F_State==0">开始</el-tag>
              <el-tag type="info" v-else-if="scope.row.F_State==1">暂停</el-tag>
              <el-tag type="danger" v-else-if="scope.row.F_State==2">终止</el-tag>
          </template>
      </el-table-column>
      <el-table-column prop="F_Content" label="任务内容" align="center" min-width=""/>
      <el-table-column label="操作" width="320" align="center" class-name="oparate_btn" fixed="right">
        <template slot-scope="scope">
          <el-button v-permission="'HY_addChildren'" size="mini" plain type="text" @click="btn_addChildren(scope.row.F_ID)">添加子任务</el-button>
          <el-button v-permission="'HY_importChildren'" size="mini" plain type="text" @click="btn_importChildren(scope.row.F_ID)">导入子任务</el-button>
          <el-button v-permission="'HY_edit'" size="mini" plain type="text" @click="btn_edit(scope.row.F_ID.toString(),scope.row.F_Parentid.toString())">编辑</el-button>
          <el-button v-permission="'HY_detail'" size="mini" plain type="text" @click="btn_detail(scope.row.F_ID.toString())">详情</el-button>
          <el-button v-permission="'HY_delete'" size="mini" plain type="text" @click="btn_delete(scope.row.F_ID)">删除</el-button>
          <el-button v-permission="'HY_administration'" size="mini" plain type="text" @click="btn_administration(scope.row.F_ID)">管理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="pageParams.total > 0"
      :total="pageParams.total"
      :pageindex.sync="pageParams.pageindex"
      :pagesize.sync="pageParams.pagesize"
      class="pagination"
      @pagination="getList" />
  </div>
</template>

<script>
import {getDepartmentTreeList} from '@/api/commonAPI';
import { getTaskLists,deleteTask } from '@/api/taskManagement/taskManagement'
import addOrEdit from './common/addOrEdit'
import detail from './common/detail'
import importFile from './common/importFile'
import administration  from './common/administration'
import { pickerOptions } from '@/utils'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装

export default {
  name: 'BlackList',
  components: {
    Pagination
  },
  data() {
    return {
      loading: false,
      keyword: '',
      taskDate: [],//任务时间
      taskTypeId: '',//任务类型
      depid: [],//任务部门
      tasker: '',//任务人
      parentname: '',//父级任务名称
      depData:[],//部门数据
      selectedId:[],//多选删除id
      multipleSelection:[],//选中的数据id
      pickerOptions,
      parentid:0,
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      taskType:[
        {
          id:0,
          label:'日'
        },
        {
          id:1,
          label:'周'
        },
        {
          id:2,
          label:'月'
        },
      ],
      props: {
        // 自定义部门下拉数据的key值
        value: "id",
        label: "text",
        children:'children'
      },
      dataLists: [], // 列表数据
      children: [] // 子任务数据
    }
  },
  created() {
    this.getList()
    this.getDepartment()
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  methods: {
    getList() {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          keyword: this.keyword.trim(),
          // parentname: this.parentname.trim(),
          type: this.taskTypeId,
          parentid: this.parentid,
          tasker: this.tasker.trim(),
          deptid: this.depid&&this.depid.toString(),
          starttime: this.taskDate&&this.taskDate[0],
          endtime: this.taskDate&&this.taskDate[1],
        }
        getTaskLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.total
            this.dataLists = response.rows
          }
        }).catch(result=>{
          this.loading = false
          this.dataLists=[]
        })
        resolve()
      })
    },
    btn_search() {
      this.pageParams.pageindex = 1
      this.getList()
    },
    btn_import() {
      this.$layer.iframe({
        content: {
          content: importFile, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: 0 } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ["40%", "380px"],
        shadeClose: false,
        title: "导入任务信息"
      });
    },
    btn_importChildren(rowid) {
      this.$layer.iframe({
        content: {
          content: importFile, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: rowid } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ["40%", "380px"],
        shadeClose: false,
        title: "导入任务信息"
      });
    },
    btn_add() {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'parentId': 0 }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['70%', '80%'],
        title: '添加任务信息'
      })
    },
    btn_detail(rowid) {
      this.$layer.iframe({
        content: {
          content: detail, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': rowid }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['70%', '80%'],
        title: '任务信息详情'
      })
    },
    //管理
    btn_administration(administrationId) {
      this.$layer.iframe({
        content: {
          content: administration, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': administrationId }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['40%', '30%'],
        title: '管理任务'
      })
    },
    btn_addChildren(parentId) {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'parentId': parentId }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['70%', '80%'],
        title: '添加任务信息'
      })
    },
    btn_edit(editId,parentId) {
      this.$layer.iframe({
        content: {
          content: addOrEdit, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { 'rowid': editId, 'parentId': parseInt(parentId)}// props
        },
        area: ['70%', '80%'],
        title: '编辑任务信息'
      })
    },
    btn_delete(editId) {
      this.$confirm('您确定要将此任务信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTask(editId).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    //获取任务部门
    getDepartment(){
       getDepartmentTreeList().then(response=>{
        if(response.state.toLowerCase()=='success'){
          this.depData=response.data
        }
      })
    },
    changeSelects(val) {
      const ids = []
      this.multipleSelection = val
      for (let i=0; i<this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].F_ID)
      }
      this.selectedId = ids;
    },
    //批量删除
    btn_deletes(){
      if(this.selectedId.length==0){
        this.$message('请选择一个选项')
        return
      }else{
          this.$confirm('您确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteTask(this.selectedId).then(response => {
            if (response.state.toLowerCase() === 'success') {
              this.getList()
              this.$message.success('删除成功!')
            }
          })
        }).catch(() => {
          this.$message.info('已取消删除')
        })
      }
      
    },
    //进度条数据处理
     formatNum(val) {
      return val + "%";
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
