<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="itemCategoryId"
        label-width="100px"
      >
        <treeselect
          :normalizer="normalizer"
          :options="itemCategoryList"
          placeholder="请选择所属项目"
          v-model="queryParams.itemCategoryId"
          style="width: 208px"
        />
      </el-form-item>
      <el-form-item label="项目名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入测试项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sorting">
        <el-input
          v-model="queryParams.sorting"
          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:testItems: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:testItems: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:testItems: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:testItems:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="testItemsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="项目所属分类"
        align="center"
        prop="categoryName"
      />
      <!-- <el-table-column label="字典类型" align="center" prop="resultDictType">
        <template slot-scope="scope">
          {{
            scope.row.resultDictType == ResultType1.dictCode
              ? ResultType1.dictLabel
              : ""
          }}{{
            scope.row.resultDictType == ResultType2.dictCode
              ? ResultType2.dictLabel
              : ""
          }}</template
        >
      </el-table-column> -->
      <el-table-column label="项目名称" align="center" prop="itemName" />
      <el-table-column label="项目编号" align="center" prop="itemCode" />
      <el-table-column label="项目内容" align="center" prop="itemContent" />
      <!-- <el-table-column label="项目概述" align="center" prop="remark" /> -->
      <el-table-column label="项目度量单位" align="center" prop="measureUnit" />
      <el-table-column
        label="大班标准最高得分"
        align="center"
        prop="largeStandardScore"
      />
      <el-table-column
        label="中班标准最高得分"
        align="center"
        prop="middleStandardScore"
      />
      <el-table-column
        label="小班标准最高得分"
        align="center"
        prop="smallStandardScore"
      />
      <el-table-column
        label="统一标准最高得分"
        align="center"
        prop="unifiedStandardScore"
      />
      <el-table-column label="排序" align="center" prop="sorting" />
      <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:testItems:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:testItems: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="1050px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="160px"
        :inline="true"
      >
        <div style="border-bottom: 1px solid #f1f1f1; margin-bottom: 20px">
          <el-form-item label="所属分类" prop="itemCategoryId">
            <treeselect
              :normalizer="normalizer"
              :options="itemCategoryList"
              placeholder="请选择所属项目分类"
              v-model="form.itemCategoryId"
              style="width: 300px"
            />
          </el-form-item>
        </div>
        <div style="border-bottom: 1px solid #f1f1f1; margin-bottom: 20px">
          <el-form-item label="项目名称" prop="itemName">
            <el-input
              style="width: 300px"
              v-model="form.itemName"
              placeholder="请输入项目名称"
              maxlength="40"
            />
          </el-form-item>
          <el-form-item label="项目编号" prop="itemCode">
            <el-input
              v-model="form.itemCode"
              maxlength="20"
              placeholder="请输入测试项目编号"
              style="width: 300px"
            />
          </el-form-item>
          <el-form-item label="测试项目成绩类型" prop="resultDictType">
            <el-select
              v-model="form.resultDictType"
              clearable
              placeholder="请选择字典类型"
              style="width: 300px"
            >
              <el-option
                v-for="item in itemResultTypelist"
                :key="item.value"
                :label="item.dictLabel"
                :value="item.dictCode"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="项目概述" prop="remark">
            <el-input
              v-model="form.remark"
              placeholder="请输入项目概述"
              style="width: 300px"
              maxlength="200"
            />
          </el-form-item>
          <el-form-item label="项目内容" prop="itemContent">
            <el-input
              type="textarea"
              :autosize="{ minRows: 6, maxRows: 8 }"
              v-model.number="form.itemContent"
              placeholder="项目内容"
              maxlength="200"
              style="width: 500px"
            />

            <!-- <editor
            v-model="form.itemContent"
            :min-height="190"
            style="margin-left: 80px"
            maxlength="200"
          /> -->
          </el-form-item>
        </div>

        <el-form-item label="排序" prop="sorting">
          <el-input
            v-model="form.sorting"
            placeholder="请输入排序"
            maxlength="10"
            style="width: 300px"
          />
        </el-form-item>

        <el-form-item label="度量单位" prop="measureUnit">
          <el-select
            v-model="form.measureUnit"
            clearable
            placeholder="请选择度量单位"
            style="width: 300px"
          >
            <el-option
              v-for="item in measureUnitTypelist"
              :key="item.value"
              :label="item.dictValue"
              :value="item.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最低得分" prop="minScore">
          <el-input
            v-model="form.minScore"
            type="text"
            maxlength="10"
            style="width: 300px"
            placeholder="请输入最低得分"
          />
        </el-form-item>
        <el-form-item label="最高得分" prop="maxScore">
          <el-input
            v-model="form.maxScore"
            type="text"
            maxlength="10"
            placeholder="请输入最高得分"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item label="标准最高得分类型" prop="standardType">
          <el-radio-group v-model="form.standardType" style="width: 300px">
            <el-radio :label="ty">统一指标</el-radio>
            <el-radio :label="fty">非统一指标</el-radio>
          </el-radio-group>
          <!-- <el-select
            v-model="form.standardType"
            clearable
            placeholder="请选择指标类型"
            style="width: 300px"
          >
            <el-option
              v-for="item in standardTypelist"
              :key="item.value"
              :label="item.dictLabel"
              :value="item.dictValue"
            >
            </el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item
          label="大班标准最高得分"
          prop="largeStandardScore"
          v-if="form.standardType == fty"
        >
          <el-input
            v-model="form.largeStandardScore"
            maxlength="10"
            type="text"
            placeholder="请输入大班标准最高得分"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item
          label="中班标准最高得分"
          prop="middleStandardScore"
          v-if="form.standardType == fty"
        >
          <el-input
            v-model="form.middleStandardScore"
            type="text"
            maxlength="10"
            placeholder="请输入中班标准最高得分"
            style="width: 300px"
          />
        </el-form-item>
        <el-form-item
          label="小班标准最高得分"
          prop="smallStandardScore"
          v-if="form.standardType == fty"
        >
          <el-input
            v-model="form.smallStandardScore"
            type="text"
            style="width: 300px"
            maxlength="10"
            placeholder="请输入小班标准最高得分"
          />
        </el-form-item>
        <el-form-item
          label="统一标准最高得分"
          prop="unifiedStandardScore"
          v-if="form.standardType == ty"
        >
          <el-input
            style="width: 300px"
            v-model="form.unifiedStandardScore"
            type="text"
            maxlength="10"
            placeholder="请输入统一标准最高得分"
          />
        </el-form-item>

        <el-form-item label="级别描述" prop="levelDescription">
          <el-input
            style="width: 300px"
            v-model="form.levelDescription"
            placeholder="请输入级别描述"
            maxlength="50"
          />
        </el-form-item>
        <el-form-item label="成绩级别" prop="itemLevelList">
          <div
            v-for="(item, index) in form.itemLevelList"
            :key="item.id"
            style="margin-bottom: 10px"
          >
            <el-input
              v-model="item.levelName"
              placeholder="级别名"
              maxlength="10"
              style="width: 75px; margin-right: 10px"
            />
            <el-input
              v-model.number="item.levelMinScore"
              placeholder="最低分"
              maxlength="10"
              style="width: 75px; margin-right: 5px"
            /><el-input
              v-model.number="item.levelMaxScore"
              placeholder="最高分"
              maxlength="10"
              style="width: 75px; margin-right: 5px"
            />

            <el-select
              v-model="item.standardFlag"
              clearable
              placeholder="请选择是否达标"
              style="width: 145px; margin-right: 8px"
            >
              <el-option
                v-for="item in list"
                :key="item.id"
                :label="item.label"
                :value="item.id"
              >
              </el-option>
            </el-select>
            <el-button
              type="info"
              circle
              plain
              icon="el-icon-circle-plus-outline"
              @click="addInput"
              v-if="index == 0"
            ></el-button>

            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              plain
              @click="delInput(index)"
              v-if="index != 0"
            ></el-button>
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer" style="margin-right: 80px">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTestItems,
  getTestItems,
  delTestItems,
  addTestItems,
  updateTestItems,
} from "@/api/system/testItems";
import { tKindergartenInfo } from "@/api/system/kindergartenInfo";
import { listItemCategory } from "@/api/system/itemCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  components: { Treeselect },
  name: "TestItems",
  data() {
    var roleMoeny = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.middleStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.middleStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny1 = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(0, str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.middleStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.unifiedStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny2 = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(0, str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.middleStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.middleStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny3 = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(0, str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.smallStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.smallStandardScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny4 = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(0, str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.minScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.minScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny5 = (rule, value, callback) => {
      var str = value + "";
      var str_ = "";
      var str2 = "";
      str_ = str.substr(str.indexOf(".")).length;
      if (str.includes(".")) {
        str2 = str.substr(0, str.indexOf(".")).length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.maxScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        console.log(str2);
        if (Number.isNaN(Number.parseFloat(value))) {
          callback(new Error("请输入数字"));
        } else if (this.form.maxScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    let itemLevelList = (rule, value, callback) => {
      for (let i = 0; i < value.length; i++) {
        console.log(value[i].levelMinScore > value[i].levelMaxScore);
        if (value[i].levelMinScore > value[i].levelMaxScore) {
          callback(new Error("最高分必须大于最低分,请重新填写！"));
        } else if (value[i].levelName == "") {
          callback();
        } else if (value[i].levelMinScore == "") {
          callback();
        } else if (value[i].levelMaxScore == "") {
          callback();
        } else {
          callback();
        }
      }
      // value.for((e) => {
      //   console.log(e.levelMinScore, e.levelMaxScore);
      //   console.log(e.levelMinScore > e.levelMaxScore);
      //   if (e.levelMinScore > e.levelMaxScore) {
      //     callback(new Error("最高分必须大于最低分,请重新填写！"));
      //   } else if (e.levelName == "") {
      //     callback();
      //   } else if (e.levelMinScore == "") {
      //     callback();
      //   } else if (e.levelMaxScore == "") {
      //     callback();
      //   } else {
      //     callback();
      //   }
      // });
    };

    let minScore = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (this.form.maxScore <= value) {
        callback();
      }
      callback();
    };
    let maxScore = (rule, value, callback) => {
      if (!value) {
        callback();
      } else if (this.form.minScore >= value) {
        callback(new Error("最高得分必须大于最低得分,请重新填写最高分！"));
      } else {
        callback();
      }
    };

    const validateVin = (rule, value, callback) => {
      const containAlpha = /[a-zA-Z]/; //含有字母

      const containLowerCase = /[a-z]/; //匹配小写字母
      value = value.slice(0, 1);
      console.log(value);

      if (!value) {
        callback(new Error("不能为空"));
      } else if (!containAlpha.test(value)) {
        callback(new Error("第一个必须为字母"));
      } else if (containLowerCase.test(value)) {
        callback(new Error("英文必须大写"));
      } else {
        callback();
      }
    };

    return {
      str: [],
      //项目测试类型
      itemResultType: "item_result_type",
      //度量单位
      measureUnitType: "measure_unit_type",
      //统一与非统一
      standardType: "standard_type",
      ty: "unified_standard",
      fty: "disunity_standard",
      standardTypelist: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //度量单位表
      measureUnitTypelist: [],
      //shifouhege
      list: [
        {
          label: "是",
          id: 1,
        },
        {
          label: "否",
          id: 0,
        },
      ],
      //字典类型
      itemResultTypelist: [],
      ResultType1: "",
      ResultType2: "",
      // 测试项目表格数据
      testItemsList: [],
      //项目分类单
      itemCategoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemCategoryId: null,
        itemName: null,
        itemCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        unifiedStandardScore: [
          {
            required: true,
            validator: roleMoeny1,
            message: "",
            trigger: "blur",
          },
        ],
        largeStandardScore: [
          {
            required: true,
            validator: roleMoeny,
            message: "",
            trigger: "blur",
          },
        ],
        middleStandardScore: [
          {
            required: true,
            validator: roleMoeny2,
            message: "",
            trigger: "blur",
          },
        ],
        smallStandardScore: [
          {
            required: true,
            validator: roleMoeny3,
            message: "",
            trigger: "blur",
          },
        ],
        itemLevelList: [
          {
            required: true,
            validator: itemLevelList,
            trigger: "blur",
          },
        ],
        minScore: [
          {
            required: true,
            validator: roleMoeny4,
            message: "",
            trigger: "blur",
          },
        ],
        maxScore: [
          {
            required: true,
            validator: roleMoeny5,
            message: "",
            trigger: "blur",
          },
        ],
        itemContent: [
          { required: true, message: "项目内容不能为空", trigger: "blur" },
        ],
        itemCategoryId: [
          { required: true, message: "请选择项目所属分类", trigger: "blur" },
        ],
        itemCode: [
          { required: true, message: "项目编号不能为空", trigger: "blur" },
          {
            required: true,
            validator: validateVin,
            trigger: "blur",
          },
        ],
        itemName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
        ],
        sorting: [{ required: true, message: "排序不能为空", trigger: "blur" }],
        measureUnit: [
          {
            required: true,
            message: "度量单位不能为空",
            trigger: "change",
          },
        ],
        resultDictType: [
          {
            required: true,
            message: "所属字典类型不能为空",
            trigger: "change",
          },
        ],
        standardFlag: [
          {
            required: true,
            message: "请选择是否达标",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // checkInput() {
    //   this.form.minScore = this.dealInputVal(this.form.minScore);
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },
    // checkInput1() {
    //   this.form.maxScore = this.dealInputVal(this.form.maxScore);
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },
    // checkInput2() {
    //   this.form.largeStandardScore = this.dealInputVal(
    //     this.form.largeStandardScore
    //   );
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.splilevelMinScoredatat(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },
    // checkInput3() {
    //   this.form.middleStandardScore = this.dealInputVal(
    //     this.form.middleStandardScore
    //   );
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },
    // checkInput4() {
    //   this.form.smallStandardScore = this.dealInputVal(
    //     this.form.smallStandardScore
    //   );
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },

    // checkInput5() {
    //   this.form.unifiedStandardScore = this.dealInputVal(
    //     this.form.unifiedStandardScore
    //   );
    // },
    // dealInputVal(value) {
    //   value = value.replace(/^0*(0\.|[1-9])/, "$1");
    //   value = value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符
    //   value = value.replace(/^\./g, ""); //验证第一个字符是数字而不是字符
    //   value = value.replace(/\.{1,}/g, "."); //只保留第一个.清除多余的
    //   value = value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
    //   value = value.replace(/^(\-)*(\d*)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
    //   value =
    //     value.indexOf(".") > 0
    //       ? value.split(".")[0].substring(0, 10) + "." + value.split(".")[1]
    //       : value.substring(0, 10);
    //   return value;
    // },

    delInput(index) {
      this.form.itemLevelList.splice(index, 1);
      console.log(this.form.itemLevelList);
    },
    addInput() {
      console.log(this.form);
      this.form.itemLevelList.push({
        levelName: "",
        levelMinScore: "",
        levelMaxScore: "",
        standardFlag: "",
      });
    },
    /** 查询测试项目列表 */
    getList() {
      this.loading = true;
      listItemCategory(this.queryParams).then((response) => {
        this.itemCategoryList = this.handleTree(response.data);
        // console.log(this.itemCategoryList);
      });
      tKindergartenInfo(this.itemResultType).then((res) => {
        // console.log(res);
        this.itemResultTypelist = res.data;
        console.log(this.itemResultTypelist);

        this.ResultType1 = this.itemResultTypelist[0];
        this.ResultType2 = this.itemResultTypelist[1];
        // console.log(this.ResultType1);
      });
      tKindergartenInfo(this.measureUnitType).then((res) => {
        // console.log(res);
        this.measureUnitTypelist = res.data;
      });
      tKindergartenInfo(this.standardType).then((res) => {
        this.standardTypelist = res.data;
        console.log(this.standardTypelist);
      });
      listTestItems(this.queryParams).then((response) => {
        this.testItemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemCategoryId: null,
        resultDictType: null,
        itemName: "",
        itemCode: null,
        itemContent: "",
        measureUnit: "",
        standardType: null,
        largeStandardScore: "",
        middleStandardScore: "",
        smallStandardScore: "",
        maxScore: "",
        levelDescription: "",
        minScore: "",
        itemLevelList: [
          {
            levelName: "",
            levelMinScore: "",
            levelMaxScore: "",
            standardFlag: "",
          },
        ],
        sorting: null,
        remark: null,
        unifiedStandardScore: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      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 id = row.id || this.ids;
      getTestItems(id).then((response) => {
        if (response.data.itemLevelList == null) {
          response.data.itemLevelList = [
            { levelName: "", levelMinScore: "", levelMaxScore: "" },
          ];
        }
        // response.data.unifiedStandardScore = Number(
        //   response.data.unifiedStandardScore
        // );
        this.form = response.data;
        // console.log(typeof this.form.unifiedStandardScore);
        this.open = true;
        this.title = "修改测试项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form);
      // this.form.unifiedStandardScore = Number(this.form.unifiedStandardScore);
      // console.log(typeof this.form.unifiedStandardScore);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTestItems(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTestItems(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 delTestItems(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        itemCategoryId: node.id,
        label: node.categoryName,
        children: node.children,
      };
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/testItems/export",
        {
          ...this.queryParams,
        },
        `testItems_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
