<template>
  <!-- 在职员工 -->
  <div class="displsy-mod">
    <div style="display: flex;">
      <div style='height:38px'>
        <el-button size="mini" type="primary" @click="edit('添加')" v-role='4002002'>添加</el-button>
      </div>
    </div>
    <hr />
    <el-table :data="list" stripe style="width: 100%" :header-cell-style="{background: 'linear-gradient(#6cb3ff, #1873d4)',color:'#eeeeee'}">
      <el-table-column show-overflow-tooltip align="center" prop="brandNumber" label="水牌号" sortable min-width="80"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="staffName" label="员工名称" sortable min-width="90"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="sex" label="性别" sortable min-width="75">
        <template slot-scope="scope">
          <div v-if="scope.row.sex==1">男</div>
          <div v-else>女</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="mobilePhone" label="手机号码" sortable min-width="110"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="joinedDate" label="入职日期" sortable min-width="105"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="wordTypeName" label="工种" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="gradeName" label="APP权限" sortable min-width="80"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="postName" label="职务" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="type" label="员工类型" sortable min-width="80">
        <template slot-scope="scope">
          {{scope.row.type==0?"高管":scope.row.type==1?"普工":"收银"}}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align=" center" prop="source" label="员工来源" sortable min-width="80">
        <template slot-scope="scope">
          {{scope.row.source==0?"校招":scope.row.source==1?"社招":scope.row.source==2?"介绍":"其他"}}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="userState" label="是否用户" sortable min-width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.userState==1">是</div>
          <div v-else>否</div>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="wechat" label="微信" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="qq" label="qq" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" v-if='sessionStorageData.payroll!=1' prop="baseSalary" label="底薪" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="identityCard" label="身份证号" sortable min-width="160"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="popularity" label="人气" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="deptName" label="部门" sortable min-width="75">
        <template slot-scope="scope">
          {{scope.row.deptName==0?"美发部":scope.row.deptName==1?"美容部":scope.row.deptName==2?"其他部":"无"}}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="inputBy" label="创建人" sortable min-width="75"></el-table-column>
      <el-table-column show-overflow-tooltip align="center" prop="modifyBy" label="修改人" sortable min-width="75"></el-table-column>
      <el-table-column align="center" fixed="right" label="查看" width="76">
        <template slot-scope="scope">
          <el-button @click="examine(scope.row)" size="mini" type="primary">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="修改" width="76">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" size="mini" type="primary" plain v-role='4002003'>修改</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="作品" width="76">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="works(scope.row)" v-role='4002004'>作品</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="离职" width="76">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="leave(scope.row,leavesdialogFormVisible=true)" plain v-role='4002005'>离职</el-button>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="停用" width="76" v-role='4002006'>
        <template slot-scope="scope">
          <el-button @click="deactivate(scope.row)" size="mini" type="primary">停用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block" style="margin-top:15px;">
      <!-- 分页组件 -->
      <el-pagination align="left" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryForm.pageNum" :page-sizes="[10,20,30,40,50]" :page-size="queryForm.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"></el-pagination>
    </div>
    <!-- 工资弹窗 -->
    <el-dialog :close-on-click-modal="false" title="工资" :visible.sync="wagevisible" width="700px" v-dialogDrag>
      <div class="wage">
        <el-button size="mini" type="primary" @click="editwagevisibe(drawer = true)">增加修改</el-button>
        <el-form :inline="true" style="text-align:center">
          <el-form-item label="员工：">
            <span>{{wageList.staffName}}</span>
          </el-form-item>
          <el-form-item style="margin:10px 30px" label="月度任务：">
            <span>{{wageList.monthTask}}</span>
          </el-form-item>
          <el-form-item style="margin:10px 30px" label="底薪：">
            <span>{{wageList.baseSalary}}</span>
          </el-form-item>
          <el-form-item label="提成：">
            <span>{{wageList.monthRatio}}</span>
          </el-form-item>
          <br />
          <br />
          <el-form-item label="超额业绩1：">
            <span>{{wageList.monthHigherOne}}</span>
          </el-form-item>
          <el-form-item label="超额提成1：">
            <span>{{wageList.monthHigherOneRatio}}</span>
          </el-form-item>
          <el-form-item label="未达标业绩1：">
            <span>{{wageList.monthLowerOne}}</span>
          </el-form-item>
          <el-form-item label="未达标扣款1：">
            <span>{{wageList.monthLowerOneRatio}}</span>
          </el-form-item>
          <br />
          <br />
          <el-form-item label="超额业绩2：">
            <span>{{wageList.monthHigherTwo}}</span>
          </el-form-item>
          <el-form-item label="超额提成2：">
            <span>{{wageList.monthHigherTwoRatio}}</span>
          </el-form-item>
          <el-form-item label="未达标业绩2：">
            <span>{{wageList.monthLowerTwo}}</span>
          </el-form-item>
          <el-form-item label="未达标扣款2：">
            <span>{{wageList.monthLowerTwoRatio}}</span>
          </el-form-item>
          <br />
          <br />
          <el-form-item label="超额业绩3：">
            <span>{{wageList.monthHigherThree}}</span>
          </el-form-item>
          <el-form-item label="超额提成3：">
            <span>{{wageList.monthHigherThreeRatio}}</span>
          </el-form-item>
          <el-form-item label="未达标业绩3：">
            <span>{{wageList.monthLowerThree}}</span>
          </el-form-item>
          <el-form-item label="未达标扣款3：">
            <span>{{wageList.monthLowerThreeRatio}}</span>
          </el-form-item>
          <br />
          <br />
          <el-form-item label="超额业绩4：">
            <span>{{wageList.monthHigherFour}}</span>
          </el-form-item>
          <el-form-item label="超额提成4：">
            <span>{{wageList.monthHigherFourRatio}}</span>
          </el-form-item>
          <el-form-item label="未达标业绩4：">
            <span>{{wageList.monthLowerFour}}</span>
          </el-form-item>
          <el-form-item label="未达标扣款4：">
            <span>{{wageList.monthLowerFourRatio}}</span>
          </el-form-item>
        </el-form>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="wagevisible = false" plain>取 消</el-button>
        <el-button size="mini" type="primary" @click="wagevisible = false">确 定</el-button>
      </div>
    </el-dialog>
    <!--  工资弹窗   -->
    <el-dialog :close-on-click-modal="false" title="增加" :visible.sync="drawer" width="60%" v-dialogDrag>
      <div style=" height: 50vh;overflow: auto;">
        <el-form :model="editwageForm" label-width="120px">
          <el-col :span="12">
            <el-form-item label="月度任务">
              <el-input v-model="editwageForm.monthTask" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="底薪">
              <el-input v-model="editwageForm.baseSalary" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="提成">
              <el-input v-model="editwageForm.monthRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额业绩1">
              <el-input v-model="editwageForm.monthHigherOne" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额提成1">
              <el-input v-model="editwageForm.monthHigherOneRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额业绩2">
              <el-input v-model="editwageForm.monthHigherTwo" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额提成2">
              <el-input v-model="editwageForm.monthHigherTwoRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额业绩3">
              <el-input v-model="editwageForm.monthHigherThree" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额提成3">
              <el-input v-model="editwageForm.monthHigherThreeRatio" autocomplete="on"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="超额业绩4">
              <el-input v-model="editwageForm.monthHigherFour" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="超额提成4">
              <el-input v-model="editwageForm.monthHigherFourRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标业绩1">
              <el-input v-model="editwageForm.monthLowerOne" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标扣款1">
              <el-input v-model="editwageForm.monthLowerOneRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标业绩2">
              <el-input v-model="editwageForm.monthLowerTwo" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标扣款2">
              <el-input v-model="editwageForm.monthLowerTwoRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标业绩3">
              <el-input v-model="editwageForm.monthLowerThree" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标扣款3">
              <el-input v-model="editwageForm.monthLowerThreeRatio" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标扣款4">
              <el-input v-model="editwageForm.monthLowerFour" autocomplete="on"></el-input>
            </el-form-item>
            <el-form-item label="未达标扣款4">
              <el-input v-model="editwageForm.monthLowerFourRatio" autocomplete="on"></el-input>
            </el-form-item>
          </el-col>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="drawer = false" plain>取 消</el-button>
        <el-button size="mini" type="primary" @click="editWage(drawer=false)">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 离职弹窗 -->
    <el-dialog :close-on-click-modal="false" title="离职" :visible.sync="leavesdialogFormVisible" v-dialogDrag>
      <el-form :model="leaves">
        <el-form-item label="离职时间">
          <div class="block">
            <el-date-picker v-model="leaves.leaveDate" type="date" placeholder="选择日期"></el-date-picker>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" plain @click="leavesdialogFormVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="leaveconfirm(leavesdialogFormVisible=false)">确 定
        </el-button>
      </div>
    </el-dialog>
    <edit ref="edit" @editData="getPage"></edit>
    
  </div>
