<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="kindergartenName">
        <el-input
          v-model="queryParams.kindergartenName"
          placeholder="请输入幼儿园名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="幼儿园地址" prop="kindergartenAddress">
        <el-input
          v-model="queryParams.kindergartenAddress"
          placeholder="请输入幼儿园地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="幼儿园类型" prop="kindergartenType">
        <treeselect
          :normalizer="normalizer"
          :options="types"
          placeholder="请选择幼儿园类型"
          v-model="queryParams.kindergartenType"
          style="width: 208px"
        />
      </el-form-item>

      <el-form-item label="联系人" prop="contacts">
        <el-input
          v-model="queryParams.contacts"
          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 label="组织名称" prop="parentId">
        <treeselect
          :normalizer="normalizer2"
          :options="infolist"
          placeholder="请选择所属组织"
          v-model="queryParams.organizeId"
          style="width: 208px"
        />
      </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:kindergartenInfo: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:kindergartenInfo: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:kindergartenInfo: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:kindergartenInfo:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="kindergartenInfoList"
      @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="kindergartenAddress"
      />
      <el-table-column label="幼儿园类型" align="center" prop="kindergartenType"
        ><template slot-scope="scope">{{
          scope.row.kindergartenType == type ? "公办" : "私有"
        }}</template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="contacts" />
      <el-table-column label="联系电话" align="center" prop="phone" />
      <el-table-column label="组织名称" align="center" prop="organizeName" />

      <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:kindergartenInfo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:kindergartenInfo: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="organizeId">
          <treeselect
            :normalizer="normalizer2"
            :options="infolist"
            placeholder="请选择所属组织"
            v-model="form.organizeId"
          />
        </el-form-item>
        <el-form-item label="幼儿园名称" prop="kindergartenName">
          <el-input
            v-model="form.kindergartenName"
            placeholder="请输入幼儿园名称"
          />
        </el-form-item>
        <el-form-item label="幼儿园地址" prop="kindergartenAddress">
          <el-input
            v-model="form.kindergartenAddress"
            placeholder="请输入幼儿园地址"
          />
        </el-form-item>
        <el-form-item label="幼儿园类型" prop="kindergartenType">
          <el-radio-group v-model="form.kindergartenType">
            <el-radio :label="type">公办</el-radio>
            <el-radio :label="type2">私有</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="form.phone"
            maxlength="11"
            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>
  </div>
</template>

<script>
import {
  tKindergartenInfo,
  listKindergartenInfo,
  getKindergartenInfo,
  delKindergartenInfo,
  addKindergartenInfo,
  updateKindergartenInfo,
} from "@/api/system/kindergartenInfo";
import { list } from "@/api/system/info.js";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "KindergartenInfo",
  components: { Treeselect },
  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,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 幼儿园信息管理表格数据
      kindergartenInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      kindergartenType: "kindergarten_type",
      type: "",
      types: [
        {
          id: 1,
          label: "公办",
          children: [],
        },
        {
          id: 2,
          label: "私有",
          children: [],
        },
      ],
      type2: "",
      // 查询组织参数
      infolist: [],
      infolist2: [],
      info: {
        parentId: undefined,
        areaId: undefined,
        organizeName: "",
        organizeAddress: "",
        phone: "",
        contacts: "",
        id: undefined,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizeId: null,
        kindergartenName: null,
        kindergartenAddress: null,
        kindergartenType: null,
        phone: null,
        contacts: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          {
            required: true,
            validator: checkMobile,
            trigger: "blur",
            message: "请输入正确的手机号",
          },
        ],
        kindergartenType: [{ required: true, message: "请选择幼儿园类型" }],
        kindergartenAddress: [
          { required: true, message: "请输入幼儿园地址", trigger: "blur" },
        ],
        kindergartenName: [
          { required: true, message: "请选择幼儿园名称", trigger: "blur" },
        ],
        organizeId: [
          { required: true, message: "请选择所属组织", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        kindergartenType: node.id,
        label: node.label,
        children: node.children,
      };
    },
    normalizer2(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        organizeId: node.id,
        label: node.organizeName,
        children: node.children,
      };
    },
    /** 查询幼儿园信息管理列表 */
    getList() {
      this.loading = true;
      list(this.info).then((response) => {
        this.infolist = this.handleTree(response.data);
        this.infolist2 = response.data;
        this.loading = false;
      });
      listKindergartenInfo(this.queryParams).then((response) => {
        this.kindergartenInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      tKindergartenInfo(this.kindergartenType).then((res) => {
        this.type = res.data[0].dictValue;
        this.type2 = res.data[1].dictValue;
        this.types[0].id = res.data[0].dictValue;
        this.types[1].id = res.data[1].dictValue;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        organizeId: null,
        kindergartenName: null,
        kindergartenAddress: null,
        kindergartenType: undefined,
        phone: "",
        contacts: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      listKindergartenInfo(this.queryParams).then((response) => {
        this.kindergartenInfoList = response.rows;
        this.total = response.total;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        organizeId: null,
        kindergartenName: null,
        kindergartenAddress: null,
        kindergartenType: null,
        phone: null,
        contacts: null,
      }),
        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 id = row.id || this.ids;
      getKindergartenInfo(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改幼儿园信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateKindergartenInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKindergartenInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delKindergartenInfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/kindergartenInfo/export",
        {
          ...this.queryParams,
        },
        `kindergartenInfo_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
