<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="areaId">
        <treeselect
          :normalizer="normalizer"
          :options="areaList"
          placeholder="请选择所属区域"
          v-model="queryParams.areaId"
          style="width: 228px"
        />
      </el-form-item>
      <el-form-item label="所属项目" prop="itemId">
        <treeselect
          :normalizer="normalizer3"
          :options="testItemsList2"
          placeholder="请选择所属项目"
          v-model="queryParams.itemId"
          style="width: 228px"
        />
      </el-form-item>
      <el-form-item label="平均得分" prop="itemAvgScore">
        <el-input
          v-model="queryParams.itemAvgScore"
          placeholder="请输入平均得分"
          clearable
          @keyup.enter.native="handleQuery2"
        />
      </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-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:testItemCityScore: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:testItemCityScore: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:testItemCityScore: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:testItemCityScore:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="testItemCityScoreList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属区域" align="center" prop="name" />
      <el-table-column label="所属项目" align="center" prop="itemName" />
      <el-table-column label="平均得分" align="center" prop="itemAvgScore">
        <template slot-scope="scope">
          {{ scope.row.dictValue == "number" ? scope.row.itemAvgScore : "" }}
          {{ scope.row.dictValue == "text" ? scope.row.itemAvgText : "" }}
        </template>
      </el-table-column>
      <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:testItemCityScore:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:testItemCityScore: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
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属区域" prop="areaId">
          <treeselect
            :normalizer="normalizer"
            :options="areaList"
            placeholder="请选择所属区域"
            v-model="form.areaId"
          />
        </el-form-item>
        <el-form-item label="所属项目" prop="itemId">
          <el-button
            @click="innerVisible = true"
            style="width: 380px; text-align: left; height: 36px; color: #c0c4cc"
            v-if="xmname == '请选择所属项目'"
          >
            {{ xmname }}
          </el-button>
          <el-button
            @click="innerVisible = true"
            style="width: 380px; text-align: left; height: 36px"
            v-else
          >
            {{ xmname }}
          </el-button>
        </el-form-item>

        <el-form-item
          label="平均得分"
          :required="true"
          v-if="form.resultDictType == ''"
        >
          <el-input placeholder="请输入平均得分" />
        </el-form-item>

        <el-form-item
          label="平均得分"
          v-else-if="form.dictValue == ResultType1.dictValue"
          key="itemAvgScore"
          prop="itemAvgScore"
        >
          <el-input
            v-model="form.itemAvgScore"
             @input="NumberCheck()"
            placeholder="请输入平均得分"
          />
        </el-form-item>

        <el-form-item
          label="平均得分"
          v-else-if="form.dictValue == ResultType2.dictValue"
          key="itemAvgText"
          prop="itemAvgText"
        >
          <el-input v-model="form.itemAvgText" placeholder="请输入平均得分" />
        </el-form-item>
        <el-form-item
          label="平均得分"
          v-else-if="form.dictValue == 'text'"
          key="itemAvgText"
          prop="itemAvgText"
        >
          <el-input v-model="form.itemAvgText" 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>

    <!-- 修改全市平均成绩对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open2"
      width="500px"
      append-to-body
      :before-close="cancel"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属区域" prop="areaId">
          <treeselect
            :normalizer="normalizer"
            :options="areaList"
            placeholder="请选择所属区域"
            v-model="form.areaId"
          />
        </el-form-item>

        <el-form-item label="所属项目" prop="itemId">
          <el-button
            @click="innerVisible = true"
            style="width: 380px; text-align: left; height: 36px; color: #c0c4cc"
            v-if="xmname == '请选择所属项目'"
          >
            {{ xmname }}
          </el-button>
          <el-button
            @click="innerVisible = true"
            style="width: 380px; text-align: left; height: 36px"
            v-else
          >
            {{ xmname }}
          </el-button>
        </el-form-item>

        <el-form-item
          label="平均得分"
          prop="itemAvgScore"
          v-if="form.dictValue == 'number'"
        >
          <el-input
            v-model="form.itemAvgScore"
             @input="NumberCheck()"
            placeholder="请输入平均得分"
          />
        </el-form-item>
        <el-form-item
          label="平均得分"
          prop="itemAvgText"
          v-if="form.dictValue == 'text'"
        >
          <el-input v-model="form.itemAvgText" 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>

    <!-- //选择所属项目 -->
    <el-dialog
      width="1150px"
      title="选择所属项目"
      :visible.sync="innerVisible"
      append-to-body
      :before-close="cancel2"
    >
      <el-form
        :model="queryParams2"
        ref="queryForm"
        size="small"
        :inline="true"
        label-width="70px"
      >
        <el-form-item
          label="所属项目分类"
          prop="itemCategoryId"
          label-width="100px"
        >
          <treeselect
            :normalizer="normalizer2"
            :options="itemCategoryList"
            placeholder="请选择所属项目分类"
            v-model="queryParams2.itemCategoryId"
            style="width: 180px"
          />
        </el-form-item>

        <el-form-item label="项目名称" prop="itemName">
          <el-input
            style="width: 180px"
            v-model="queryParams2.itemName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="项目编号" prop="itemCode">
          <el-input
            style="width: 180px"
            v-model="queryParams2.itemCode"
            placeholder="请输入测试项目编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="排序" prop="sorting">
          <el-input
            style="width: 180px"
            v-model="queryParams2.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-table
        v-loading="loading"
        :data="testItemsList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column width="60" align="center" label="请选择">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 15px; height: 15px"
              circle
              v-if="scope.row.id == xmid"
              @click="xmclick2(scope.row)"
            ></el-button>

            <el-button
              v-else
              style="width: 15px; height: 15px"
              circle
              @click="xmclick(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="项目所属分类"
          align="center"
          prop="categoryName"
          width="80px"
        />
        <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>

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

