<template>
    <div class="employee-detail">
        <employee-info :isNew="isNew" ref="emmployInfo"></employee-info>
        <div class="btn-boxs">
            <el-button @click="save" type="primary">保存</el-button>
            <el-button type="primary" @click="save(1)" v-if="isNew">保存并继续添加</el-button>
            <el-button @click="cancel">取消</el-button>
        </div>
    </div>
</template>
<script>
import employeeInfo from "components/contacts/employeeInfo";
import { getRequest, postRequest, postJsonRequest } from '@/api/api';
export default {
    name: "employeeDetail",
    components: {
        employeeInfo
    },
    data() {
        return {
            managerMode: false
        };
    },
    methods: {
        save(contin) {
            let info = this.$refs.emmployInfo.info;
            console.log( info , "ssss");
            if (this.isNew) {
                this.addEmployee(info, contin);
            } else {
                this.saveEmployeeInfo(info);
            }
        },
        /**
         * 新增员工
         */
        addEmployee(info, contin) {
            let that = this;
            let params = {
                name: info.name,
                isClerk: 0,
                phoneNumber: info.phoneNumber,
                positionName: info.positionName,
                departmentId: info.departmentId,
                managerMode: info.managerMode*1
            };
            console.log(info, params);
            getRequest("/haoban-manage-web/emp/add", params)
            .then(res => {
                console.log(res, "add result");
                if (res.data.errorCode == 1) {
                    that.$message.success({
                        message: "操作成功"
                    });
                    if (contin == 1) {
                        that.$refs.emmployInfo.info  = {
                            name: "",
                            phoneNumber: "",
                            departmentId: "",
                            departmentName: "",
                            managerMode: false
                        };
                    } else {
                        window.history.go(-1);
                    }
                } else {
                    that.$message.error({
                        message: res.data.message
                    });
                }
            })
            .catch(e => {
                console.log(e, "error");
            });
        },
        saveEmployeeInfo(info) {
            let that = this;
            let params = {
                name: info.name,
                phoneNumber: info.phoneNumber,
                positionName: info.positionName,
                employeeClerkId: that.$route.query.employeeClerkId,
                managerMode: info.managerMode * 1
            };
            console.log(info, params);
            getRequest("/haoban-manage-web/emp/update", params)
            .then(res => {
                console.log(res, "update result");
                if (res.data.errorCode == 1) {
                    that.$message.success({
                        message: "操作成功"
                    });
                } else {
                    that.$message.error({
                        message: res.data.message
                    });
                }
            })
            .catch(e => {
                console.log(e, "error");
                that.$message.error({
                    message: e.message
                });
            });
        },
        cancel() {
            this.$confirm(" 是否确认取消，取消后当前页面信息将丢失 ？", "提示", {
                type: "warning"
            }).then(() => {
                window.history.go(-1);
            }).catch(e => {
                console.log(e);
            });
        }
    },
    computed: {
        isNew() {
            return !!this.$route.query.addnew == 1;
        }
    }
};
</script>
<style lang="scss">
.employee-detail {
    overflow: auto;
    height: 460px;
    .btn-boxs {
        width: calc(100% - 200px);
        padding: 12px 0;
        background: #fff;
        position: fixed;
        bottom: 0;
        right: 0;
        text-align: center;
    }
}
</style>

