<template>
  <div class="mod-role">
    <el-container>
      <!-- 树状图 -->
      <el-aside>
        <div class="rect">
          <user-left ref="left" @getUsers="getUsers"></user-left>
        </div>
      </el-aside>
      <el-container>
        <!-- form表单 -->
        <el-header>
          <div class="rect rect-form">
            <el-form :inline="true" :model="form" size="mini" ref="form">
              <el-form-item>
                <el-input v-model="form.username" placeholder="姓名/手机号码" clearable></el-input>
              </el-form-item>
              <el-form-item style="width: 140px">
                <el-select placeholder="部门" v-model="form.departmentCode">
                  <el-option
                    v-for="(item, index) in departments"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item style="width: 140px">
                <el-select placeholder="岗位" v-model="form.jobCode">
                  <el-option
                    v-for="(item, index) in jobs"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item style="width: 140px">
                <el-select placeholder="性别" v-model="form.gender">
                  <el-option
                    v-for="item in gender"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button @click="getDataList()" icon="el-icon-search">查 询</el-button>
              </el-form-item>
              <el-form-item>
                <el-button 
                  icon="el-icon-circle-close"
                  @click="clearAll">清 空</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-header>
        <!-- role表格 -->
        <el-main>
          <div class="rect rect-table">
            <!-- 新增 删除按钮 -->
            <el-form inline size="mini">
              <el-form-item>
                <el-button 
                  icon="el-icon-plus" 
                  v-if="isAuth('sys:user:save')" 
                  @click="addOrUpdateHandle()">
                  新 增
                </el-button>
              </el-form-item>
              <el-form-item>
                <el-button 
                  icon="el-icon-delete" 
                  v-if="isAuth('sys:user:delete')" 
                  type="danger" 
                  @click="deleteHandle()" 
                  :disabled="dataListSelections.length <= 0">
                  删 除
                </el-button>
              </el-form-item>
            </el-form>
            
            <el-table
              :data="dataList"
              border
              stripe
              v-loading="dataListLoading"
              @selection-change="selectionChangeHandle"
              style="width: 100%;"
              size="mini"
              height="540"
              :header-cell-style="{background:'rgb(245,245,245)',color:'#000',height: '45px',fontSize: '13px',fontWeight: 'normal',borderBottom: '1px solid #ccc'}" 
              :cell-style="{color: '#888',fontSize: '13px'}">
              <el-table-column
                type="selection"
                header-align="center"
                align="center"
                width="50">
              </el-table-column>
              <el-table-column
                prop="username"
                header-align="center"
                align="center"
                width="100"
                label="姓名">
              </el-table-column>
              <el-table-column
                header-align="center"
                align="center"
                width="60"
                label="性别">
                <template slot-scope="scope">
                  <span>{{scope.row.sex===1? '男': '女'}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="birthday"
                header-align="center"
                align="center"
                width="100"
                label="出生日期">
              </el-table-column>
              <el-table-column
                prop="mobile"
                header-align="center"
                align="center"
                width="130"
                label="手机">
              </el-table-column>
              <el-table-column
                prop="email"
                header-align="center"
                align="center"
                width="200"
                label="邮箱">
              </el-table-column>
              <el-table-column
                prop="jobName"
                header-align="center"
                align="center"
                width="140"
                label="岗位">
              </el-table-column>
              <el-table-column
                prop="departmentName"
                header-align="center"
                align="center"
                width="140"
                label="部门">
              </el-table-column>
              <el-table-column
                prop="farmName"
                header-align="center"
                align="center"
                width="140"
                label="牧场">
              </el-table-column>
              <el-table-column
                prop="address"
                header-align="center"
                align="center"
                width="200"
                label="住址">
              </el-table-column>
              <el-table-column
                prop="remarks"
                header-align="center"
                align="center"
                width="180"
                label="备注">
              </el-table-column>
              <el-table-column
                header-align="center"
                align="center"
                width="150"
                label="操作">
                <template slot-scope="scope">
                  <el-button v-if="isAuth('sys:user:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.userId)">修改</el-button>
                  <el-button v-if="isAuth('sys:user:delete')" type="text" size="small" @click="deleteHandle(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
              @size-change="sizeChangeHandle"
              @current-change="currentChangeHandle"
              :current-page="pageIndex"
              :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize"
              :total="totalPage"
              layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>
            <!-- 弹窗, 新增 / 修改 -->
            <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
          </div>
        </el-main>
      </el-container>
      
      
    </el-container>
  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  import userLeft from './user-left.vue'
  export default {
    data () {
      return {
        form: {
          username: undefined,
          departmentCode: undefined,
          jobCode: undefined,
          gender: undefined
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        farmId: undefined,
        gender: [
          {
            value: 1,
            label: '男'
          },
          {
            value: 2,
            label: '女'
          }
        ],
        departments: [], // 部门select
        jobs: [], // 岗位select
      }
    },
    components: {
      AddOrUpdate,
      userLeft
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // form表单清空
      clearAll () {
        this.form.username = undefined
        this.form.departmentCode = undefined
        this.form.jobCode = undefined
        this.form.gender = undefined
        // this.$refs['left'].clearTree()
        // this.farmId = undefined
      },
      // 左侧树状图对users的限制
      getUsers (val) {
        this.farmId = undefined
        if (val.order === 1) {
          this.farmId = val.id
        }
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/user/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'username': this.form.username,
            'departmentCode': this.form.departmentCode,
            'jobCode': this.form.jobCode,
            'sex': this.form.gender,
            'farmId': this.farmId
          }, false)
        }).then(async({data}) => {
          console.log(data);
          // 部门select
          this.departments = []
          let departments = []
          let departmentR = await this.$http({
            url: this.$http.adornUrl("/sys/department/findAll"),
            method: "get"
          })
          departmentR.data.departmentList && (departments = departmentR.data.departmentList)
          departments.forEach(department => {
            let item = {
              value: department.departmentCode,
              label: department.departmentName
            }
            this.departments.push(item)
          })
          // 岗位select
          this.jobs = []
          let jobs = []
          let jobR = await this.$http({
            url: this.$http.adornUrl("/sys/job/findAll"),
            method: "get"
          })
          jobR.data.jobList && (jobs = jobR.data.jobList)
          jobs.forEach(job => {
            let item = {
              value: job.jobCode,
              label: job.jobName
            }
            this.jobs.push(item)
          })
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
            this.$message.error(data.msg)
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (user) {
        let ids = user ? [user.userId] : this.dataListSelections.map(item => item.userId)
        let names = user ? [user.username] : this.dataListSelections.map(item => item.username)
        this.$confirm(`确定${user ? '删除' : '批量删除'}姓名${names.join(',')}的用户?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/user/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.getDataList()
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                // onClose: () => {
                  
                // }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
<style scoped>
.rect {
  background-color: #fff;
  padding: 30px 15px;
  border-radius: 5px;
  border: 1px solid #e8e8e8;
}
.rect-form {
  padding-bottom: 10px;
}
.rect-table {
  margin-top: 20px;
}
.demo-table-expand {
  font-size: 0;
  margin-left: 40px;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 80%;
}
.el-table .height {
  background: rgba(254, 254, 254, 0.5);
}
/deep/ .el-table--mini td, .el-table--mini th {
  padding: 3px 0 !important;
  height: 20px !important;
}
/deep/ .el-checkbox__input.is-checked+.el-checkbox__label {
  color: rgb(24,144,255);
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__inner:hover {
  border-color: rgb(24,144,255);
}
/deep/.el-table .el-checkbox__input.is-focus .el-checkbox__inner {
  border-color: rgb(24,144,255);
}
/deep/.el-table #select .cell .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
  background-color: rgb(24,144,255);
  border-color: rgb(24,144,255);
}
</style>
