<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="所属幼儿园" prop="kindergartenId">
        <treeselect
          :normalizer="normalizer"
          :options="kindergartenInfoList"
          placeholder="请选择所属幼儿园"
          v-model="queryParams.kindergartenId"
          style="width: 208px"
        />
      </el-form-item>
      <el-form-item label="班级名称" prop="className">
        <el-input
          v-model="queryParams.className"
          placeholder="请输入班级名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班主任姓名" prop="classTeacher">
        <el-input
          v-model="queryParams.classTeacher"
          placeholder="请输入班主任姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          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"
          v-hasPermi="['system:classinfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:classinfo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:classinfo:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:classinfo:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="classinfoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="所属幼儿园"
        align="center"
        prop="kindergartenName"
      />
      <el-table-column label="班级名称" align="center" prop="className" />
      <el-table-column label="班级类型" align="center" prop="classType">
        <template slot-scope="scope">
          <!-- {{scope.row.classType}} -->
          {{ scope.row.classType == dictValue1 ? "大班" : "" }}
          {{ scope.row.classType == dictValue2 ? "中班" : "" }}
          {{ scope.row.classType == dictValue3 ? "小班" : "" }}
          {{ scope.row.classType == dictValue4 ? "毕业" : "" }}
        </template>
      </el-table-column>
      <el-table-column label="班主任姓名" align="center" prop="classTeacher" />
      <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)"
            v-hasPermi="['system:classinfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:classinfo:remove']"
            >删除</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属幼儿园" prop="kindergartenId">
          <el-button
            type=""
            @click="innerVisible = true"
            style="
              width: 360px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              padding-left: -10px;
            "
            v-if="kindergartenName == '请选择所在幼儿园'"
            >{{ kindergartenName }}</el-button
          >

          <el-button
            type=""
            @click="innerVisible = true"
            style="width: 360px; text-align: left; height: 36px"
            v-else
            >{{ kindergartenName }}</el-button
          >
        </el-form-item>
        <el-form-item label="班级名称" prop="className">
          <el-input v-model="form.className" placeholder="请输入班级名称" />
        </el-form-item>

        <el-form-item label="班级类型" prop="classType">
          <el-radio-group v-model="form.classType">
            <el-radio :label="dictValue1">大班</el-radio>
            <el-radio :label="dictValue2">中班</el-radio>
            <el-radio :label="dictValue3">小班</el-radio>
            <el-radio :label="dictValue4">毕业</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="班主任姓名" prop="classTeacher">
          <el-input
            v-model="form.classTeacher"
            placeholder="请输入班主任姓名"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model.number="form.phone"
            placeholder="请输入联系电话"
            maxlength="11"
          />
        </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
        width="850px"
        title="选择所属幼儿园"
        :visible.sync="innerVisible"
        append-to-body
      >
        <el-form
          :model="queryParams2"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="90px"
        >
          <el-form-item label="幼儿园名称" prop="kindergartenName">
            <el-input
              v-model="queryParams2.kindergartenName"
              placeholder="请输入幼儿园名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="幼儿园地址" prop="kindergartenAddress">
            <el-input
              v-model="queryParams2.kindergartenAddress"
              placeholder="请输入幼儿园地址"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery2"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <el-table
          :data="kindergartenInfoList"
          @row-click="kindergartenNameclick"
        >
          <el-table-column label="请选择" width="70" align="center">
            <template slot-scope="scope">
              <el-button
                type="primary"
                v-if="kindergartenid2 == scope.row.id"
                style="width: 15px; height: 15px"
                circle
                @click="kindergartenNameclick(scope.row)"
              ></el-button>

              <el-button
                v-else
                style="width: 15px; height: 15px"
                circle
                @click="kindergartenNameclick(scope.row)"
              ></el-button>
            </template>
          </el-table-column>

          <el-table-column
            property="kindergartenName"
            label="幼儿园名字"
            width="150"
            align="center"
          >
          </el-table-column>
          <el-table-column
            property="kindergartenAddress"
            label="幼儿园地址"
            width="150"
            align="center"
          >
          </el-table-column>
          <el-table-column
            label="幼儿园所在组织"
            align="center"
            prop="organizeName"
            width="150"
          >
          </el-table-column>
          <el-table-column
            property="contacts"
            label="联系人"
            width="150"
            align="center"
          >
          </el-table-column>
          <el-table-column
            property="phone"
            label="联系电话"
            width="150"
            align="center"
          >
          </el-table-column>
        </el-table>

        <pagination
          v-show="total2 > 0"
          :total="total2"
          :page.sync="queryParams2.pageNum"
          :limit.sync="queryParams2.pageSize"
          @pagination="getList"
        />
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import {
  listClassinfo,
  getClassinfo,
  delClassinfo,
  addClassinfo,
  updateClassinfo,
} from "@/api/system/classinfo";
import {
  listKindergartenInfo,
  tKindergartenInfo,
} from "@/api/system/kindergartenInfo";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "Classinfo",
  data() {
    // 验证手机号的规则
    var checkMobile = (rule, value, cb) => {
      // 验证手机号的正则表达式
      const regMobile =
        /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/;

      if (regMobile.test(value)) {
        return cb();
      }

      cb(new Error("请输入合法的手机号"));
    };

    return {
      // 遮罩层
      loading: true,
      innerVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      // 班级信息表格数据
      classinfoList: [],
      ClassType: "class_type",
      kindergartenInfoList: [],
      dictValue1: "",
      dictValue2: "",
      dictValue3: "",
      dictValue4: "",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      kindergartenName: "请选择所在幼儿园",
      kindergartenid: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        kindergartenId: null,
        className: null,
        classType: null,
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        kindergartenId: null,
        className: null,
        classType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        kindergartenId: [{ required: true, message: "请选择所属幼儿园" }],
        className: [
          { required: true, message: "请输入班级名称", trigger: "blur" },
        ],
        classType: [{ required: true, message: "请选择幼儿园类型" }],
        classTeacher: [
          { required: true, message: "请输入班主任姓名", trigger: "blur" },
        ],
        phone: [
          {
            required: true,
            validator: checkMobile,
            trigger: "blur",
            message: "请输入正确的手机号",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    kindergartenNameclick(row) {
      this.kindergartenName = row.kindergartenName;
      this.kindergartenid2 = row.id;
      this.form.kindergartenId = row.id;
      this.innerVisible = false;
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        kindergartenId: node.id,
        label: node.kindergartenName,
        children: node.children,
      };
    },
    //信息表ID

    /** 查询班级信息列表 */

    getList() {
      this.loading = true;
      listKindergartenInfo(this.queryParams2).then((response) => {
        this.kindergartenInfoList = response.rows;
        this.total2 = response.total;
      });
      listClassinfo(this.queryParams).then((response) => {
        this.classinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      tKindergartenInfo(this.ClassType).then((res) => {
        this.dictValue1 = res.data[0].dictValue;
        this.dictValue2 = res.data[1].dictValue;
        this.dictValue3 = res.data[2].dictValue;
        this.dictValue4 = res.data[3].dictValue;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.kindergartenName = "请选择所在幼儿园";
      this.kindergartenid2 = "";
    },
    // 表单重置
    reset() {
      this.form = {
        kindergartenId: null,
        className: null,
        classType: null,
        classTeacher: null,
        phone: undefined,
        kindergartenName: null,
      };

      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery2() {
      this.queryParams2.pageNum = 1;
      listKindergartenInfo(this.queryParams2).then((response) => {
        this.kindergartenInfoList = response.rows;
        this.total2 = response.total;
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      listClassinfo(this.queryParams).then((response) => {
        this.classinfoList = response.rows;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        kindergartenId: null,
        className: null,
        classType: null,
      };
      this.queryParams2 = {
        pageNum: 1,
        pageSize: 10,
        kindergartenId: null,
        className: null,
        classType: null,
      };
      this.handleQuery();
      this.handleQuery2();
    },
    // 多选框选中数据
    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.kindergartenid2 = "";
      this.title = "添加班级信息";
      this.kindergartenName = "请选择所在幼儿园";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.kindergartenid2 = row.kindergartenId;
      this.reset();
      const id = row.id || this.ids;
      getClassinfo(id).then((response) => {
        response.data.phone = Number(response.data.phone);
        this.form = response.data;
        this.kindergartenName = this.form.kindergartenName;
        this.open = true;
        this.title = "修改班级信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateClassinfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClassinfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      this.kindergartenid2 = "";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除数据？")
        .then(function () {
          return delClassinfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    // handleExport() {
    //   this.download(
    //     "system/classinfo/export",
    //     {
    //       ...this.queryParams,
    //     },
    //     `classinfo_${new Date().getTime()}.xlsx`
    //   );
    // },
  },
};
</script>