<script>
import {
  listTestItemCityScore,
  getTestItemCityScore,
  delTestItemCityScore,
  addTestItemCityScore,
  updateTestItemCityScore,
} from "@/api/system/testItemCityScore";
import { tKindergartenInfo } from "@/api/system/kindergartenInfo";
import { listItemCategory } from "@/api/system/itemCategory";
import { listTestItems } from "@/api/system/testItems";
import { listArea } from "@/api/system/area.js";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
    name: "TestItemCityScore",
     components: { Treeselect },
  data() {
    var roleMoeny = (rule, value, callback) => {
      if (Number.isNaN(Number.parseFloat(value))) {
        callback(new Error("请输入数字"));
      } else  if (!this.form.itemAvgScore) {
        callback(new Error("不能为空"));
      } else if (this.form.itemAvgScore * 1 > 99999999) {
        callback(new Error("最大99999999")); // 限制最大额度
      } else {
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      innerVisible: false,
      // 选中数组
      ids: [],
      //项目分类单
      itemCategoryList: [],
      //字典类型
      itemResultTypelist: [],
      ResultType1: "",
      ResultType2: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //项目测试类型
      itemResultType: "item_result_type",
      // 总条数
      total: 0,
      // 全市平均成绩表格数据
      testItemCityScoreList: [],
      //区域列表
      areaList: [],
      //name
      name: [],
      //项目列表
      testItemsList: [],
      testItemsList2: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      //xmname
      xmname: "请选择所属项目",
      xmid: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        itemId: null,
        itemAvgScore: null,
      },
      // 查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        itemCategoryId: null,
        itemName: null,
        itemCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // itemAvgScore: [
          // {
          //   required: true,
          //   type: "number",
          //   message: "必须填入数字",
          //   trigger: "blur",
          // },
            itemAvgScore: [
          { required: true, validator: roleMoeny, message: "" },
        ],
        itemAvgText: [
          { required: true, message: "请输入全市平均成绩", trigger: "blur" },
        ],
        areaId: [
          { required: true, message: "请选择所属区域", trigger: "blur" },
        ],
        itemId: [
          { required: true, message: "请选择所属项目", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
        NumberCheck(num) {
  let str = this.form.itemAvgScore;
  //限制只能输入一个小数点
  if (str.indexOf(".") != -1) {
    var str_ = str.substr(str.indexOf(".") + 1);
    if (str_.indexOf(".") != -1) {
      str = str.substr(0, str.indexOf(".") + str_.indexOf(".") + 1);
    }
  }
  //正则替换，保留数字和小数点
  str = str.replace(/[^\d^\.]+/g,'')
  this.form.itemAvgScore=str
},

    normalizer3(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        itemId: node.id,
        label: node.itemName,
        children: node.children,
      };
    },
    normalizer2(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        itemCategoryId: node.id,
        label: node.categoryName,
        children: node.children,
      };
    },
    xmclick2(item) {
      this.xmname = "请选择所属项目";
      this.xmid = "";
      this.form.itemId = "";
    },
    xmclick(item) {
      console.log(item);
      this.xmname = item.itemName;
      this.form.itemId = item.id;
      this.xmid = item.id;
      console.log(this.ResultType1, this.ResultType2);
      if (item.resultDictType == this.ResultType1.dictCode) {
        this.form.resultDictType = this.ResultType1.dictValue;
        this.form.dictValue = this.ResultType1.dictValue;
      }
      if (item.resultDictType == this.ResultType2.dictCode) {
        this.form.resultDictType = this.ResultType2.dictValue;
        this.form.dictValue = this.ResultType2.dictValue;
      }
      console.log(this.form);
      this.innerVisible = false;
    },
    /** 查询全市平均成绩列表 */
    getList() {
      this.loading = true;
      listArea(this.queryParams2).then((response) => {
        // this.areaList = response.data.forEach((e) => {
        //   e.code = Number(e.code);
        // });
        this.areaList = this.handleTree(response.data);
        // console.log(this.areaList);
      });
      listTestItemCityScore(this.queryParams).then((response) => {
        this.testItemCityScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.testItemCityScoreList);
      });
      listTestItems(this.queryParams2).then((response) => {
        this.testItemsList = response.rows;
        this.testItemsList2 = this.handleTree(response.rows);
        // console.log(this.testItemsList);
      });
      listItemCategory(this.queryParams2).then((response) => {
        this.itemCategoryList = this.handleTree(response.data);
        // console.log(this.itemCategoryList);
      });
      tKindergartenInfo(this.itemResultType).then((res) => {
        // console.log(res);
        this.itemResultTypelist = res.data;
        this.ResultType1 = this.itemResultTypelist[0];
        this.ResultType2 = this.itemResultTypelist[1];
        // console.log(this.ResultType1);
      });
    },
    // 取消按钮
    cancel2() {
      this.innerVisible = false;
      this.queryParams2 = {
        pageNum: 1,
        pageSize: 10,
        itemCategoryId: null,
        itemName: null,
        itemCode: null,
      };
      this.getList();
    },
    cancel() {
      this.open = false;
      this.open2 = false;
      this.reset();
      this.xmname = "请选择所属项目";
      this.xmid = "";
      this.form.itemId = "";
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        areaId: null,
        itemId: null,
        itemAvgScore: null,
        resultDictType: "",
      };
    },
    /** 搜索按钮操作 */
    handleQuery2() {
      console.log(this.queryParams);
      listTestItemCityScore(this.queryParams).then((response) => {
        this.testItemCityScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(this.testItemCityScoreList);
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        areaId: null,
        itemId: null,
        itemAvgScore: null,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.ids = selection.map((item) => item.id);
      this.name = selection.map((item) => item.itemName);
      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;
      getTestItemCityScore(id).then((response) => {
        this.form = response.data;
        this.open2 = true;
        this.title = "修改全市平均成绩";
        this.xmname = this.form.itemName;
        this.xmid = this.form.itemId;
        console.log(this.form);
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.resultDictType == this.ResultType1.dictValue) {
        console.log(this.ResultType1.dictValue);
        this.form.itemAvgText = "";
      }
      if (this.form.resultDictType == this.ResultType2.dictValue) {
        console.log(this.ResultType2.dictValue);
        this.form.itemAvgScore = "";
      }
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTestItemCityScore(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            });
          } else {
            addTestItemCityScore(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.xmname = "请选择所属项目";
              this.xmid = "";
              this.form.itemId = "";
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row);
      const ids = row.id || this.ids;
      const name = row.itemName || this.name;
      this.$modal
        .confirm('是否确认删除项目名称为"' + name + '"的全市平均成绩？')
        .then(function () {
          return delTestItemCityScore(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        areaId: node.id,
        label: node.name,
        children: node.children,
      };
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/testItemCityScore/export",
        {
          ...this.queryParams,
        },
        `testItemCityScore_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
