<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="关键字" prop="searchValue">
            <el-input v-model="queryParams.searchValue" placeholder="请输入员工姓名，编号，手机号" maxlength="20" clearable style="width: 240px"/>
        </el-form-item> 
        <el-form-item label="性别" prop="sex">
            <el-select v-model="queryParams.sex" style="width: 240px" clearable> 
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>  
            </el-select>   
        </el-form-item>
        <el-form-item label="岗位" prop="postIds">
            <el-select v-model="queryParams.postIds" multiple style="width: 100%;" placeholder="请选择岗位">
                <el-option
                    v-for="item in postOptions"
                    :key="item.postId"
                    :label="item.postName"
                    :value="item.postId"
                    :disabled="item.status == 1"
                ></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="人脸状态" prop="faceStatus">
            <el-select v-model="queryParams.faceStatus" style="width: 240px" clearable> 
                <el-option label="开启" value="0"></el-option>
                <el-option label="停用" value="1"></el-option>  
            </el-select>   
        </el-form-item>
        <el-form-item label="健康证" prop="healthCertificate">
            <el-select v-model="queryParams.healthCertificate" placeholder="健康证状态" style="width: 240px" clearable> 
                <el-option label="正常" value="0"></el-option>
                <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-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>  
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="tableListData" height="800">
        <el-table-column label="序号" align="center" width="80" type="index">
        <template slot-scope="scope">
            <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
        </el-table-column>
        <el-table-column label="员工编号" align="center" prop="staffNo" :show-overflow-tooltip="true" width="100" />
        <el-table-column label="员工名称" align="center" prop="userName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="手机号" align="center" prop="phonenumber" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="性别" align="center" prop="sex" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="岗位名称" align="center" prop="postName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="员工照片" align="center" prop="canteenName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="人脸状态" align="center" prop="canteenName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="健康证照片" align="center" prop="canteenName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="出入权限" align="center" prop="canteenName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="健康证状态" align="center" prop="canteenName" :show-overflow-tooltip="true" width="150" />
        <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width">
        <template slot-scope="scope"> 
            <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)" 
            >编辑</el-button>
            <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)" 
            >删除</el-button>
        </template>
        </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body> 
        <div style="width: 100%;height: 600px;overflow-y: auto;">
            <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="24">
                        <div style="font-size: 18px;font-weight: bold;border-left: 4px solid #1890FF;padding-left: 4px;margin-bottom: 20px;">
                            个人信息
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="员工名称:" prop="userId">
                        <el-cascader v-model="form.userId"
                            :options="userTreeOptions" :filterable="true" style="width: 100%"
                            ref="userCascader"
                            :props="{ 
                                multiple: false,
                                emitPath: false,// 若设置 false，则只返回该节点的值，只返回最后选择的id
                                checkStrictly: false,//来设置父子节点取消选中关联，从而达到选择任意一级选项的目的
                                value:'id',label:'label' 
                            }" clearable @change="handleUserChange">
                            </el-cascader> 
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="员工编号:" prop="staffNo">
                            <el-input v-model="form.staffNo"
                                placeholder="请输入员工编号"
                                maxlength="30"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="员工性别:" prop="sex">
                            <el-select v-model="form.sex" disabled clearable style="width: 100%;"> 
                                <el-option label="男" value="0"></el-option>
                                <el-option label="女" value="1"></el-option>  
                            </el-select>   
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="手机号:" prop="phonenumber">
                            <el-input v-model="form.phonenumber"
                                placeholder="请输入手机号" disabled
                                maxlength="11"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="关联食堂:" prop="canteenId">
                            <el-select v-model="form.canteenId" style="width: 100%;" placeholder="请选择关联食堂">
                                <el-option
                                    v-for="item in canteenOptions"
                                    :key="item.canteenId"
                                    :label="item.canteenName"
                                    :value="item.canteenId" 
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="岗位名称:" prop="postIds">
                            <el-select v-model="form.postIds" multiple disabled style="width: 100%;" placeholder="请选择岗位">
                                <el-option
                                    v-for="item in postOptions"
                                    :key="item.postId"
                                    :label="item.postName"
                                    :value="item.postId"
                                    :disabled="item.status == 1"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col> 
                    <el-col :span="24">
                        <div style="font-size: 18px;font-weight: bold;border-left: 4px solid #1890FF;padding-left: 4px;margin-bottom: 20px;">
                            健康证信息
                        </div>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="健康证到期:" prop="healthCertExpire">
                            <el-date-picker v-model="form.healthCertExpire" type="date" value-format="yyyy-MM-dd" placeholder="选择日期" style="width: 45%;">
                        </el-date-picker> 
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="健康证正面照片:" prop="healthCertFrontImg">
                        <el-upload
                                :http-request="
                                    (obj) => imgUpLoad(obj, 'fileUrl')
                                "
                                action="#"
                                :limit="1"
                                :file-list="fileList"
                                :show-file-list="true"
                                list-type="picture-card"
                                accept=".png, .jpg, .jpeg" 
                                :class="{ disabled: uploadDisabled }"
                                :before-upload="handleBeforeUpload"
                                :on-preview="handlePictureCardPreview"
                                :on-remove="handleRemove"
                            >
                                <i
                                    class="el-icon-plus avatar-uploader-icon"
                                ></i>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="健康证反面照片:" prop="healthCertBackImg">
                        <el-upload
                                :http-request="
                                    (obj) => imgUpLoad2(obj, 'fileUrl')
                                "
                                action="#"
                                :limit="1"
                                :file-list="fileList2"
                                :show-file-list="true"
                                list-type="picture-card"
                                accept=".png, .jpg, .jpeg" 
                                :class="{ disabled: uploadDisabled2 }"
                                :before-upload="handleBeforeUpload"
                                :on-preview="handlePictureCardPreview"
                                :on-remove="handleRemove2"
                            >
                                <i
                                    class="el-icon-plus avatar-uploader-icon"
                                ></i>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <div style="font-size: 18px;font-weight: bold;border-left: 4px solid #1890FF;padding-left: 4px;margin-bottom: 20px;">
                            出入权限
                        </div>
                    </el-col>
                </el-row>
                <el-row :span="24" v-for="(item,index) in accessAuthorityList" :key="index">
                    <el-col :span="8">
                        <el-form-item label="关联门禁设备:" prop="devicesId">
                            <el-select v-model="item.devicesId" style="width: 100%;" placeholder="请选择关联门禁设备">
                                <el-option
                                    v-for="item in canteenOptions"
                                    :key="item.canteenId"
                                    :label="item.canteenName"
                                    :value="item.canteenId" 
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="设备位置:" prop="location">
                            <el-input v-model="item.staffNo"
                                placeholder="设备位置"
                                maxlength="30" disabled
                            />
                        </el-form-item>
                    </el-col> 
                    <el-col :span="8" style="display: flex;align-items: center;height: 40px;padding-left: 20px;">
                        <el-button
                            type="primary"
                            plain 
                            size="mini"
                            @click="addDevice"
                            >新增</el-button>
                        <el-button
                            type="info"
                            plain 
                            size="mini" v-if="index>0"
                            @click="delDevice(index)"
                            >新增</el-button>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <div style="font-size: 18px;font-weight: bold;border-left: 4px solid #1890FF;padding-left: 4px;margin-bottom: 20px;">
                            留样柜权限
                        </div>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="留样柜权限:" prop="accountStatus">
                            <el-switch v-model="form.accountStatus"
                                active-text="开启" inactive-text="关闭"
                                :active-value="0" :inactive-value="1">
                            </el-switch>
                        </el-form-item>
                    </el-col> 
                </el-row>
            </el-form> 
        </div> 
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" width="700px">
        <img style="width: 100%;height: 100%;" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script> 
