<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="grade">
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入年级"
          clearable
        />
      </el-form-item>
      <el-form-item label="班级:" prop="classes">
        <el-input
          v-model="queryParams.classes"
          placeholder="请输入班级"
          clearable
        />
      </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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table
      ref="table"
      :data="memberList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="身份证号" align="center" prop="cardid" /> -->
      <el-table-column label="年级" align="center" prop="grade" />
      <el-table-column label="班级" align="center" prop="classes" />
      <el-table-column label="社团部门" align="center" prop="orgName" />
      <el-table-column label="社团职位" align="center" prop="orgPosition" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column
        label="操作"
        align="center"
        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
      center
      :title="title"
      :visible.sync="open"
      width="45%"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="姓名:" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入姓名:"
            @focus="selectStudent"
          />
        </el-form-item>
        <el-form-item label="性别：" prop="sex">
          <el-select
            v-model="form.sex"
            placeholder="请输入性别"
            style="width: 100%"
          >
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="身份证号:" prop="cardid">
          <el-input v-model="form.cardid" placeholder="请输入身份证号" />
        </el-form-item> -->
        <el-form-item label="年级:" prop="grade">
          <el-input v-model="form.grade" placeholder="请输入年级" />
        </el-form-item>
        <el-form-item label="班级:" prop="classes">
          <el-input v-model="form.classes" placeholder="请输入班级" />
        </el-form-item>
        <el-form-item label="联系方式:" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-form-item label="社团部门:" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入社团部门" />
        </el-form-item>
        <el-form-item label="社团职位:" prop="orgPosition">
          <el-input v-model="form.orgPosition" placeholder="请输入社团职位" />
        </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>
    <!-- 选择学生 -->
    <StudentInfoSelect
      v-if="studentOptions.open"
      :openSelect.sync="studentOptions.open"
      :title="studentOptions.title"
      @getStudentList="getStudentList"
      @setStudentInfo="setStudentInfo"
    ></StudentInfoSelect>
  </div>
</template>

<script>
import {
  getMemberInfo, // 查询社团信息列表
  listMemberInfo, // 查询社团详细信息列表
  editMemberInfo, // 修改社团信息
  addMemberInfo, // 新增社团信息
  delMemberInfo, // 删除社团信息
} from "@/api/smartSchool/MassOrganization/memberManagement";
import StudentInfoSelect from "@/views/components/moduleComponets/studentInfoSelect/index";
import { listStudent } from "@/api/smartSchool/teachAffairAdministration/stuEssentialInfAlert/index.js";

export default {
  name: "communityStyle",
  dicts: ["sys_user_sex"],
  components: {
    StudentInfoSelect,
  },
  //   components: { picAvatar },
  data() {
    return {
      // 选择学生对话框
      studentOptions: {
        open: false,
        title: "选择学生",
      },
      memberList: [],
      pev: process.env.VUE_APP_BASE_API,

      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classes: "",
        grade: "",
      },
      // 表单参数
      form: {
        id: "",
        name: "",
        sex: "",
        // cardid: "",
        grade: "",
        classes: "",
        orgName: "",
        orgPosition: "",
        phone: "",
        userid: "",
      },
      // 表单校验
      rules: {},
      options: [],
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 选择学生
    selectStudent() {
      this.studentOptions.open = true;
    },
    /** 获取学生信息列表 */
    getStudentList(queryForm, callback) {
      listStudent({ ...queryForm, gradeId: this.gradeId })
        .then((response) => {
          callback(response.rows, response.total);
        })
        .catch((error) => {
          callback([], 0);
        });
    },
    /** 设置学生信息 */
    setStudentInfo(item) {
      this.form.ids = item.id;
      console.log(this.form.ids, "this.form.ids11111");
      console.log(item, "item");
      this.$set(this.form, "name", item.studentName);
      this.$set(this.form, "sex", item.sex);
      //   this.$set(this.form, "cardid", item.idCard);
      this.$set(this.form, "grade", item.gradeName);
      this.$set(this.form, "classes", item.className);
      this.$set(this.form, "phone", item.stuTelephone);
    },
    /** 查询列表 */
    getList() {
      const params = {
        cid: this.$route.query.id,
        name: this.queryParams.name,
        grade: this.queryParams.grade,
        classes: this.queryParams.classes,
      };
      getMemberInfo(params).then((response) => {
        // console.log("response111", response);
        this.memberList = response.rows;
        // console.log("this.memberList", this.memberList);
        this.total = response.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.queryParams = {};
      this.imageUrl = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.throttle(() => {
        this.queryParams.pageNum = 1;
        this.getList();
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      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.reset();
      const ids = row.id || this.ids;
      listMemberInfo(ids).then((response) => {
        // console.log("response", response);
        this.form = response.data;
        this.open = true;
        this.title = "修改社团成员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   this.$modal.loading("正在上传数据，请稍等...");
          if (this.form.id != null) {
            // 修改
            const paramsedit = {
              id: this.form.id,
              name: this.form.name,
              sex: this.form.sex,
              //   cardid: this.form.cardid,
              grade: this.form.grade,
              classes: this.form.classes,
              orgPosition: this.form.orgPosition,
              phone: this.form.phone,
              orgName: this.form.orgName,
            };
            editMemberInfo(paramsedit)
              .then((response) => {
                // this.$modal.closeLoading();
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {});
          } else {
            // 新增
            // 获取当前登录人userId
            // const userName = this.$store.state.user.userId;
            const params = {
              cid: this.$route.query.id,
              orgName: this.form.orgName,
              name: this.form.name,
              sex: this.form.sex,
              //   cardid: this.form.cardid,
              grade: this.form.grade,
              classes: this.form.classes,
              orgPosition: this.form.orgPosition,
              phone: this.form.phone,
              userId: this.form.ids,
            };
            addMemberInfo(params)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {});
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (ids) {
        this.$modal
          .confirm(
            `是否确认删除选中的${
              Object.hasOwnProperty.call(ids, "length") ? ids.length : 1
            }条数据？`
          )
          .then(() => {
            return delMemberInfo(ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      }
    },
  },
};
</script>
<style scoped>
.avatar,
.avatar-uploader,
.el-icon-plus {
  width: 650px;
}
</style>
