<template>
    <!-- 弹出框表单 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row>
                <Field label="姓名" prop="name" v-model="form.name" placeholder="请输入姓名" :maxLength="20"/>
                <Field label="电话号码" prop="phone" v-model="form.phone" placeholder="请输入电话号码" :maxLength="11"/>
                <Field label="工号" prop="number" v-model="form.number" placeholder="请输入工号" :maxLength="15"/> 
            </el-row>

            <el-form-item label="所属部门" prop="deptId">
                <el-cascader v-model="deptList" style="width:50%" @change="cascaderChange" :options="dict.deptId" clearable
                :props="{label:'label',value:'id'}">
                </el-cascader>
            </el-form-item>

            <el-form-item label="照片" prop="photoPath">
                <ImageUpload :value="form.photoPath" 
                @input="replaceImage"></ImageUpload>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" v-if="pageInfo.type !== 'view'" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    import dialogShow from "./dialogshow";
    import {findParentIds} from "@/assets/utils/common.js"
    export default {
        mixins: [form],
        components: {
            dialogShow ,
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "工作人员",
                // 是否显示弹出层
                open: false,
                toString:[
                ],
                // 表单校验
                rules: {
                    name: [
                        {required: true,message: "请输入姓名", trigger: "blur" },
                        {max: 20,message: "最多只能录入20个字符",trigger: "blur",},
                    ],
                    phone: [
                        {required: true,message: "请输入电话号码", trigger: "blur" },
                        {max: 11,message: "最多只能录入个字符",trigger: "blur",},
                    ]
                },
                deptList:[]
            }
        },
        methods: {
            // formSelect(){
            //     let treeId = getTreeIds(this.dict.deptId,this.form.deptId)
            // },
            cascaderChange(val){
                if(this.deptList.length > 0){
                    this.form.deptId = this.deptList[this.deptList.length - 1]
                }else{
                    this.form.deptId = ''
                }
                console.log(this.form.deptId,'当前选择')
            },
            replaceImage(val){
                this.form.photoPath = val
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="workman/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "编辑";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "workman/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增";

            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="workman/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "工作人员详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
                this.deptList = []
            },

            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
                this.deptList = findParentIds(this.dict.deptId,this.form.deptId).reverse()
                this.deptList = this.deptList.concat(this.form.deptId)
                console.log(this.deptList,'ddd')
            },

            afterSubmit(data) {
                this.open = false;
                this.reset()
                this.deptList = []
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    deptId : 0,
                    name : "",
                    number : "",
                    phone : "",
                    photoPath : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            }
        }
    }
</script>
<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}
.avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}
.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}
.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>
