<template>
  <div>
    <el-tabs :tab-position="tabPosition1" style="height: 80%">
      <el-tab-pane label="销售管理" v-if="role == 1 || role == 4">销售管理</el-tab-pane>
      <el-tab-pane label="人员管理" v-if="role == 1 || role == 3">
        <el-tabs :tab-position="tabPosition2" style="height: 80%">
          <el-tab-pane label="管理员管理" v-if="post == 1 || post == 2">
            <div>
              <el-button @click="fetchManager">刷新</el-button>
            </div>
            <el-form :inline="true" :model="empRequest" class="demo-form-inline">
              <el-form-item label="管理员姓名">
                <el-input v-model="empRequest.empName" placeholder="管理员姓名" clearable></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="fetchManager" icon="el-icon-search">查询</el-button>
              </el-form-item>
            </el-form>

            <el-button type="primary" @click="dialogFormVisible1 = true">新增管理员</el-button>

            <el-table :data="empResponse.list" border stripe @selection-change="handleSelectionChange"
                      tooltip-effect="dark" style="width: 100%"
            >
              <el-table-column type="selection" width="55"/>
              <el-table-column label="员工Id" prop="empId"/>
              <el-table-column label="员工姓名" prop="empName"/>
              <el-table-column label="员工用户名" prop="username"/>
              <el-table-column label="员工手机号" prop="empTel"/>
              <el-table-column label="员工身份证号" prop="empIdCard"/>
              <el-table-column label="员工年龄" prop="empAge"/>
              <el-table-column label="员工性别">
                <template slot-scope="scope">
                  <span v-if="scope.row.empGender == 1">男</span>
                  <span v-if="scope.row.empGender == 2">女</span>
                </template>
              </el-table-column>
              <el-table-column label="员工住址" prop="empAddress"/>
              <el-table-column label="员工薪资" prop="empSal"/>
              <el-table-column label="员工权限" prop="roleName"/>
              <el-table-column label="员工岗位" prop="postName"/>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="warning" @click="openManager(scope.row.empId)">编辑</el-button>
                  <el-button type="danger" @click="delManager(scope.row.empId)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="empRequest.pageNum"
              :page-sizes="[1, 5, 10, 20]"
              :page-size="empRequest.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="empResponse.total"
            >
            </el-pagination>

            <el-button type="danger" @click="batchDelManager">删除管理员</el-button>

            <el-dialog title="新增管理员" :visible.sync="dialogFormVisible1">
              <el-form :model="emp" :rules="rules" ref="emp">
                <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="empName">
                  <el-input v-model="emp.empName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工用户名" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="emp.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工密码" :label-width="formLabelWidth" prop="password">
                  <el-input v-model="emp.password" autocomplete="off" :key="passwordType" :type="passwordType" tabindex="2" auto-complete="on"></el-input>
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
                  </span>
                </el-form-item>
                <el-form-item label="员工手机号" :label-width="formLabelWidth" prop="empTel">
                  <el-input v-model="emp.empTel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工身份证号" :label-width="formLabelWidth" prop="empIdCard">
                  <el-input v-model="emp.empIdCard" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工年龄" :label-width="formLabelWidth" prop="empAge">
                  <el-input v-model="emp.empAge" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工性别" :label-width="formLabelWidth" prop="empGender">
                  <el-radio-group v-model="emp.empGender">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="2">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="员工住址" :label-width="formLabelWidth" prop="empAddress">
                  <el-input v-model="emp.empAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工薪资" :label-width="formLabelWidth" prop="empSal">
                  <el-input v-model="emp.empSal" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工角色" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.roleId" clearable placeholder="请选择">
                    <el-option v-for="item in roles" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="员工岗位" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.postId" clearable placeholder="请选择">
                    <el-option v-for="item in posts" :key="item.postId" :label="item.postName" :value="item.postId"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="addManager">确 定</el-button>
              </div>
            </el-dialog>

            <el-dialog title="编辑管理员" :visible.sync="dialogFormVisible2">
              <el-form :model="emp" :rules="rules" ref="emp">
                <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="empName">
                  <el-input v-model="emp.empName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工用户名" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="emp.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工密码" :label-width="formLabelWidth" prop="password">
                  <el-input v-model="emp.password" autocomplete="off" :key="passwordType" :type="passwordType" tabindex="2" auto-complete="on"></el-input>
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
                  </span>
                </el-form-item>
                <el-form-item label="员工手机号" :label-width="formLabelWidth" prop="empTel">
                  <el-input v-model="emp.empTel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工身份证号" :label-width="formLabelWidth" prop="empIdCard">
                  <el-input v-model="emp.empIdCard" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工年龄" :label-width="formLabelWidth" prop="empAge">
                  <el-input v-model="emp.empAge" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工性别" :label-width="formLabelWidth" prop="empGender">
                  <el-radio-group v-model="emp.empGender">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="2">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="员工住址" :label-width="formLabelWidth" prop="empAddress">
                  <el-input v-model="emp.empAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工薪资" :label-width="formLabelWidth" prop="empSal">
                  <el-input v-model="emp.empSal" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工角色" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.roleId" clearable placeholder="请选择">
                    <el-option v-for="item in roles" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="员工岗位" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.postId" clearable placeholder="请选择">
                    <el-option v-for="item in posts" :key="item.postId" :label="item.postName" :value="item.postId"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible2 = false">取 消</el-button>
                <el-button type="primary" @click="updateManager">确 定</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="员工管理" v-if="post == 1 || post == 2">
            <div>
              <el-button @click="fetchEmp">刷新</el-button>
            </div>
            <el-form :inline="true" :model="empRequest" class="demo-form-inline">
              <el-form-item label="员工编号">
                <el-input v-model="empRequest.empId" placeholder="员工编号" clearable></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="fetchEmp" icon="el-icon-search">查询</el-button>
              </el-form-item>
            </el-form>

            <el-button type="primary" @click="dialogFormVisible3 = true">新增员工</el-button>

            <el-table :data="empResponse.list" border stripe @selection-change="handleSelectionChange"  tooltip-effect="dark" style="width: 100%">
              <el-table-column type="selection" width="55"/>
              <el-table-column label="员工Id" prop="empId"/>
              <el-table-column label="员工姓名" prop="empName"/>
              <el-table-column label="员工用户名" prop="username"/>
              <el-table-column label="员工手机号" prop="empTel"/>
              <el-table-column label="员工身份证号" prop="empIdCard"/>
              <el-table-column label="员工年龄" prop="empAge"/>
              <el-table-column label="员工性别" >
                <template slot-scope="scope">
                  <span v-if="scope.row.empGender == 1">男</span>
                  <span v-if="scope.row.empGender == 2">女</span>
                </template>
              </el-table-column>
              <el-table-column label="员工住址" prop="empAddress"/>
              <el-table-column label="员工薪资" prop="empSal"/>
              <el-table-column label="员工权限" prop="roleName"/>
              <el-table-column label="员工岗位" prop="postName"/>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button type="warning" @click="openEmp(scope.row.empId)">编辑</el-button>
                  <el-button type="danger" @click="delEmp(scope.row.empId)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="empRequest.pageNum"
              :page-sizes="[1, 5, 10, 20]"
              :page-size="empRequest.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="empResponse.total">
            </el-pagination>

            <el-button type="danger" @click="batchDelEmp">删除员工</el-button>

            <el-dialog title="新增管理员" :visible.sync="dialogFormVisible3">
              <el-form :model="emp" :rules="rules" ref="emp">
                <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="empName">
                  <el-input v-model="emp.empName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工用户名" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="emp.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工密码" :label-width="formLabelWidth" prop="password">
                  <el-input v-model="emp.password" autocomplete="off" :key="passwordType" :type="passwordType" tabindex="2" auto-complete="on"></el-input>
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
                  </span>
                </el-form-item>
                <el-form-item label="员工手机号" :label-width="formLabelWidth" prop="empTel">
                  <el-input v-model="emp.empTel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工身份证号" :label-width="formLabelWidth" prop="empIdCard">
                  <el-input v-model="emp.empIdCard" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工年龄" :label-width="formLabelWidth" prop="empAge">
                  <el-input v-model="emp.empAge" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工性别" :label-width="formLabelWidth" prop="empGender">
                  <el-radio-group v-model="emp.empGender">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="2">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="员工住址" :label-width="formLabelWidth" prop="empAddress">
                  <el-input v-model="emp.empAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工薪资" :label-width="formLabelWidth" prop="empSal">
                  <el-input v-model="emp.empSal" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工角色" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.roleId" clearable placeholder="请选择">
                    <el-option v-for="item in roles" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="员工岗位" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.postId" clearable placeholder="请选择">
                    <el-option v-for="item in posts" :key="item.postId" :label="item.postName" :value="item.postId"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible3 = false">取 消</el-button>
                <el-button type="primary" @click="addEmp">确 定</el-button>
              </div>
            </el-dialog>

            <el-dialog title="编辑管理员" :visible.sync="dialogFormVisible4">
              <el-form :model="emp" :rules="rules" ref="emp">
                <el-form-item label="员工姓名" :label-width="formLabelWidth" prop="empName">
                  <el-input v-model="emp.empName" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工用户名" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="emp.username" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工密码" :label-width="formLabelWidth" prop="password">
                  <el-input v-model="emp.password" autocomplete="off" :key="passwordType" :type="passwordType" tabindex="2" auto-complete="on"></el-input>
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
                  </span>
                </el-form-item>
                <el-form-item label="员工手机号" :label-width="formLabelWidth" prop="empTel">
                  <el-input v-model="emp.empTel" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工身份证号" :label-width="formLabelWidth" prop="empIdCard">
                  <el-input v-model="emp.empIdCard" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工年龄" :label-width="formLabelWidth" prop="empAge">
                  <el-input v-model="emp.empAge" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工性别" :label-width="formLabelWidth" prop="empGender">
                  <el-radio-group v-model="emp.empGender">
                    <el-radio :label="1">男</el-radio>
                    <el-radio :label="2">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="员工住址" :label-width="formLabelWidth" prop="empAddress">
                  <el-input v-model="emp.empAddress" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工薪资" :label-width="formLabelWidth" prop="empSal">
                  <el-input v-model="emp.empSal" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="员工角色" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.roleId" clearable placeholder="请选择">
                    <el-option v-for="item in roles" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="员工岗位" :label-width="formLabelWidth" prop="empSal">
                  <el-select v-model="emp.postId" clearable placeholder="请选择">
                    <el-option v-for="item in posts" :key="item.postId" :label="item.postName" :value="item.postId"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4 = false">取 消</el-button>
                <el-button type="primary" @click="updateEmp">确 定</el-button>
              </div>
            </el-dialog>
          </el-tab-pane>
          <el-tab-pane label="会员管理" v-if="post == 1 || post == 3"></el-tab-pane>
          <el-tab-pane label="客户管理" v-if="post == 1 || post == 3"></el-tab-pane>
        </el-tabs>
      </el-tab-pane>
      <el-tab-pane label="仓库管理" v-if="role == 1 || role == 2">

      </el-tab-pane>
      <el-tab-pane label="综合统计" v-if="role == 1 || role == 2 || role == 3">综合统计</el-tab-pane>
      <el-tab-pane label="报表统计" v-if="role == 1 || role == 2 || role == 3">报表统计</el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）,