</template>
<script>
import edit from "./edit.vue";

import monthlytask from "../components/monthlyTask/monthlyTask";
import { staffPage, leave, monthJob,deactivate, } from "@/api/storeManage.js";
import Bus from "../Bus.js";
export default {
  components: {
    monthlytask,
    edit,
    
  },
  staffName: "",
  data() {
    return {
      leavesdialogFormVisible: false, //员工离职弹窗开关
      drawer: false, //员工工资弹窗开关
      wagevisible: false, //业绩提成弹窗开关
      list: [], //主数组
      queryForm: {
        pageNum: 1, // 当前页码
        pageSize: 10, // 每页的数据条数,
      },
      total: 0, //分页总条数 // 总条数
      sessionStorageData: null, //门店参数合集
      wageList: [], //业绩提成列表
      editwageForm: {
        //业绩提成表单
        id: "",
      },
      leaves: {
        //离职表单
        id: "",
        leaveDate: "",
      },
      onjob: {}, //在职表单
      searchTrue: false, //搜索判断
    };
  },
  methods: {
    identityCardInput(row) {
      var number = /(^\d{15}$)|bai(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
      if (number.test(row)) {
      } else {
        this.$message.error({
          message: "请输入正确的身份证号",
        });
      }
    },
    //查看
    examine(row) {
      this.$refs.edit.show(row, "查看");
    },
    //员工编辑
    edit(row) {
      if (row != "添加") {
        if (row.userState == 1) {
          row.userState = true;
        } else {
          row.userState = false;
        }
      }
      this.$refs.edit.show(row);
    },
    //工资弹窗
    wage(row) {
      this.editwageForm.id = row.id;
      this.wageList = {
        ...row,
      };
    },
    editwagevisibe() {
      this.editwageForm = {
        ...this.wageList,
      };
    },
    deactivate(row){
      this.$confirm("将该员工账号停用, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        cancelButtonClass: "cancelbtnFalses",
        confirmButtonClass: "confirmbtnFalses",
      })
        .then(() => {
          deactivate({
            id: row.id,
            mobilePhone: row.mobilePhone,
            brandNumber: row.brandNumber,
          })
            .then((res) => {
              //console.log(res);
              if (res.code == "000000") {
                this.$message.success({
                  message: "已经将该员工账号停用!",
                });
                this.getPage();
              } else {
                this.$alert(res.message, "提示", {
                  confirmButtonText: "确定",
                  confirmButtonClass: "confirmbtnFalses",
                  type: "warning",
                  center: true,
                  callback: (action) => {},
                });
              }
            })
            .catch((err) => {
              console.error(err);
            });
        })
        .catch(() => {
          this.$message.info({ message: "已取消操作！" });
        });
    },
    editWage() {
      if (this.editwageForm.row == true) {
        this.editwageForm.row = 1;
      } else {
        this.editwageForm.row = 0;
      }
      delete this.editwageForm.count;
      delete this.editwageForm.token;
      delete this.editwageForm.joinedDate;
      delete this.editwageForm.leaveDate;
      delete this.editwageForm.list;
      delete this.editwageForm.sendSmsStatus;
      monthJob(this.editwageForm)
        .then((res) => {
          if (res.code == "000000") {
            this.wagevisible = false;
            this.$message({
              type: "success",
              message: "修改成功!",
            });
            this.getPage();
          } else {
            this.$alert(res.message, "提示", {
              confirmButtonText: "确定",
              confirmButtonClass: "confirmbtnFalses",
              type: "warning",
              center: true,
              callback: (action) => {},
            });
          }
        })
        .catch((err) => {
          //console.log(err);
        });
    },
    //作品
    works(row) {
      this.$router.push({
        path: "/staffWorks",
        query: row,
      });
    },
    //离职
    leaveconfirm() {
      leave(this.leaves)
        .then((res) => {
          if (res.code == "000000") {
            this.$message.success({
              message: res.message,
            });
            this.getPage();
          } else {
            this.$alert(res.message, "提示", {
              confirmButtonText: "确定",
              confirmButtonClass: "confirmbtnFalses",
              type: "warning",
              center: true,
              callback: (action) => {},
            });
          }
        })
        .catch((err) => {
          console.log("err", err);
        });
    },
    leave(row) {
      this.leaves.id = row.id;
      this.leaves.brandNumber = row.brandNumber;
      this.leaves.mobilePhone = row.mobilePhone;
    },
    //分页方法
    handleSizeChange(val) {
      //   //console.log(`每页 ${val} 条`);
      if (this.searchTrue == true) {
        this.queryForm.pageSize = val;
        this.search();
      } else {
        this.getPage();
      }
    },
    handleCurrentChange(val) {
      //   //console.log(`当前页: ${val}`);
      this.queryForm.pageNum = val;
      if (this.searchTrue == true) {
        this.search();
      } else {
        this.getPage();
      }
    },
    getPage() {
      this.queryForm.state = 1;
      staffPage(this.queryForm)
        .then((res) => {
          res.pageInfo.list.forEach((item) => {
            if (item.row == 1) {
              item.row = true;
            } else if (item.row == 0) {
              item.row = false;
            }
          });
          this.list = res.pageInfo.list;
          this.total = res.pageInfo.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //搜索方法
    search() {
      this.searchTrue = this.onjob.searchTrue;
      staffPage(this.onjob).then((res) => {
        if (res.code == "000000") {
          res.pageInfo.list.forEach((item) => {
            if (item.row == 1) {
              item.row = true;
            } else if (item.row == 0) {
              item.row = false;
            }
          });
          this.list = res.pageInfo.list;
          this.total = res.pageInfo.total;
          if (this.onjob.searchTrue == false) {
            this.$message.success({
              message: "已刷新最新数据",
            });
          } else {
            if (res.pageInfo.total == 0) {
              this.$message.warning({
                message: "没有找到符合条件的员工信息",
              });
            }
          }
        } else {
          this.$alert(res.message, "提示", {
            confirmButtonText: "确定",
            confirmButtonClass: "confirmbtnFalses",
            type: "warning",
            center: true,
            callback: (action) => {},
          });
        }
      });
    },
  },
  mounted() {
    this.$nextTick(function () {
      this.$on("onjob", function () {
        this.getPage();
      });
    });
  },
  created() {
    Bus.$on("onjob", (val) => {
      //取  Bus.$on
      this.queryForm = { ...val, ...this.queryForm };
      this.search();
    });
    let list = JSON.parse(sessionStorage.getItem("list"));
    this.sessionStorageData = JSON.parse(
      sessionStorage.getItem("sessionStorageData")
    );
    list.forEach((item) => {
      if (item.id == 4002001) {
        this.getPage();
      }
    });
  },
};
</script>
<style scoped>
.el-input {
  max-width: 200px;
}
.el-select {
  width: 200px;
}
.el-input .el-input--suffix {
  width: 200px;
}
.grid-contents {
  border-radius: 4px;
  min-height: 36px;
  padding: 10px;
}
.el-form-item__content div:nth-child(1) {
  max-width: 200px;
}
.demo-drawer__footer {
  width: 100%;
  border: 1px solid #0e0d0d;
  color: black;
}
.wage {
  height: 50vh;
  overflow: auto;
  width: 650px;
  margin: 5px auto;
}
.wage .el-form {
  text-align: center;
}
.wage .el-form-item {
  margin: 10px 10px;
}
.examineForm {
  display: flex;
  justify-content: flex-start;
}
.examineForm .el-form-item {
  height: 30px;
}
</style>
