<template>
  <div class="app-container">
    <!-- 权限 -->
    <el-tabs tab-position="left" style="height: 100%;" v-model="tabsIndex">
      <el-button style="float:right" @click="openDialog(null)">添加人员</el-button>
      <el-tab-pane label="普通员工" name="0">
        <!-- 列表 -->
        <el-table :data="crew1" v-loading="loading" width="100%">
          <el-table-column type="index" width="40"></el-table-column>
          <el-table-column label="头像" width="110">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="'http://localhost:9102/img' + scope.row.headUrl"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名"></el-table-column>
          <el-table-column prop="phone" label="手机号" width="110"></el-table-column>
          <el-table-column prop="deptId" label="部门" width="80"></el-table-column>
          <el-table-column prop="roleName" label="角色" width="80"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="openDialog(scope.row.id)">转移</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码区域 -->
        <el-pagination
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"
          :current-page="pageIndex1"
          :page-sizes="[20, 50, 100]"
          :page-size="20"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total1"
          background
          style="float:right"
        ></el-pagination>
      </el-tab-pane>
      <el-tab-pane label="领导" name="1">
        <!-- 列表 -->
        <el-table :data="crew2" v-loading="loading" width="100%">
          <el-table-column type="index" width="40"></el-table-column>
          <el-table-column label="头像" width="110">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="'http://localhost:9102/img' + scope.row.headUrl"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名"></el-table-column>
          <el-table-column prop="phone" label="手机号" width="110"></el-table-column>
          <el-table-column prop="deptId" label="部门" width="80"></el-table-column>
          <el-table-column prop="roleName" label="角色" width="80"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="openDialog(scope.row.id)">转移</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码区域 -->
        <el-pagination
          @size-change="handleSizeChange2"
          @current-change="handleCurrentChange2"
          :current-page="pageIndex2"
          :page-sizes="[20, 50, 100]"
          :page-size="20"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total2"
          background
          style="float:right"
        ></el-pagination>
      </el-tab-pane>
      <el-tab-pane label="操作员" name="2">
        <!-- 列表 -->
        <el-table :data="crew3" v-loading="loading" width="100%">
          <el-table-column type="index" width="40"></el-table-column>
          <el-table-column label="头像" width="110">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="'http://localhost:9102/img' + scope.row.headUrl"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="姓名"></el-table-column>
          <el-table-column prop="phone" label="手机号" width="110"></el-table-column>
          <el-table-column prop="deptId" label="部门" width="80"></el-table-column>
          <el-table-column prop="roleName" label="角色" width="80"></el-table-column>
          <el-table-column label="操作" width="100">
            <template slot-scope="scope">
              <el-button @click="openDialog(scope.row.id)">转移</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 页码区域 -->
        <el-pagination
          @size-change="handleSizeChange3"
          @current-change="handleCurrentChange3"
          :current-page="pageIndex3"
          :page-sizes="[20, 50, 100]"
          :page-size="20"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total3"
          background
          style="float:right"
        ></el-pagination>
      </el-tab-pane>
    </el-tabs>

    <!-- 转移的dialog -->
    <el-dialog title="权限设置" :visible.sync="dialogVisible" width="600px">
      <el-form ref="form1" :model="switchForm" :rules="rules" label-width="110px">
        <el-form-item v-if="switchForm.single == false" label="选择人员" prop="ids">
          <el-select
            v-model="switchForm.ids"
            multiple
            filterable
            placeholder="请选择人员"
            style="width: 100%"
          >
            <el-option v-for="item in user" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="switchForm.roleId" placeholder="请选择角色" style="width: 100%">
            <el-option label="普通员工" value="1"></el-option>
            <el-option label="领导" value="2"></el-option>
            <el-option label="操作员" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="switchRole">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import request from "@/utils/request";
export default {
  data() {
    return {
      crew1: [],
      crew2: [],
      crew3: [],
      user: [],
      switchForm: {
        ids: [],
        roleId: 1,
        single: false
      },
      dialogVisible: false,
      loading: false,
      tabsIndex: 0,
      //分页相关
      pageIndex1: 1,
      pageSize1: 20,
      total1: 0,
      pageIndex2: 1,
      pageSize2: 20,
      total2: 0,
      pageIndex3: 1,
      pageSize3: 20,
      total3: 0,
      rules: {
        ids: [{ required: true, message: "请选择人员", trigger: "blur" }],
        roleId: [{ required: true, message: "请选择角色", trigger: "blur" }]
      }
    };
  },
  methods: {
    //获取列表
    getCrew(roleId) {
      this.loading = true;
      request({
        url: "/permission/getCrewByPage",
        method: "post",
        params: {
          pageIndex: this.pageIndex1,
          pageSize: this.pageSize1,
          type: roleId
        }
      })
        .then(response => {
          switch (roleId) {
            case 1:
              this.crew1 = response.data.user;
              this.total1 = response.data.page.total;
              break;
            case 2:
              this.crew2 = response.data.user;
              this.total2 = response.data.page.total;
              break;
            case 3:
              this.crew3 = response.data.user;
              this.total3 = response.data.page.total;
              break;
          }
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //变更角色
    switchRole() {
      this.$refs.form1.validate(valid => {
        if (valid) {
          this.loading = true;
          var form = new FormData();
          form.append("roleId", this.switchForm.roleId);
          this.switchForm.ids.forEach(id => {
            form.append("ids", id);
          });
          request({
            url: "/permission/switchRole",
            method: "post",
            data: form
          })
            .then(response => {
              this.loading = false;
              this.refresh();
              this.dialogVisible = false;
            })
            .catch(error => {
              this.loading = false;
              this.dialogVisible = false;
            });
        }
      });
    },

    //打开dailog
    openDialog(id) {
      this.getUser();
      this.switchForm.ids = id == null ? [] : [id];
      this.switchForm.single = id != null;
      this.switchForm.roleId = null;
      this.dialogVisible = true;
    },

    //获取下拉列表的全部人员
    getUser(user) {
      this.loading = true;
      request({
        url: "/permission/getSimpleUserList",
        method: "post"
      })
        .then(response => {
          this.user = response.data;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //页码规格变更1
    handleSizeChange1(val) {
      this.pageSize1 = val;
      this.getCrew(1);
    },

    //页码页数变更1
    handleCurrentChange1(val) {
      this.pageIndex1 = val;
      this.getCrew(1);
    },

    //页码规格变更2
    handleSizeChange2(val) {
      this.pageSize2 = val;
      this.getCrew(2);
    },

    //页码页数变更2
    handleCurrentChange2(val) {
      this.pageIndex2 = val;
      this.getCrew(2);
    },

    //页码规格变更3
    handleSizeChange3(val) {
      this.pageSize3 = val;
      this.getCrew(3);
    },

    //页码页数变更3
    handleCurrentChange3(val) {
      this.pageIndex3 = val;
      this.getCrew(3);
    },

    //全员恶人
    refresh() {
      this.getCrew(1);
      this.getCrew(2);
      this.getCrew(3);
    }
  },
  mounted() {
    this.refresh();
  }
};
</script>

<style scoped>
</style>