import { getCanteenByAreaApi } from "@/api/base/stall"; 
import { deptUserTreeApi,getUserInfoApi,getStaffListApi,getStaffInfoApi,addKitchenStaffApi,editKitchenStaffApi,delStaffApi} from '@/api/kitchen/staff';
import { imgUpLoadTwo } from '@/api/system/upload';
export default {
    name: "",
    dicts: [],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            //表格数据
            tableListData: [],
            // 岗位选项
            postOptions: [],
            // 人员选项
            userTreeOptions:[],
            // 食堂选项
            canteenOptions:[],
            //员工设备权限
            accessAuthorityList:[
                {
                    devicesI:"",
                    locatio:""
                }
            ],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false, 
            // 查询参数
            queryParams: {
            pageNum: 1,
            pageSize: 10,
            canteenName: undefined,
            areaNameStr: undefined, 
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            userId: [
                { required: true, message: "员工不能为空", trigger: "change" }
            ],
            canteenId: [
                { required: true, message: "关联食堂不能为空", trigger: "change" }
            ],
            staffNo: [
                { required: true, message: "员工编号不能为空", trigger: "blur" }
            ]
            },
            fileList: [],//图片
            checkUrlList: [],//图片
            fileList2: [],//图片
            checkUrlList2: [],//图片 
            dialogVisible:false,//图片弹窗
            dialogImageUrl:"",//图片弹窗  
        };
    },
    created() { 
        this.getPostList();
        this.getList();
    },
    computed: {
        //图片上传1张后，隐藏上传框
        uploadDisabled() {
            return this.checkUrlList.length > 0
        },
        uploadDisabled2() {
            return this.checkUrlList2.length > 0
        },
    },
    methods: {
        // 岗位选项
        getPostList(){
            getUserInfoApi().then((response) => {
                this.postOptions = response.posts 
            })
        },
        // 员工选项
        deptUserTree(){
            deptUserTreeApi({}).then((response) => {
                this.userTreeOptions = response.data 
            })
        },
        // 食堂选项
        getCanteenList(){
            getCanteenByAreaApi({}).then((response) => {
                this.canteenOptions = response.rows 
            })
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() { 
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 查询列表 */
        getList() {
            this.loading = true;
            let param = {
                "pageNum": this.queryParams.pageNum, 
                "pageSize": this.queryParams.pageSize,  
            }
            getStaffListApi(param).then(response => {
                this.tableListData = response.rows;
                this.total = Number(response.total);
                this.loading = false;
            });
        },  
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "新增";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset(); 
            getStaffInfoApi(row.staffId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改";
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.deptUserTree();
            this.getCanteenList();
            this.form = {};
            this.resetForm("form");
        },
        //选中员工查询信息
        handleUserChange(e){
            let arr = this.$refs['userCascader'].getCheckedNodes()
            let data = arr[0].data
            getUserInfoApi(data.nodeId).then((response) => {
                console.log(response.data)
                let userInfo = response.data
                this.$set(this.form,"userId",userInfo.userId)
                this.$set(this.form,"sex",userInfo.sex)
                this.$set(this.form,"phonenumber",userInfo.phonenumber)
                this.$set(this.form,"postIds",response.postIds)
            })
        },
        //添加设备
        addDevice(){

        },
        //删除设备
        delDevice(index){

        },
        /** 提交按钮 */
        submitForm: function() {
            this.$refs["form"].validate(valid => {
            if (valid) {
                if (this.form.staffId != undefined) {
                    editKitchenStaffApi(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                } else {
                    addKitchenStaffApi(this.form).then(response => {
                        this.$modal.msgSuccess("新增成功");
                        this.open = false;
                        this.getList();
                    });
                }
            }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) { 
            this.$modal.confirm('是否确认删除数据项？').then(function() {
                return delStaffApi(row.staffId);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {});
        },
        // 图片上传
        imgUpLoad(param, name, index) {
            param.type = 'canteen'
            imgUpLoadTwo(param).then((res) => {
                if (res.code == 200) {
                    this.checkUrlList.push(res.data.url) 
                } else { 
                    this.$modal.msgError(res.msg)
                }
            })
            .catch((error) => { 
                this.$modal.msgError(error)
            })
        },
        // 上传之前
        handleBeforeUpload(file) { 
            const isLt = file.size / 1024 / 1024 < 5
            if (!isLt) {
                this.$modal.msgError(`图片大小不能超过 5 MB`)
                return false
            }
        },
        handleExceed(files, fileList) {
            this.$message.warning('最多只可以上传一张图片')
        },
        handleRemove(file, fileList) {
            let sum = 0
            this.checkUrlList.forEach((item, index) => {
                if (item == file.url) {
                    sum = index
                }
            })
            this.checkUrlList.splice(sum, 1)
        },
        handleRemove2(file, fileList) {
            let sum = 0
            this.checkUrlList2.forEach((item, index) => {
                if (item == file.url) {
                    sum = index
                }
            })
            this.checkUrlList2.splice(sum, 1)
        },
        //图片点击查看
        handlePictureCardPreview(file) {
            console.log(file)
            this.dialogImageUrl = file.url
            this.dialogVisible = true
        }
  }
};
</script>
<style lang="scss" scoped>
  //隐藏图片上传框的css
  ::v-deep.disabled {
      .el-upload--picture-card {
          display: none;
      }
  }
</style>