<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="batchCode">
        <el-input
          v-model="queryParams.batchCode"
          placeholder="请输入批次编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次名称" prop="batchName">
        <el-input
          v-model="queryParams.batchName"
          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:testScoreBatch: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:testScoreBatch: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:testScoreBatch: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:testScoreBatch:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="testScoreBatchList"
      @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="batchCode" />
      <el-table-column label="批次名称" align="center" prop="batchName" />
      <el-table-column
        label="备注信息"
        align="center"
        prop="remark"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <!-- <el-table-column label="修改人" align="center" prop="updateBy" />
      <el-table-column label="修改时间" align="center" prop="updateTime" /> -->
      <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-search"
            @click="seeitem(scope.row)"
            v-hasPermi="['system:studentInfo:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:testScoreBatch:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:testScoreBatch: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="900px" append-to-body>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :inline="true"
      >
        <el-form-item label="批次名称" prop="batchName">
          <el-input
            style="width: 250px"
            v-model="form.batchName"
            placeholder="请输入批次名称"
            maxlength="30"
          />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注信息"
            style="width: 250px"
            maxlength="100"
          />
        </el-form-item>

        <el-form-item label="测试项目" prop="itemBatchInfoIdList">
          <el-button
            size="small"
            @click="innerVisible3 = true"
            type="primary"
            plain
            >添加测试项目</el-button
          >
        </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>

      <div>
        <el-table
          ref="singleTable"
          :data="items"
          style="width: 95%; margin: 20px auto"
        >
          <el-table-column type="index" width="80" align="center">
          </el-table-column>
          <el-table-column
            property="itemCode"
            label="项目编号"
            width="230"
            align="center"
          >
          </el-table-column>
          <el-table-column
            property="itemName"
            label="项目名称"
            width="230"
            align="center"
          >
          </el-table-column>
          <el-table-column
            property="itemType"
            label="字典类型"
            align="center"
            width="180"
          >
            <template slot-scope="scope">
              {{
                scope.row.itemType == ResultType1.dictCode
                  ? ResultType1.dictLabel
                  : ""
              }}{{
                scope.row.itemType == ResultType2.dictCode
                  ? ResultType2.dictLabel
                  : ""
              }}</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-delete"
                @click="delitem(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <!-- <pagination
          v-show="total2 > 0"
          :total="total2"
          :page.sync="temqueryParams.pageNum"
          :limit.sync="temqueryParams.pageSize"
          @pagination="lookitem"
        /> -->
      </div>
    </el-dialog>

    <!-- 测试项目弹窗 -->
    <el-dialog
      width="1000px"
      title="选择测试项目名称"
      :visible.sync="innerVisible3"
      append-to-body
    >
      <el-form
        :model="queryParams4"
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="90px"
      >
        <el-form-item label="项目名称" prop="itemName">
          <el-input
            v-model="queryParams4.itemName"
            placeholder="请输入项目名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery4"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <el-table :data="testItemss" @cell-dblclick="addtestitem">
        <el-table-column label="请选择" width="70" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 20px; height: 20px; padding: 0px"
              v-if="items.find((e) => e.itemId == scope.row.id)"
              @click="deltestitem(scope.row)"
            ></el-button>
            <el-button
              v-else
              style="width: 20px; height: 20px; padding: 0px"
              @click="addtestitem(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column
          property="itemCode"
          label="项目编号"
          width="150"
          align="center"
        >
        </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="itemContent"
          label="项目内容"
          width="150"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="measureUnit"
          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="creatBy"
          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="total4 > 0"
        :total="total4"
        :page.sync="queryParams4.pageNum"
        :limit.sync="queryParams4.pageSize"
        @pagination="getList"
        style="margin-top: 40px"
      />

      <el-button
        type="primary"
        size="mini"
        @click="itemclick"
        style="
          margin-top: 30px;
          margin-left: 85%;
          width: 100px;
          height: 40px;
          font-size: 15px;
        "
        >选择完成</el-button
      >
    </el-dialog>

    <el-dialog
      width="680px"
      title="查看测试项目"
      :visible.sync="innerVisible"
      append-to-body
    >
      <el-table ref="singleTable" :data="itemlist" style="width: 100%">
        <el-table-column type="index" width="100" align="center">
        </el-table-column>
        <el-table-column
          property="itemCode"
          label="项目编号"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="itemName"
          label="项目名称"
          width="180"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="resultDictType"
          label="字典类型"
          width="180"
          align="center"
        >
          <template slot-scope="scope">
            {{
              scope.row.resultDictType == ResultType1.dictCode
                ? ResultType1.dictLabel
                : ""
            }}{{
              scope.row.resultDictType == ResultType2.dictCode
                ? ResultType2.dictLabel
                : ""
            }}</template
          >
        </el-table-column>
      </el-table>
      <!-- <pagination
        v-show="total2 > 0"
        :total="total2"
        :page.sync="temqueryParams.pageNum"
        :limit.sync="temqueryParams.pageSize"
        @pagination="lookitem"
      /> -->
    </el-dialog>
  </div>
