<template>
    <div class="page">
        <LayoutTable :data="tableData" :config="tableConfig">
<!--                        <el-button
                    slot="table-head-left2"
                    style="margin-left: 10px"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="handleImport"
            >导入</el-button>
            <el-button
                    slot="table-head-left2"
                    style="margin-left: 10px"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="doExport"
                    :disabled="isExport"
            >导出</el-button>-->
        </LayoutTable>
        <!-- 工作人员导入对话框 -->
        <el-dialog
                :title="upload.title"
                :visible.sync="upload.open"
                width="400px"
                append-to-body
        >
            <el-upload
                    ref="upload"
                    :limit="1"
                    accept=".xlsx, .xls"
                    :headers="upload.headers"
                    :action="upload.url + '?updateSupport=' + upload.updateSupport"
                    :disabled="upload.isUploading"
                    :on-progress="handleFileUploadProgress"
                    :on-success="handleFileSuccess"
                    :auto-upload="false"
                    drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或
                    <em>点击上传</em>
                </div>
                <div class="el-upload__tip" slot="tip">
                    <el-checkbox
                            v-model="upload.updateSupport"
                    />是否更新已经存在的数据
                    <el-link type="primary" style="font-size: 14px" @click="downloadTemplate"
                    >下载模板</el-link
                    >
                </div>
                <div class="el-upload__tip" style="color: red" slot="tip">
                    提示：仅允许导入“xls”或“xlsx”格式文件！
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>


      <el-dialog
          :title="user.title"
          :visible.sync="user.open"
          width="400px"
          append-to-body
      >
        <el-form label-width="80px">
          <el-form-item label="新密码">
            <el-input v-model="newPwd"></el-input>
          </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitUserForm">确 定</el-button>
          <el-button @click="user.open = false">取 消</el-button>
        </div>
      </el-dialog>

        <drawer-show ref="drawerform" @ok="getData" />
    </div>
</template>

<script>
    /** 表单弹出框模式需引入 */
    import drawerShow from "./drawershow";
    import table from "@/assets/mixins/table";
    export default {
        name: "WorkmanList",
        components: {
            drawerShow
},
        mixins: [table],
        created() {
        },
        methods: {
            /** 导入 */
            handleImport() {
                this.upload.title = "工作人员导入";
                this.upload.open = true;
            },
            /** 下载模板操作 */
            downloadTemplate() {
                this.isExport = true;
                this.$download("/workman/downloadTemplate", {}, { type: "excel" })
                    .then(() => (this.isExport = false))
                    .catch((error) => {
                        this.isExport = false;
                        this.$message.error(error.message);
                    });
            },
            /** 文件上传中处理 */
            handleFileUploadProgress(event, file, fileList) {
                this.upload.isUploading = true;
            },
            /** 文件上传成功处理 */
            handleFileSuccess(response, file, fileList) {
                this.upload.open = false;
                this.upload.isUploading = false;
                this.$refs.upload.clearFiles();
                this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
                this.getData();
            },
            /** 提交上传文件 */
            submitFileForm() {
                this.$refs.upload.submit();
            },
            /** 导出Excel */
            doExport() {
                this.isExport = true;
                this.$download("/workman/exportExcel", {
                "idList": this.selection,
                }, { type: "excel" }).then(() => this.isExport = false).catch(error => {
                    this.isExport = false;
                    this.$message.error(error.message);
                });
            },
            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.drawerform.add(row);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.$refs.drawerform.edit(row);
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.drawerform.view(row);
            },

          handleChangePwd(row) {
            this.user.title = "修改密码";

            this.loginName=row.loginName;
            this.newPwd=null;

            this.user.open = true;
          },

          submitUserForm() {
            this.$post("/workman/change/password", {
              loginName: this.loginName,
              newPwd: this.newPwd
            })
                .then((res) => {
                  if (res.code == 1) {
                    this.$message.success("批量更新大厅设备成功！");
                    this.user.open = false;

                    this.getData();
                  }
                })
                .catch((error) => {
                  this.$message.error(error.message);
                });
          },

        },
        data() {
            return {
              loginName:null,
              newPwd:null,
                // 用户导入参数
                upload: {
                    // 是否显示弹出层（工作人员导入）
                    open: false,
                    // 弹出层标题（工作人员导入）
                    title: "导入工作人员数据",
                    // 是否禁用上传
                    isUploading: false,
                    // 是否更新已经存在的数据
                    updateSupport: 0,
                    // 上传的地址
                    url: "/office/workman/importData",
                },
              user: {
                // 是否显示弹出层（工作人员导入）
                open: false,
                // 弹出层标题（工作人员导入）
                title: "修改密码",
              },
                isExport: false,
                config: {
                    search: [
                      {
                        name: "name",
                        type: "text",
                        label: "姓名",
                        fuzzy: true
                      },
                      {
                        name: "roomId",
                        type: "select",
                        label: "所属房间"
                      },
                      {
                        name: "workStatus",
                        type: "select",
                        label: "工作状态"
                      }
                    ],
                    columns: [
                        {type: "selection", width: 60},
                        {type: "index",label: "序号",width: 50},

                      {width: 100,label: "照片", prop: "photoPath",formatter: (row) => {
                          return row.photoPath != "" ? (
                              <el-image
                                  style="width: 70px; height: 100px"
                                  src={row.photoPath}
                                  preview-src-list={[row.photoPath]}
                              ></el-image>
                          ) : (
                              "--"
                          );
                        },},

                      {label: "姓名", prop: "name"},

                      {label: "所属部门", prop: "deptName"},

                        {label: "所属房间", prop: "roomName"},

                      {label: "职位", prop: "userPost"},

                      {label: "电话", prop: "phone"},
                      {label: "手机", prop: "mobile"},


                        {label: "工作状态", prop: "workStatus"},

                        {
                            label: "操作",
                            width: 240,
                            formatter: row => {
                                return (
                                    <div>
                                    <table-buttons noAdd row={row} onEdit={this.toEdit} onView={this.toView} onDel={this.toDel} />

                                      <span> </span>
                                      <el-button
                                          type="text"
                                          size="mini"
                                          onClick={() => this.handleChangePwd(row)}
                                      >
                                        修改密码
                                      </el-button>


                                    </div>

                            );
                            }
                        }
                    ]
                }
            };
        }
    };
</script>
