<!-- 系统配置 - 权限配置 -->
<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form ref="form" :model="queryParams" label-width="100px">
        <el-row>
          <el-col :span="4">
            <el-form-item :label="'部门编号'" prop="deptCode">
              <el-input v-model="queryParams.deptCode" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item :label="'部门名称'" prop="deptName">
              <el-input v-model="queryParams.deptName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left: -30px">
            <el-form-item :label="'状态'" prop="status">
              <el-select v-model="queryParams.status" clearable :placeholder="'请选择'">
                <el-option
                  v-for="item,index in statusList"
                  :key="index"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8" style="margin-left: -10px">
            <el-form-item :label="'创建日期'" prop="teamCode">
              <el-date-picker
                v-model="queryParams.creationTime"
                value-format="yyyy-MM-dd"
                type="daterange"
                :range-separator="'至'"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                @change="changeTime"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4" style="margin-left: 40px">
            <div class="searchbox">
              <el-button class="my_search" @click="research()"> {{ '搜索' }}</el-button>
              <el-button class="my_reset" @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button size="small" class="thebtn1" @click="addRules" v-has-buttons="['departmentRoleAdd']">{{'添加'}}</el-button>
          <el-button size="small" @click="handleExport" style="background-color: #FFAD41;border-color:#FFAD41 " class="thebtn1" v-has-buttons="['departmentRoleExport']"> {{ '导出' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          height="630px"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :span-method="objectSpanMethod"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column align="center" prop="deptCode" :label="'部门编号'" />
          <el-table-column align="center" prop="deptName" :label="'部门名称'" />
          <el-table-column align="center" prop="userCount" :label="'部门人数'" />
          <el-table-column align="center" prop="status" :label="'状态'" />
          <el-table-column align="center" prop="creationTime" :label="'创建日期'" />
          <el-table-column align="center" prop="" :label="'操作'" fixed="right" width="300">
            <template slot-scope="scope">
              <el-button
                class="button-s"
                type="text"
                size="small"
                style="color: #6962F6"
                @click="updateShow(scope.row, tableData)"
                v-has-buttons="['departmentRoleUpdate']"
              >
                {{ '修改' }}
              </el-button>
              <el-button v-has-buttons="['departmentRoleDisabled']" v-if="scope.row.status=='正常'" type="text" size="small" class="button-s" @click="stopRole(scope.row, tableData)"> {{'禁用'}} </el-button>
              <el-button v-has-buttons="['departmentRoleEnable']" v-if="scope.row.status=='停用'" type="text" size="small" class="button-s" @click="stopRole(scope.row, tableData)">{{'恢复'}} </el-button>
              <el-button v-has-buttons="['departmentRoleDelete']" type="text" size="small" class="button-s" style="color: #c73030" @click="delRole(scope.row, tableData)">
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <el-dialog
      :title="'添加'"
      :visible.sync="ruleShow"
      width="37%"
      center
      :close-on-click-modal="false"
      :before-close="handleClose"
    >
      <el-form ref="forms" :rules="rules" :model="table" label-width="80px">
        <el-form-item :label="'部门名称'" prop="deptName" style="margin-bottom: 10px">
          <el-input
            v-model="table.deptName"
            clearable
            :placeholder="'请输入'"
          />
        </el-form-item>
        <el-form-item :label="'状态'" prop="status">
          <el-checkbox-group v-model="table.status" @change="statusChange">
            <el-checkbox
              v-for="(item, index) in statusList"
              :key="index"
              :label="item.name"
              name="type"
            />
          </el-checkbox-group>
        </el-form-item>
        <div style="padding-top: 230px;display: flex;justify-content: center">
          <el-button type="text" size="small" class="button-style" @click="handleClose"> {{ '取消' }}</el-button>
          <el-button v-preventReClick="3000" type="text" size="small" class="button-style" style="margin-left: 40px;background-color: #48B2FD;" @click="submitBtn()">
            {{ '确认' }}
          </el-button>
        </div>
      </el-form>
    </el-dialog>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="research"
    />
  </div>
</template>

<script>
import topBar from '@/components/topBar'
import { getbdmenuList, getPubenumsList } from '@/api/system'
import { getgradeRanglist, getAwardsListChiose } from '@/api/level'
import * as api from '@/api/notice'
import { Message } from 'element-ui'
import { deleteAgreement } from '@/api/configManage'
export default {
  name: 'Qxpz',
  components: {
    topBar
  },
  data() {
    return {
      forms: '',
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '', trigger: 'blur' }
        ]
      },
      table: { 'deptName': '', 'status': ['正常'] },
      ruleShow: false,
      moren: 'departmentRole',
      topList: [
        {
          name: '部门配置',
          path: 'departmentRole'
        }
      ],
      defaultParams: {
        label: 'label',
        value: 'value',
        children: 'enumList'
      },
      multipleSelection: [],
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        deptCode: '', // 部门编号
        deptName: '', // 部门姓名
        status: '' // 状态
      },
      gradeRangList: [],
      awardsList: [],
      statusList: [{ 'name': '正常', 'id': '0' }, { 'name': '停用', 'id': '1' }],
      menuOptions: [],
      tableData: [],
      loading:false,
      total: 0,
      types: '',
      formId: '',
      creationTime: []
    }
  },
  created() {
    this.research()
    this.getgradeRanglist()
    this.getAwardsListChiose()
    this.getPubenumsList()
  },
  methods: {
    // 导出
    handleExport() {
      this.download(
        '/system/manage/dept/export',
        { ...this.params },
        `${'部门信息'}${new Date().getTime()}.xlsx`
      )
    },
    addRules() {
      this.types = 1
      this.ruleShow = true
    },
    submitBtn() {
      // 添加角色
      this.ruleSubmit()
    },
    updateShow(item) {
      this.types = 2
      // 更新部门信息
      this.formId = item.deptId
      api.deptDetails(item.deptId).then((res) => {
        if (res.code === 200) {
          this.ruleShow = true
          this.table = { 'deptName': res.data.deptName, 'status': res.data.status === 0 ? ['正常'] : ['停用'] }
        } else {
          Message({
            message: res.msg,
            type: 'error'
          })
        }
      })
    },
    delRole(item) {
      // 删除部门信息
      this.$confirm('确认要删除该角色吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.delDept(item.deptId).then((res) => {
          if (res.code === 200) {
            Message({
              message: '删除成功',
              type: 'success'
            })
            this.research()
          } else {
            Message({
              message: res.msg,
              type: 'error'
            })
          }
        })
      })
    },
    stopRole(item) {
      // 停用部门
      this.$confirm('确认要停用该部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.stopDept({ 'deptId': item.deptId, 'status': item.status === '正常' ? 1 : 0 }).then((res) => {
          if (res.code === 200) {
            Message({
              message: res.msg,
              type: 'success'
            })
            this.research()
          } else {
            Message({
              message: res.msg,
              type: 'error'
            })
          }
        })
      })
    },
    ruleSubmit() {
      const datas = this.table
      const that = this
      this.$refs['forms'].validate(valid => {
        if (!valid) {
          return false
        } else {
          if (this.types === 1) {
            if (datas.status[0] === '正常') {
              datas.status = ''
              datas.status = 0
            } else if (datas.status[0] === '停用') {
              datas.status = ''
              datas.status = 1
            }
            api.addDept(datas).then((res) => {
              if (res.code === 200) {
                Message({
                  message: '添加成功',
                  type: 'success'
                })
                that.ruleShow = false
                that.table = { 'deptName': '', 'status': ['正常'] }
                that.research()
              } else {
                Message({
                  message: res.msg,
                  type: 'error'
                })
              }
            })
          }
          if (this.types === 2) {
            if (datas.status[0] === '正常') {
              datas.status = ''
              datas.status = 0
            } else if (datas.status[0] === '停用') {
              datas.status = ''
              datas.status = 1
            }
            datas.deptId = this.formId
            api.putDept(datas).then((res) => {
              if (res.code === 200) {
                Message({
                  message: '修改成功',
                  type: 'success'
                })
                that.ruleShow = false
                that.table = { 'deptName': '', 'status': ['正常'] }
                that.research()
              } else {
                Message({
                  message: res.msg,
                  type: 'error'
                })
              }
            })
          }
        }
      })
    },
    statusChange(value) {
      if (this.table.status.length > 1) {
        this.table.status.splice(0, 1)
      }
    },
    handleClose() {
      this.ruleShow = false
      this.table = { 'deptName': '', 'status': ['正常'] }
    },
    changeBox(row) {
      console.error(row)
    },
    addFrom() {
    },
    addRule() {
    },
    changeTime(value) {
      // this.queryParams.creationTime = value[0]
      // this.queryParams.endCreationTime = value[1]
    },
    handleChangeMunu(value) {
      console.log('🌈', value[value.length - 1])
      this.queryParams.menuId = value[value.length - 1]
    },
    // 菜单下拉选
    getPubenumsList() {
      getPubenumsList().then((res) => {
        this.menuOptions = res.data
      })
    },
    // 等级范围选择
    getgradeRanglist() {
      getgradeRanglist().then((res) => {
        this.gradeRangList = res.data
      })
    },
    // 奖衔下拉列表
    getAwardsListChiose() {
      getAwardsListChiose().then((res) => {
        this.awardsList = res.rows
      })
    },
    research() {
      this.loading = true
      // // 日期传入改为起止两个字段
      // if ("" != this.queryParams.creationTime) {
      //   this.queryParams.creationTimeStart = this.queryParams.creationTime[0];
      //   this.queryParams.creationTimeEnd = this.queryParams.creationTime[1];
      // }

      // creationTime: '', // 开始时间
      //   modifiedTime: '' // 结束时间
      api.deptList(this.queryParams).then((res) => {
        res.data.forEach((element) => {
          element.status = element.status === 0 ? '正常' : '停用'
        })

        this.tableData = res.data
        this.total = res.total
        this.loading = false
      })
    },
    reChongzhi() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        deptCode: '', // 部门编号
        deptName: '', // 部门姓名
        status: '' // 状态
      }
      this.research()
    },

    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 0) {
      //   if (rowIndex % 2 === 0) {
      //     return {
      //       rowspan: 2,
      //       colspan: 1,
      //     };
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0,
      //     };
      //   }
      // }
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .el-input__inner{
  width: 100%;
}
::v-deep .el-dialog__title{
  font-weight: bold;
}
.button-style{
  width: 209px;
  height: 48px;
  background: #CCCCCC;
  border-radius: 5px 5px 5px 5px;
  opacity: 1;
  line-height: 48px;
  padding: 0!important;
  color: #fff;
  font-size: 18px;
}
::v-deep .el-dialog__header{
  border-bottom: 1px solid rgba(0,0,0,0.1);
}
.button-s{
  padding: 0 2px!important;
}
::v-deep .el-button{
  padding: 10px 20px;
}
::v-deep .el-date-editor .el-range__close-icon{
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range-separator{
  margin-top: -10px;
}
::v-deep .el-date-editor .el-range__icon{
  margin-top: -10px;
}
::v-deep .el-input__inner{
  height:32px;
  line-height: 32px;
}
::v-deep .el-form-item__label{
  color: #333;
  font-weight: inherit;
}
::v-deep .el-row {
  margin-top: 10px;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
.openClose {
  margin-top: 10px;
  margin-right: 10px;
  color: #666666;
  text-align: center;
}
.page {
  padding: 10px 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    background: #ffffff;
    border-radius: 8px;
    padding-top: 1px;
    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    .maintop {
      display: flex;
      padding: 10px 0;
      justify-content: space-between;
      align-items: center;
      .mainbtn {
        .thebtn1 {
          color: #ffffff;
          background: #3181E5;
          border-color: #3181E5;
        }
      }
    }
  }
}
</style>