</template>

<script>
import {
  listTestScoreBatch,
  getTestScoreBatch,
  delTestScoreBatch,
  addTestScoreBatch,
  updateTestScoreBatch,
  temBatchInfoList,
} from "@/api/system/testScoreBatch";
import { testItems } from "@/api/system/testItemScore";
import { tKindergartenInfo } from "@/api/system/kindergartenInfo";
export default {
  name: "TestScoreBatch",
  data() {
    var itembatch = (rule, value, callback) => {
      var str = this.form.itemBatchInfoIdList;
      console.log(str);
      if (str.length > 0) {
        callback();
      } else {
        return callback(new Error("请添加测试项目"));
      }
    };
    return {
      itemResultType: "item_result_type",
      ResultType1: "", //numberLeixing
      ResultType2: "", //textleixing
      innerVisible: false,
      innerVisible3: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      total2: 0,
      testItemss: [],
      total4: 0,
      // 测试成绩批次表格数据
      testScoreBatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        batchCode: null,
        batchName: null,
      },
      temqueryParams: {
        pageNum: 1,
        pageSize: 9999,
        id: "",
      },
      // 测试项目
      queryParams4: {
        pageNum: 1,
        pageSize: 10,
        itemName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // batchCode: [
        //   { required: true, message: "批次编号不能为空" },
        //   { type: "number", message: "批次编号必须为数字值", trigger: "blur" },
        // ],type:
        itemBatchInfoIdList: [
          {
            required: true,
            validator: itembatch,
            message: "",
            trigger: "blur",
          },
        ],
        batchName: [{ required: true, message: "批次名称不能为空" }],
        remark: [{ required: true, message: "备注信息不能为空" }],
      },
      items: [],
      itemlist: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    delitem(item) {
      console.log(item);
      this.items = this.items.filter((e) => {
        return e.itemId != item.itemId;
      });
    },
    deltestitem(item) {
      console.log(item);
      this.items = this.items.filter((e) => {
        return e.itemId != item.id;
      });
    },
    addtestitem(row) {
      var item = {
        itemId: row.id,
        itemName: row.itemName,
        itemType: row.resultDictType,
        itemCode: row.itemCode,
      };
      this.items.push(item);
      // this.form.itemBatchInfoIdList.push(row.id);
      console.log(this.items);
    },
    itemclick() {
      this.innerVisible3 = false;
    },
    lookitem(item) {
      console.log(item)
      this.itemlist = [];
      this.items = [];
      this.temqueryParams.batchId = item.id;
      temBatchInfoList(this.temqueryParams).then((res) => {
        console.log(res);
        if (res.rows.length != 0) {
          this.itemlist = res.rows;
          this.total2 = res.total;
          this.items = res.rows;
          this.items.forEach((e) => {
            e.itemType = e.resultDictType;
            e.itemId = e.id;
          });
        }
        console.log(this.items);
      });
    },
    seeitem(item) {
      this.lookitem(item);
      console.log(this.items);

      if (this.items != []) {
        this.innerVisible = true;
      }
    },
    handleQuery4() {
      this.queryParams4.pageNum = 1;
      testItems(this.queryParams4).then((response) => {
        this.testItems = response.rows;
        this.total = response.total;
        this.loading = false;
        this.getList();
      });
    },
    /** 查询测试成绩批次列表 */
    getList() {
      tKindergartenInfo(this.itemResultType).then((res) => {
        // console.log(res);
        this.ResultType1 = res.data[0];
        this.ResultType2 = res.data[1];
        console.log(this.ResultType1, this.ResultType2);
      });
      // 查询测试项目
      testItems(this.queryParams4).then((res) => {
        // console.log(res)
        this.testItemss = res.rows;
        this.total4 = res.total;
        this.loading = false;
      });
      this.loading = true;
      listTestScoreBatch(this.queryParams).then((response) => {
        this.testScoreBatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.items = [];
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        batchCode: null,
        batchName: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        itemBatchInfoIdList: [],
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
      this.queryParams.className = "";
      this.queryParams4.itemName = null;
    },
    // 多选框选中数据
    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.items = [];
      this.total2 = 0;
      this.title = "添加测试成绩批次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      this.lookitem(row);
      getTestScoreBatch(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改测试成绩批次";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.itemBatchInfoIdList = [];
      this.items.forEach((e) => {
        this.form.itemBatchInfoIdList.push(e.itemId);
      });
      console.log(this.form);
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTestScoreBatch(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTestScoreBatch(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 delTestScoreBatch(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/testScoreBatch/export",
        {
          ...this.queryParams,
        },
        `testScoreBatch_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped='scss'>
.title {
  font-size: 600;
}
.text {
  margin-left: 10px;
  margin-right: 10px;
  font-weight: 500;
  display: inline-block;
  width: 100px;
  border: 1px solid #f0f0f0;
  text-align: center;
  border-radius: 4px;
}
</style>
