<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="itemId">
        <!-- this.queryParams.itemName=null -->
        <el-input
          v-model="queryParams.itemName"
          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:testItemCountryScore: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:testItemCountryScore: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:testItemCountryScore: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:testItemCountryScore:export']"
          >导出</el-button
        >
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="testItemCountryScoreList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="所属项目" align="center" prop="itemName" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          {{ scope.row.sex == "MALE" ? "男" : "" }}
          {{ scope.row.sex == "FEMALE" ? "女" : "" }}
        </template>
      </el-table-column>
      <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:testItemCountryScore:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:testItemCountryScore: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="open2"
      width="550px"
      append-to-body
      :before-close="cancel2"
    >
      <el-form ref="form" :model="form" :rules="rulesxg" label-width="80px" style>
        <el-form-item label="所属项目" prop="itemId">
          <el-button
            type="info"
            plain
            disabled
            @click="innerVisible = true"
            v-if="xmname == '请选择所属项目'"
            style="
              width: 360px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              padding-left: -10px;
            "
          >{{ xmname }}</el-button>

          <el-button
            type="info"
            plain
            disabled
            @click="innerVisible = true"
            v-else
            style="
              width: 360px;
              text-align: left;
              height: 36px;
              padding-left: -10px;
            "
          >{{ xmname }}</el-button>
        </el-form-item>
        <el-form-item label="学生性别" prop="sex">
          <el-select v-model="form.sex" clearable disabled placeholder="请选择" style="width: 360px">
            <el-option
              v-for="item in sexs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平均得分" prop="itemAvgScore" v-if="form.dictValue == 'number'">
          <el-input
            v-model="form.itemAvgScore"
            style="width: 360px"
            placeholder="请输入平均得分"
            maxlength="10"
          />
        </el-form-item>
        <el-form-item label="平均得分" prop="itemAvgText" v-if="form.dictValue == 'text'">
          <el-input
            v-model="form.itemAvgText"
            placeholder="请输入平均得分"
            style="width: 360px"
            maxlength="100"
          />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加全国平均成绩对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="550px"
      append-to-body
      :before-close="cancel"
    >
      <el-form ref="form2" :model="form2" :rules="rules" label-width="80px" style>
        <el-form-item label="所属项目" prop="itemId">
          <el-button
            type
            @click="innerVisible = true"
            v-if="xmname == '请选择所属项目'"
            style="
              width: 360px;
              text-align: left;
              height: 36px;
              color: #c0c4cc;
              padding-left: -10px;
            "
          >{{ xmname }}</el-button>

          <el-button
            type
            @click="innerVisible = true"
            v-else
            style="
              width: 360px;
              text-align: left;
              height: 36px;
              padding-left: -10px;
            "
          >{{ xmname }}</el-button>
        </el-form-item>
        <el-form-item label="学生性别" prop="sex">
          <el-select v-model="form2.sex" clearable placeholder="请选择" style="width: 360px">
            <el-option
              v-for="item in sexs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              @click.native="sexclick(item)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="平均得分" prop="itemAvgScore1" v-if="form2.resultDictType == ''">
          <el-input v-model="value" placeholder="请输入平均得分" style="width: 360px" maxlength="10" />
        </el-form-item>

        <el-form-item label="平均得分" prop="itemAvgScore" v-else-if="form2.resultDictType == 'number'">
          <el-input
            v-model="form2.itemAvgScore"
            placeholder="请输入平均得分"
            style="width: 360px"
            maxlength="10"
          />
        </el-form-item>

        <el-form-item label="平均得分" prop="itemAvgText" v-else-if="form2.resultDictType == 'text'">
          <el-input
            v-model="form2.itemAvgText"
            placeholder="请输入平均得分"
            style="width: 360px"
            maxlength="100"
          />
        </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="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="itemName">
          <el-input
            v-model="queryParams2.itemName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="测试项目编号" prop="itemCode" label-width="90">
          <el-input
            v-model="queryParams2.itemCode"
            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="testItemslists" @cell-dblclick="xmclick">
        <el-table-column label="请选择" width="70" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              v-if="itemId2 == scope.row.id"
              style="width: 15px; height: 15px"
              circle
              @click="xmclick(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 property="itemName" label="项目名称" width="150" align="center"></el-table-column>
        <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 property="itemCode" label="测试项目编号" width="150" align="center"></el-table-column>
        <el-table-column property="itemContent" label="项目内容" width="150" align="center"></el-table-column>
        <el-table-column property="mesureUnit" label="项目度量单位" width="150" align="center"></el-table-column>
        <el-table-column property="largeStandardScore" label="大班标准最高得分" width="150" align="center"></el-table-column>
        <el-table-column property="middleStandardScore" label="中班标准最高得分" width="150" align="center"></el-table-column>
        <el-table-column property="smallStandardScore" label="小班标准最高得分" width="150" align="center"></el-table-column>
        <el-table-column
          property="unifiedStandardScore"
          label="统一标准最高得分"
          width="150"
          align="center"
        ></el-table-column>
        <el-table-column property="sorting" label="排序" width="150" align="center"></el-table-column>
        <el-table-column property="remark" label="项目概述" width="150" align="center"></el-table-column>
        <el-table-column property="createBy" label="创建人" width="150" align="center"></el-table-column>
        <el-table-column property="createTime" label="创建时间" width="150" align="center"></el-table-column>
        <el-table-column property="updateBy" label="修改人" width="150" align="center"></el-table-column>
        <el-table-column property="updateTime" 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>
  </div>