//例如：import { 组件名称 } from '组件路径',

import { add, batchDel, del, fetchManager, findById, update, fetchEmp } from '@/api/manager'
import { findRoles, findPosts } from '@/api/role'

export default {
  //import引入的组件需要注入到对象中才能使用
  data() {
    //这里存放数据
    return {
      tabPosition1: 'left',
      tabPosition2: 'left',
      role: localStorage.getItem('roleId'),
      post: localStorage.getItem('postId'),
      emp: {
        empId: null,
        empName: '',
        username: '',
        password: '',
        empTel: '',
        empIdCard: '',
        empAge: null,
        empGender: null,
        empAddress: '',
        empSal: null,
        roleId: null,
        roleName: '',
        postId: null,
        postName: ''
      },
      empRequest: {
        empId: null,
        empName: '',
        pageNum: 1,
        pageSize: 5
      },
      empResponse: {
        total: 0,
        list: []
      },
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      formLabelWidth: '120px',
      passwordType: 'password',
      redirect: undefined,
      multipleSelection: [],
      rules: {
        empName: [
          { required: true, message: '请输入员工姓名', trigger: 'blur' },
          { pattern: /^[\u4e00-\u9fa5a-zA-Z0-9]+$/, message: '姓名只能包含中文、英文和数字', trigger: 'blur' }
        ],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        empTel: [
          { required: true, message: '请输入联系方式', trigger: 'blur' },
          { pattern: /^(\d{11})$/, message: '手机号格式错误', trigger: 'blur' }
        ],
        empIdCard: [
          { required: true, message: '请输入身份证号', trigger: 'blur' },
          { min: 18, max: 18, message: '身份证号长度应为18位', trigger: 'blur' },
          { pattern: /^[1-9][0-9]{16}[\dXx]$/, message: '身份证号格式不正确', trigger: 'blur' }],
        empAge: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
          { pattern: /^(\d+|\d+\.\d+)$/, message: '只能输入数字', trigger: 'blur' }
        ],
        empGender: [{ required: true, message: '请输入性别', trigger: 'blur' }],
        empAddress: [{ required: true, message: '请输入住址', trigger: 'blur' }],
        empSal: [
          { required: true, message: '请输入薪资', trigger: 'blur' },
          { pattern: /^(\d+|\d+\.\d+)$/, message: '只能输入数字', trigger: 'blur' }
        ]
      },
      roles: [],
      posts: []
    }
  },
  methods: {
    fetchManager() {
      fetchManager(this.empRequest).then(res => {
        this.empResponse.list = []
        this.empResponse = res.data
      })
    },
    handleSizeChange(val) {
      this.empRequest.pageSize = val
      this.fetchManager()
    },
    handleCurrentChange(val) {
      this.empRequest.pageNum = val
      this.fetchManager()
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    addManager() {
      add(this.emp).then(res => {
        this.$message.success(res.msg)
        this.dialogFormVisible1 = false
        this.emp = {}
        this.fetchManager()
      })
    },
    openManager(empId) {
      this.dialogFormVisible2 = true
      findById(empId).then(res => {
        this.emp = res.data
      })
    },
    updateManager() {
      update(this.emp).then(res => {
        this.$message.success(res.msg)
        this.dialogFormVisible2 = false
        this.emp = {}
        this.fetchManager()
      })
    },
    delManager(empId) {
      del(empId).then(res => {
        this.$message.success(res.msg)
        this.fetchManager()
      })
    },
    batchDelManager() {
      batchDel(this.multipleSelection.map(item => item.empId)).then(res => {
        this.$message.success(res.msg)
        this.fetchManager()
      })
    },
    fetchEmp(){
      fetchEmp(this.empRequest).then(res =>{
        console.log(res)
        this.empResponse.list = []
        this.empResponse = res.data
      })
    },
    addEmp() {
      add(this.emp).then(res => {
        this.$message.success(res.msg)
        this.dialogFormVisible3 = false
        this.emp = {}
        this.fetchManager()
      })
    },
    openEmp(empId){
      this.dialogFormVisible4 = true
      findById(empId).then(res =>{
        this.emp = res.data
      })
    },
    updateEmp() {
      update(this.emp).then(res => {
        this.$message.success(res.msg)
        this.dialogFormVisible4 = false
        this.emp = {}
        this.fetchManager()
      })
    },
    delEmp(empId){
      del(empId).then(res =>{
        this.$message.success(res.msg)
        this.fetchManager()
      })
    },
    batchDelEmp() {
      batchDel(this.multipleSelection.map(item => item.empId)).then(res => {
        this.$message.success(res.msg)
        this.fetchManager()
      })
    },
    findRoles() {
      findRoles().then(res => {
        this.roles = res.data
      })
    },
    findPosts() {
      findPosts().then(res => {
        this.posts = res.data
      })
    }
  },
  //声明周期 - 创建完成（可以访问当前this实例）,
  created() {
    this.fetchManager()
    this.findRoles()
    this.findPosts()
    this.fetchEmp()
  }
}
</script>
<style scoped>

</style>
