<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="8">
        <div class="filter-container">
          <el-button type="primary" class="filter-item" icon="el-icon-plus" @click="btn_add()">添加</el-button>
          <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="btn_edit()">编辑</el-button>
          <el-button
            type="primary"
            class="filter-item"
            icon="el-icon-delete"
            @click="btn_delete()"
          >删除</el-button>
        </div>
        <el-alert :closable="false" title="部门分类" type="success" />
        <el-table
          v-loading="loading"
          :data="departmentDataLists"
          highlight-current-row
          @current-change="handleCurrentChange"
          border
          row-key="F_DeptId"
          default-expand-all
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @selection-change="changeSelects"
        >
          <el-table-column prop="F_DeptName" label="部门名称" align="center" min-width />
          <!-- <el-table-column prop="F_Lock" label="锁定" align="center" min-width="">
            <template slot-scope="scope">
              {{ scope.row.F_Lock | judgmentLockName }}
            </template>
          </el-table-column>-->
          <el-table-column prop="F_TargetMoney" label="任务额" align="center" min-width>
            <template slot-scope="scope">{{ scope.row.F_TargetMoney | judgmentTargetMoney }}</template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="16">
        <div>
          <div class="filter-container search">
            <el-input v-model="keyword" 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_addR"
            >添加</el-button>
          </div>
          <el-table v-loading="loading" :data="dataLists" border stripe>
            <el-table-column type="index" label="编号" align="center" fixed width="80" />
            <el-table-column prop="F_DeptName" label="部门名称" align="center" min-width />
            <el-table-column prop="F_Name" label="团队名称" align="center" min-width />
            <el-table-column prop="F_CreateBy" label="创建人" align="center" min-width />
            <el-table-column prop="F_CreateOn" label="创建时间" align="center" min-width />
            <el-table-column prop="F_TargetMoney" label="任务额" align="center" min-width>
              <template slot-scope="scope">{{ scope.row.F_TargetMoney | judgmentTargetMoney }}</template>
            </el-table-column>
            <el-table-column prop="F_Des" label="描述说明" align="center" min-width />
            <el-table-column
              label="操作"
              width="160"
              align="center"
              class-name="oparate_btn"
              fixed="right"
            >
              <template slot-scope="scope">
                <el-button
                  v-permission="'HY_edit'"
                  size="mini"
                  plain
                  type="primary"
                  @click="btn_editR(scope.row.F_Id.toString())"
                >编辑</el-button>
                <el-button
                  v-permission="'HY_delete'"
                  size="mini"
                  plain
                  type="danger"
                  @click="btn_deleteR(scope.row.F_Id.toString())"
                >删除</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>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getScoreList, deleteType, deleteScore, getDepartmentList } from '@/api/systemSetup/roleSetting/department'