</template>

<script>
import {
  listTestItemCountryScore,
  getTestItemCountryScore,
  delTestItemCountryScore,
  addTestItemCountryScore,
  updateTestItemCountryScore,
  testItemslist,
  getItemCountryScoreInfo
} from "@/api/system/testItemCountryScore";
import { tKindergartenInfo } from "@/api/system/kindergartenInfo";

export default {
  name: "TestItemCountryScore",
  data() {
    var roleMoeny = (rule, value, callback) => {
      var str = value;
      var str_ = "";
      var str2 = "";
      if (str.includes(".")) {
        str_ = str.substr(str.indexOf(".")).length;
        str2 = str.substr(0, str.indexOf(".")).length;
        if (Number.isNaN(Number.parseFloat(str))) {
          callback(new Error("请输入数字"));
        } else if (!this.form2.itemAvgScore) {
          callback(new Error("不能为空"));
        } else if (this.form2.itemAvgScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (this.form2.itemAvgScore == "0") {
          callback(new Error("请输入成绩")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        if (Number.isNaN(Number.parseFloat(str))) {
          callback(new Error("请输入数字"));
        } else if (!this.form2.itemAvgScore) {
          callback(new Error("不能为空"));
        } else if (this.form2.itemAvgScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (this.form2.itemAvgScore == "0") {
          callback(new Error("请输入成绩")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };
    var roleMoeny2 = (rule, value, callback) => {
      var str = String(this.form.itemAvgScore);
      var str_ = "";
      var str2 = "";
      if (str.includes(".")) {
        str_ = str.substr(str.indexOf(".")).length;
        str2 = str.substr(0, str.indexOf(".")).length;
        if (Number.isNaN(Number.parseFloat(str))) {
          callback(new Error("请输入数字"));
        } else if (!this.form.itemAvgScore) {
          callback(new Error("不能为空"));
        } else if (this.form.itemAvgScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (this.form.itemAvgScore == "0") {
          callback(new Error("请输入成绩")); // 限制最大额度
        } else if (str_ > 3) {
          callback(new Error("小数点后两位")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      } else {
        str2 = str.length;
        if (Number.isNaN(Number.parseFloat(str))) {
          callback(new Error("请输入数字"));
        } else if (!this.form.itemAvgScore) {
          callback(new Error("不能为空"));
        } else if (this.form.itemAvgScore * 1 > 99999999) {
          callback(new Error("最大99999999")); // 限制最大额度
        } else if (this.form.itemAvgScore == "0") {
          callback(new Error("请输入成绩")); // 限制最大额度
        } else if (str2 > 3) {
          callback(new Error("整数为三位")); // 限制最大额度
        } else {
          callback();
        }
      }
    };

    return {
      value: "",
      //男女选择
      sexs: [
        {
          label: "男",
          value: "MALE"
        },
        {
          label: "女",
          value: "FEMALE"
        }
      ],
      //项目测试类型
      itemResultType: "item_result_type",
      //字典类型
      itemResultTypelist: [],
      ResultType1: "", //numberLeixing
      ResultType2: "", //textleixing
      // 遮罩层
      loading: true,
      innerVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //xiangmuname
      xmname: "请选择所属项目",
      // 总条数
      total: 0,
      total2: 0,
      //性别
      male: "MALE",
      female: "FEMALE",
      //xiangmu
      testItemslists: [],
      // 全国平均成绩表格数据
      testItemCountryScoreList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2: false,
      // 查询参数
      itemId2: "",
      itemId: "",
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
        sex: null
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        itemCategoryId: "",
        itemName: "",
        itemCode: ""
      },
      queryParams3: {
        itemId: null,
        sex: null
      },
      // 表单参数
      form: {},
      form2: {},

      // 表单校验
      rulesxg: {
        itemId: [
          { required: true, message: "请选择所属项目", trigger: "blur" }
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        itemAvgScore: [
          {
            required: true,
            validator: roleMoeny2,
            message: "",
            trigger: "blur"
          }
        ],
        itemAvgText: [
          { required: true, message: "请输入平均得分", trigger: "blur" }
        ]
      },
      rules: {
        itemId: [
          { required: true, message: "请选择所属项目", trigger: "blur" }
        ],
        itemAvgScore1: [
          {
            required: true,
            message: "请选择所属项目之后输入平均得分",
            trigger: "blur"
          }
        ],
        itemAvgScore: [
          {
            required: true,
            validator: roleMoeny,
            message: "",
            trigger: "blur"
          }
        ],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
        itemAvgText: [
          { required: true, message: "请输入平均得分", trigger: "blur" }
        ]
      },
      realVal: ""
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getItemCountryScore() {
      getItemCountryScoreInfo(this.queryParams3).then(res => {
        if (res.data) {
          this.form2.itemAvgScore = res.data.itemAvgScore;
          this.form2.itemAvgText = res.data.itemAvgText;
        } else {
          this.form2.itemAvgScore = "";
          this.form2.itemAvgText = "";
        }
      });
    },
    sexclick(item) {
      this.queryParams3.sex = item.value;
      if (this.queryParams3.itemId != null) {
        this.getItemCountryScore();
      }
    },
    xmclick(row) {
      this.xmname = row.itemName;
      this.queryParams3.itemId = row.id;
      this.itemId2 = row.id;
      this.form.itemId = row.id;
      this.form2.itemId = row.id;
      this.innerVisible = false;
      this.form.itemName = row.itemName;
      if (row.resultDictType == this.ResultType1.dictCode) {
        this.form2.resultDictType = this.ResultType1.dictValue;
        this.form.dictValue = this.ResultType1.dictValue;
        this.form2.itemAvgText = "";
      }

      if (row.resultDictType == this.ResultType2.dictCode) {
        this.form2.resultDictType = this.ResultType2.dictValue;
        this.form.dictValue = this.ResultType2.dictValue;
        this.form.itemAvgScore = "";
        this.form2.itemAvgScore = "";
      }
      if (this.queryParams3.sex != null) {
        this.getItemCountryScore();
      }
    },

    /** 查询全国平均成绩列表 */
    getList() {
      this.loading = true;
      testItemslist(this.queryParams2).then(res => {
        this.testItemslists = res.rows;
        this.total2 = res.total;
      });
      listTestItemCountryScore(this.queryParams).then(response => {
        this.testItemCountryScoreList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      tKindergartenInfo(this.itemResultType).then(res => {
        this.itemResultTypelist = res.data;
        this.ResultType1 = this.itemResultTypelist[0];
        this.ResultType2 = this.itemResultTypelist[1];
      });
    },
    // 取消按钮
    cancel2() {
      this.open2 = false;
      this.xmname = "请选择所属项目";
      this.reset();
    },
    cancel() {
      this.open = false;
      this.xmname = "请选择所属项目";
      this.itemId2 = "";
      this.reset();
      this.$nextTick(() => {
        this.$refs.form2.clearValidate();
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        itemId: "",
        itemAvgScore: "",
        itemAvgText: "",
        resultDictType: "", //number text
        sex: ""
      };
      this.form2 = {
        itemId: "",
        itemAvgScore: null,
        itemAvgText: "",
        resultDictType: "", //number text
        sex: ""
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery2() {
      this.queryParams2.pageNum = 1;
      testItemslist(this.queryParams).then(response => {
        this.testItemslist = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getList();
      });
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.handleQuery2();
      this.queryParams.itemName = "";
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.queryParams3 = {
        itemId: null,
        sex: null
      };
      this.reset();
      this.open = true;
      this.title = "添加全国平均成绩";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.itemId2 = row.itemId;
      this.reset();
      const id = row.id || this.ids;
      getTestItemCountryScore(id).then(response => {
        this.form = response.data;
        this.xmname = response.data.itemName;
        this.open2 = true;
        this.title = "修改全国平均成绩";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.dictValue == this.ResultType1.dictValue) {
        this.form.itemAvgText = "";
      }
      if (this.form.dictValue == this.ResultType2.dictValue) {
        this.form.itemAvgScore = "";
      }
      if (this.form.id != null) {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateTestItemCountryScore(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open2 = false;

              this.getList();
            });
            this.itemId2 = "";
            this.xmname = "";
            this.xmname = "请选择所属项目";
          }
        });
      } else {
        this.$refs["form2"].validate(valid => {
          if (valid) {
            this.form2.itemAvgScore = Number(this.form2.itemAvgScore);
            addTestItemCountryScore(this.form2).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.itemId2 = "";
              this.xmname = "";
              this.xmname = "请选择所属项目";
            });
          } else {
            if (this.form2.itemAvgScore == "") {
              this.form2.itemAvgScore == "";
            }
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除全国平均成绩的数据项？")
        .then(function() {
          return delTestItemCountryScore(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/testItemCountryScore/export",
        {
          ...this.queryParams
        },
        `testItemCountryScore_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
