<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="组织名称" prop="tenantName">
        <el-input
          v-model="queryParams.param.tenantName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listData"
      @selection-change="handleSelectionChange"
      max-height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        fixed
        label="昵称"
        align="center"
        prop="nickName"
        min-width="100"
      />
      <el-table-column
        fixed
        label="用户名"
        align="center"
        prop="userName"
        show-overflow-tooltip
        min-width="100"
      >
      </el-table-column>
      <el-table-column
        fixed
        label="组织名称"
        align="center"
        prop="tenantName"
        show-overflow-tooltip
        min-width="80"
      />
      <el-table-column
        label="状态"
        align="center"
        show-overflow-tooltip
        min-width="100"
      >
        <template slot-scope="scope" v-if="scope.row.status != null">
          <span v-if="scope.row.status == 0" style="color: #67c23a">启用</span>
          <span v-if="scope.row.status == 1" style="color: #f56c6c">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人/创建时间" align="center" min-width="140">
        <template slot-scope="scope">
          <div>{{ scope.row.createBy }}</div>
          <span>
            {{ parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}") }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="200"
      >
        <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
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-refresh"
            @click="handleResetPwd(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
      class="popup"
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        class="formStep"
      >
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="密码" prop="password" v-if="!form.id">
          <el-input v-model="form.password" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="组织" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择" filterable>
            <el-option
              v-for="item in tenantsDataList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <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
      class="popup"
      title="重置密码"
      :visible.sync="resetOpen"
      width="780px"
      append-to-body
    >
      <el-form
        ref="pasForm"
        :model="form"
        :rules="rules"
        label-width="80px"
        class="formStep"
      >
        <el-form-item label="新密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPasForm">确 定</el-button>
        <el-button @click="resetOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  tenantsList,
  tenantUsersList,
  tenantUsersAdd,
  tenantUsersUpd,
  tenantUsersDel,
  dataSourcesList,
  resetPwd,
} from "@/api/member";
import { followupQuery } from "@/api/followupFile";
export default {
  name: "Notice",
  dicts: ["sys_normal_disable", "sys_user_sex"],
  data() {
    return {
      tenantsDataList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          tenantName: "",
        },
      },
      listData: [],
      title: "",
      resetOpen: false,
      open: false,
      total: 0,
      form: {},
      loading: false,
      showSearch: true,
      multiple: false,
      // 表单校验
      rules: {
        nickName: [
          {
            required: true,
            message: "昵称不能为空",
            trigger: "blur",
          },
        ],
        userName: [
          {
            required: true,
            message: "账户不能为空",
            trigger: "blur",
          },
        ],
        tenantId: [
          {
            required: true,
            message: "所属组织不能为空",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur",
          },
          {
            pattern: /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[^\da-zA-Z\s]).{8,20}$/,
            message: "必须包含数字、字母、特殊字符, 长度为8到20位",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getTenantsList();
  },
  methods: {
    // 数据源
    getTenantsList() {
      tenantsList({
        pageNum: -1,
        param: {},
      }).then((res) => {
        this.tenantsDataList = res.data.list;
      });
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      tenantUsersList(this.queryParams).then((res) => {
        this.listData = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        tenantId: "",
        userName: "",
        nickName: "",
        userName: "",
        status: "0",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.param = {
        tenantName: "",
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true;
      this.title = "修改用户";
      this.form = JSON.parse(JSON.stringify(row));
    },
    /** 诊疗档案 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            tenantUsersUpd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            tenantUsersAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleResetPwd(row) {
      this.resetOpen = true;
      this.form = JSON.parse(JSON.stringify(row));
      this.form.password = "";
    },
    submitPasForm(row) {
      this.$refs["pasForm"].validate((valid) => {
        if (valid) {
          resetPwd(this.form).then((response) => {
            this.$modal.msgSuccess("重置成功");
            this.resetOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const idList = row.id ? [row.id] : this.ids;
      this.$modal
        .confirm("是否确认删除当前选择的数据？")
        .then(function () {
          return tenantUsersDel({
            idList: idList,
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download1(
        "/treatment/exportTreatment",
        {
          ...this.queryParams.params,
        },
        `诊疗档案.xlsx`
      );
    },
    /** 下载按钮操作 */
    handleDownload() {
      window.open(
        `${process.env.VUE_APP_API_QZURL}/profile/TreamentTemplate.xlsx`
      );
      // this.download1(
      // 	"/patient/export", {},
      // 	`患者档案导入模版.xlsx`
      // );
    },
    /** 导入按钮操作 */
    handleImport() {
      this.fileList = [];
      this.importOpen = true;
    },
  },
};
</script>
<style scoped src="@/assets/styles/common.css"></style>

<style scoped>
.div-title1 {
  font-size: 22px;
  font-weight: bold;
  margin-bottom: 10px;
}

.div-title2 {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 10px;
}

.div-title3 {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 10px;
}

.span-but {
  display: inline-block;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  line-height: 32px;
  padding: 0 15px;
  margin: 5px;
}

.span-but-active {
  border: 1px solid #1890ff;
}

.human-body {
  display: flex;
  flex-wrap: wrap;
}

.human-body >>> .el-form-item {
  width: 49%;
  margin-right: 2%;
}

.human-body >>> .el-form-item:nth-of-type(2n) {
  margin-right: 0;
}

.formStep1 >>> .el-form-item__label {
}

.form-item-zd {
  width: 100%;
  text-align: left;
}

.form-item-age {
  display: flex;
  align-items: center;
}

.form-item-age span {
  margin: 0 10px;
}

.form-item-age >>> .el-input {
  width: 100px;
}

>>> .el-drawer.rtl {
  width: 50% !important;
}
</style>