import { getTeamLists, deleteTeam, getDepidTeam } from '@/api/systemSetup/sysSetting/teamManagement'
import addOrEditKnowledge from './component/addOrEdit'
import addOrEditTeam from './component/addOrEditTeam'
import Pagination from '@/components/Pagination' // 对el-pagination 二次封装
export default {
  components: {
    Pagination
  },
  filters: {
    judgmentLockName (status) {
      const statusMap = {
        '0': '未锁定',
        '1': '锁定',
      }
      return statusMap[status]
    },
    judgmentTargetMoney (status) {
      if (status == 0) {
        return ''
      } else {
        return status
      }
    }
  },
  data () {
    return {
      keyword: '',
      selectedId: [],
      depId: 0,//最后一级部门id
      depName: '',//最后一级部门名称
      loading: false,
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0 // 总共多少数据
      },
      dataLists: [], // 团队管理列表数据
      departmentDataLists: [], //部门列表数据
    }
  },
  created () {
    this.getDepList() //部门列表
    this.getList()// 团队管理数据
    document.onkeyup = (e) => {
      if (e.keyCode === 13) {
        this.getList()
      }
    }
  },
  mounted: function () {
  },
  methods: {
    // 部门分类
    getDepList () {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          keyword: this.keyword.trim(),
        }
        getDepartmentList(params).then(response => {
          this.loading = false
          this.departmentDataLists = response
        }).catch(result => {
          this.loading = false
          this.departmentDataLists = []
        })
        resolve()
      })
    },
    //添加
    btn_add () {
      this.$layer.iframe({
        content: {
          content: addOrEditKnowledge, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: '' } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['80%', '90%'],
        shadeClose: false,
        title: '添加部门分类'
      })
    },
    //编辑
    btn_edit () {
      if (this.selectedId.length !== 1) {
        this.$message({
          message: '请选择一行编辑！',
          type: 'warning'
        })
        return
      }
      let rowId = this.selectedId[0]
      this.$layer.iframe({
        content: {
          content: addOrEditKnowledge, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: { rowid: rowId } // props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
        },
        area: ['80%', '90%'],
        shadeClose: false,
        title: '编辑部门分类'
      })
    },
    //删除
    btn_delete () {
      if (this.selectedId.length === 0) {
        this.$message({
          message: '没有可删除的选项！',
          type: 'warning'
        })
      } else {
        let ids = this.selectedId.join(',')
        this.deleteType(this.selectedId)
      }
    },
    //删除
    deleteType (ids) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteType(ids).then(response => {
            if (response.state.toLowerCase() === 'success') {
              this.getDepList()
              this.$message.success('删除成功!')
            }
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    changeSelects (val) {
      const ids = []
      this.multipleSelection = val
      for (let i = 0; i < this.multipleSelection.length; i++) {
        ids.push(this.multipleSelection[i].F_DeptId)
      }
      this.selectedId = ids;
    },
    handleCurrentChange (data) {
      if (data.F_DeptId !== 0) {
        this.depId = data.F_DeptId
        this.depName = data.F_DeptName
        this.getList()
      } else {
        this.depId = 0
      }
    },

    //获取团队列表数据
    getList () {
      this.loading = true
      return new Promise(resolve => {
        const params = {
          pageindex: this.pageParams.pageindex, // 第几页
          pagesize: this.pageParams.pagesize, // 每页几条信息
          key: this.keyword.trim(),
          deptid: this.depId
        }
        getTeamLists(params).then(response => {
          this.loading = false
          if (response.state.toLowerCase() === 'success') {
            this.pageParams.total = response.data.Totals
            this.dataLists = response.data.Rows
          }
        })
        resolve()
      })
    },
    //搜索
    btn_search () {
      this.pageParams.pageindex = 1
      this.getList()
    },
    //右边添加功能
    btn_addR () {
      if (this.depId !== 0) {
        this.$layer.iframe({
          content: {
            content: addOrEditTeam, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: { 'depId': this.depId, 'depName': this.depName }// props//该方法会自动添加一个key为layerid的值， 该值为创建层的id， 可以直接使用
          },
          area: ['80%', '80%'],
          title: '添加团队管理信息'
        })
      } else {
        this.$message.info('请选择部门')
      }
    },
    //右边编辑功能
    btn_editR (editId) {
      if (this.depId !== 0) {
        this.$layer.iframe({
          content: {
            content: addOrEditTeam, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: { 'rowid': editId, 'depId': this.depId, 'depName': this.depName }// props
          },
          area: ['80%', '80%'],
          title: '编辑团队管理信息'
        })
      } else {
        this.$message.info('请选择部门')
      }
    },
    //右边删除功能
    btn_deleteR (editId) {
      this.$confirm('您确定要将此团队信息删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTeam(editId).then(response => {
          if (response.state.toLowerCase() === 'success') {
            this.getList()
            this.$message.success('删除成功!')
            this.depId = 0
          }
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.search {
  text-align: right !important;
}
</style>
