<template>
  <div>
    <div class="d-flex flex-between pt7">
      <div>
        <!-- <el-upload
          class="upload-demo"
          :action="import_type_url"
          :show-file-list="false"
          :data="import_type_data"
          :on-success="import_success"
          style="display: inline-block; margin: 0 10px"
        >
          <el-button type="primary">导入</el-button>
        </el-upload>
        <el-button type="primary" @click="exportModle">导入模板下载</el-button> -->
        <el-button
          type="primary"
          @click="exportAll"
          :disabled="tableData.length == 0"
          >导出</el-button
        >
        <el-button type="primary" @click="addData">添加</el-button>
        <el-button
          type="danger"
          @click="delData"
          :disabled="multipleSelection.length == 0"
          >删除</el-button
        >
      </div>
      <el-form :inline="true">
        <!-- <el-form-item label="">
          <el-select
            v-model="this.xdxn_id"
            placeholder="请选择年级"
            clearable
            @change="gradeChange"
          >
            <el-option
              v-for="(item, i) in gradeList"
              :key="i"
              :label="item.grade_name"
              :value="item.grade_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="">
          <el-select
            v-model="this.xdxb_id"
            placeholder="请选择班级"
            clearable
            @change="classChange"
          >
            <el-option
              v-for="(item, index) in classList"
              :key="index"
              :label="item.class_name"
              :value="item.class_num"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item class="mr10" prop="keyword" label="">
          <el-input
            placeholder="请输入学生姓名"
            v-model="keyword"
            clearable
            @clear="getListData"
          ></el-input>
        </el-form-item>
        <el-form-item class="mr10" prop="xdxx_xsxh" label="">
          <el-input
            placeholder="请输入学籍号"
            v-model="xdxx_xsxh"
            clearable
            @clear="getListData"
          ></el-input>
        </el-form-item>

        <el-button type="primary" class="ml5" @click="getListData"
          >搜索</el-button
        >
        <!-- <el-button type="primary" class="ml5" @click="clearData">清空</el-button> -->
      </el-form>
    </div>
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      v-loading="loading"
      ref="multipleTable"
      @selection-change="handleSelectionChange"
      style="width: 100%"
    >
      <el-table-column type="selection" align="center" width="55">
      </el-table-column>
      <el-table-column label="年级" prop="grade_name" width="">
      </el-table-column>
      <el-table-column label="班级" prop="class_name" width="200">
      </el-table-column>
      <el-table-column label="学生姓名" prop="xdxx_xsxm" width="">
      </el-table-column>
      <el-table-column label="学籍号" prop="xdxx_xsxh" width="250">
      </el-table-column>
      <el-table-column label="性别" prop="xdxx_xsxb" width="">
      </el-table-column>
      <el-table-column label="总分" prop="xdxx_zdf" width=""> </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑 </el-button>
          <!-- <el-button type="text" @click="detailData(scope.row)"
            >管理
          </el-button> -->

          <el-button type="text" @click="czdaData(scope.row)"
            >成长档案
          </el-button>
          <el-button type="text" @click="rate(scope.row)">评价 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer
      class="flex-item-none"
      style="display: flex; justify-content: flex-end; margin-top: 30px"
    >
      <el-pagination
        background
        layout="total,prev, pager, next"
        :page-size="limit"
        :total="total"
        :current-page.sync="cur_page"
        @current-change="handleCurrentChange"
      ></el-pagination>
    </footer>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="700px">
      <el-form
        :inline="true"
        :model="dialogForm"
        ref="dialogForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item class="mr75" label="学生姓名" prop="xsxm">
          <!-- <el-input v-model="dialogForm.xsxm"></el-input> -->
          <RemoteSelect
            v-model="dialogForm.xsxh"
            :label.sync="dialogForm.xsxm"
            url="/xddy/dygl_xsxjk/index"
            field="xdx_xsxh,xdx_xsxm"
          />
        </el-form-item>
        <el-form-item class="mr10" label="学籍号" prop="xsxh">
          <el-input disabled v-model="dialogForm.xsxh"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="年级" prop="nj">
          <!-- <el-input v-model="dialogForm.nj"></el-input> -->

          <el-select
            v-model="dialogForm.nj_id"
            placeholder="请选择年级"
            clearable
            filterable
          >
            <el-option
              v-for="(item, i) in gradeList"
              :key="i"
              :label="item.grade_name"
              :value="item.grade_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr10" label="班级" prop="bj">
          <!-- <el-input v-model="dialogForm.bj"></el-input> -->
          <el-select
            v-model="dialogForm.bj_id"
            placeholder="请选择班级"
            clearable
            filterable
          >
            <el-option
              v-for="(item, index) in classList"
              :key="index"
              :label="item.class_name"
              :value="item.class_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="mr75" label="性别" prop="xb">
          <el-radio-group v-model="dialogForm.xb">
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="text-center">
        <el-button @click="formCancel">取 消</el-button>
        <el-button type="primary" @click="formSubmit('dialogForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <el-dialog
      title="评价"
      :visible.sync="commentDialogVisible"
      width="550px"
      v-loading="loading"
    >
      <el-form
        :model="commentForm"
        ref="commentForm"
        :rules="rules"
        label-width="130px"
      >
        <el-form-item class="mr75" label="习惯养成得分" prop="xgycdf">
          <el-input v-model="commentForm.xgycdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="课堂学习得分" prop="ktxxdf">
          <el-input @input="score" v-model="commentForm.ktxxdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="过程性成绩得分" prop="gcxcjdf">
          <el-input @input="score" v-model="commentForm.gcxcjdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="期末评估得分" prop="qmpgdf">
          <el-input @input="score" v-model="commentForm.qmpgdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="特长展示得分" prop="tczsdf">
          <el-input @input="score" v-model="commentForm.tczsdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="参加活动得分" prop="cyhddf">
          <el-input @input="score" v-model="commentForm.cyhddf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="学生体检得分" prop="xstjdf">
          <el-input @input="score" v-model="commentForm.xstjdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="体康测试得分" prop="tkcsdf">
          <el-input @input="score" v-model="commentForm.tkcsdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="班级值日得分" prop="bjzrdf">
          <el-input @input="score" v-model="commentForm.bjzrdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="社会实践得分" prop="shsjdf">
          <el-input @input="score" v-model="commentForm.shsjdf"></el-input>
        </el-form-item>
        <el-form-item class="mr75" label="总得分" prop="zdf">
          <el-input @input="score" v-model="commentForm.zdf"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="commentDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="commentFormSubmit('commentForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import RemoteSelect from "@/components/RemoteSelect/index.vue";
import {
  xs_list,
  xs_edit,
  xs_add,
  xs_del,
  xs_export,
  grade_search,
  class_search,
  xslb_list,
  xslb_detail,
} from "./api";
import { useUserStore } from "@/stores/user";
const { token } = useUserStore();
export default {
  name: "index",
  components: { RemoteSelect },
  data() {
    return {
      limit: 10,
      total: 12,
      cur_page: 1,
      keyword: "",
      grade_name: "",
      class_name: "",
      xdxx_xsxm: "",
      xdxx_xsxh: "",
      tableData: [],
      dialogForm: {
        xsxm: "",
        xsxh: "",
        nj_id: this.xdxn_id,
        nj_name: "",
        bj_id: this.xdxb_id,
        bj_name: "",
        xb: "",
      },

      xdxn_id: "",
      xdxb_id: "",
      gradeList: [],
      classList: [],
      title: "",
      dialogFormVisible: false,
      commentDialogVisible: false,
      loading: false,
      multipleSelection: [],
      delSelection: [],
      // 导入导出
      import_type_url:
        window.globalVariables.api +
        "/xddy/dygl_xsczda_xs/import?issubmit=1&token=" +
        token,
      import_type_data: {},
      commentForm: {
        xgycdf: "",
        ktxxdf: "",
        gcxcjdf: "",
        qmpgdf: "",
        tczsdf: "",
        cyhddf: "",
        xstjdf: "",
        tkcsdf: "",
        bjzrdf: "",
        shsjdf: "",
        zdf: "",
      },
      rules: {
        xgycdf: [{ required: true, message: "请打分", trigger: "blur" }],
        ktxxdf: [{ required: true, message: "请打分", trigger: "blur" }],
        gcxcjdf: [{ required: true, message: "请打分", trigger: "blur" }],
        qmpgdf: [{ required: true, message: "请打分", trigger: "blur" }],
        tczsdf: [{ required: true, message: "请打分", trigger: "blur" }],
        cyhddf: [{ required: true, message: "请打分", trigger: "blur" }],
        xstjdf: [{ required: true, message: "请打分", trigger: "blur" }],
        tkcsdf: [{ required: true, message: "请打分", trigger: "blur" }],
        bjzrdf: [{ required: true, message: "请打分", trigger: "blur" }],
        shsjdf: [{ required: true, message: "请打分", trigger: "blur" }],
        zdf: [{ required: true, message: "请打分", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    getListData() {
      this.loading = true;
      let data = {
        page: this.cur_page,
        limit: this.limit,
        keyword: this.keyword,
        grade_id: this.xdxn_id,
        class_id: this.xdxb_id,
        xdxx_xsxm: this.xdxx_xsxm,
        xdxx_xsxh: this.xdxx_xsxh,
      };

      xs_list(data).then((res) => {
        this.loading = false;
        this.tableData = res.data.page_data;
        this.cur_page = Number(res.data.page_now);
        this.total = Number(res.data.total_rows);
      });
    },
    gradeChange(e) {
      this.xdxn_id = e;
      this.getListData();
    },
    classChange(e) {
      this.xdxb_id = e;
      this.getListData();
    },
    addData() {
      this.title = "添加/申请";
      this.dialogForm = {
        xsxm: "",
        xsxh: "",
        nj_id: this.xdxn_id,
        nj_name: "",
        bj_id: this.xdxb_id,
        bj_name: "",
        xb: "1",
      };
      this.isEdit = false;
      this.dialogFormVisible = true;
    },
    editData(item) {
      this.title = "编辑信息";
      this.isEdit = true;
      this.dialogFormVisible = true;
      this.id = item.xdxx_id;
      this.dialogForm.xsxm = item.xdxx_xsxm;
      this.dialogForm.xsxh = item.xdxx_xsxh;
      this.dialogForm.nj_id = item.grade_id;
      this.dialogForm.nj_name = item.grade_name;
      this.dialogForm.bj_id = item.class_id;
      this.dialogForm.bj_name = item.class_name;
      this.dialogForm.xb = item.xdxx_xsxb == "男" ? "1" : "2";
    },
    formSubmit(formName) {
      this.gradeList.forEach((item) => {
        if (item.grade_id == this.dialogForm.nj_id) {
          this.dialogForm.nj_name = item.grade_name;
        }
      });
      this.classList.forEach((item) => {
        if (item.class_id == this.dialogForm.bj_id) {
          this.dialogForm.bj_name = item.class_name;
        }
      });

      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdxx_xsxm: this.dialogForm.xsxm,
            xdxx_xsxh: this.dialogForm.xsxh,
            xdxx_xsxb: this.dialogForm.xb,
            grade_id: this.dialogForm.nj_id,
            grade_name: this.dialogForm.nj_name,
            class_id: this.dialogForm.bj_id,
            class_name: this.dialogForm.bj_name,
          };
          console.log(data, 11);
          // return;
          if (!this.isEdit) {
            xs_add(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "信息添加成功！",
                  type: "success",
                });
                this.dialogFormVisible = false;
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          } else {
            data.xdxx_id = this.id;
            xs_edit(data).then((res) => {
              if (res.code == 1) {
                this.$message({
                  message: "信息编辑成功！",
                  type: "success",
                });

                this.dialogFormVisible = false;
                this.getListData();
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            });
          }
        }
      });
    },
    import_success(res) {
      if (res.code == 1) {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "success",
        });
        this.getListData();
      } else {
        this.$message({
          dangerouslyUseHTMLString: true,
          message: res.msg,
          type: "error",
        });
      }
    },

    handleSelectionChange(val) {
      this.multipleSelection = [];
      this.delSelection = [];
      for (let i in val) {
        this.multipleSelection.push(val[i].xdxx_id);
        this.delSelection.push(val[i].xdxx_id);
      }
    },
    delData() {
      this.$confirm("确认删除这些信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let data = {
            xdxx_id: this.multipleSelection,
          };
          console.log(data, 222222222222);
          xs_del(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "信息删除成功！",
                type: "success",
              });
              this.getListData();
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        })
        .catch(() => {});
    },
    rate(item) {
      this.title = "评价";

      this.id = item.xdxx_id;
      this.commentForm.xgycdf = Number(item.xdxx_xgycdf);
      this.commentForm.ktxxdf = Number(item.xdxx_ktxxdf);
      this.commentForm.gcxcjdf = Number(item.xdxx_gcxcjdf);
      this.commentForm.qmpgdf = Number(item.xdxx_qmpgdf);
      this.commentForm.tczsdf = Number(item.xdxx_tczsdf);
      this.commentForm.cyhddf = Number(item.xdxx_cyhddf);
      this.commentForm.xstjdf = Number(item.xdxx_xstjdf);
      this.commentForm.tkcsdf = Number(item.xdxx_tkcsdf);
      this.commentForm.bjzrdf = Number(item.xdxx_bjzrdf);
      this.commentForm.shsjdf = Number(item.xdxx_shsjdf);
      this.commentForm.zdf = Number(item.xdxx_zdf);
      this.commentDialogVisible = true;
      this.score();
    },
    score() {
      this.commentForm.zdf = Number(
        Number(this.commentForm.xgycdf) +
          Number(this.commentForm.ktxxdf) +
          Number(this.commentForm.gcxcjdf) +
          Number(this.commentForm.qmpgdf) +
          Number(this.commentForm.tczsdf) +
          Number(this.commentForm.cyhddf) +
          Number(this.commentForm.xstjdf) +
          Number(this.commentForm.tkcsdf) +
          Number(this.commentForm.bjzrdf) +
          Number(this.commentForm.shsjdf)
      );
    },
    commentFormSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let data = {
            xdxx_xgycdf: this.commentForm.xgycdf,
            xdxx_ktxxdf: this.commentForm.ktxxdf,
            xdxx_gcxcjdf: this.commentForm.gcxcjdf,
            xdxx_qmpgdf: this.commentForm.qmpgdf,
            xdxx_tczsdf: this.commentForm.tczsdf,
            xdxx_cyhddf: this.commentForm.cyhddf,
            xdxx_xstjdf: this.commentForm.xstjdf,
            xdxx_tkcsdf: this.commentForm.tkcsdf,
            xdxx_bjzrdf: this.commentForm.bjzrdf,
            xdxx_shsjdf: this.commentForm.shsjdf,
            xdxx_zdf: this.commentForm.zdf,
          };
          data.xdxx_id = this.id;
          xs_edit(data).then((res) => {
            if (res.code == 1) {
              this.$message({
                message: "评价成功！",
                type: "success",
              });
              this.getListData();
              this.commentDialogVisible = false;
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        }
      });
    },

    formCancel() {
      this.dialogFormVisible = false;
    },
    exportModle() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsczda_xs/index?api=xls&page=1&limit=1&token=" +
        token;
      console.log(link, 3333333);
      window.open(link, "_blank");
    },
    exportAll() {
      let link =
        window.globalVariables.api +
        "/xddy/dygl_xsczda_xs/index?api=xls&page=1&limit=10000&token=" +
        token +
        "&xdxx_id=" +
        this.multipleSelection.join(",");
      window.open(link, "_blank");
    },
    gradeListData() {
      let data = {
        page: 1,
        limit: 9999,
      };
      grade_search(data).then((res) => {
        if (res.code == 1) {
          this.gradeList = res.data.page_data;
          // let obj = {
          //   grade_id: "",
          //   grade_name: "全部",
          // };
          // this.gradeList.unshift(obj);
        }
      });
    },
    classListData() {
      let data = {
        page: 1,
        limit: 9999,
        grade_id: this.xdxn_id,
      };
      class_search(data).then((res) => {
        if (res.code == 1) {
          this.classList = res.data.page_data;
          // let obj = {
          //   class_id: "",
          //   class_name: "全部",
          // };
          // this.classList.unshift(obj);
          // console.log(this.classList, 3);
        }
      });
    },
    // clearData() {
    //   this.keyword = "";
    //   this.firstForm.grade = "";
    //   this.firstForm.class = "";
    //   this.xdxx_xsxh = "";
    // },
    czdaData(item) {
      this.$router.push({
        name: "xsczda_xslb_bjlb_xslbxq_czda",
        params: {
          xdxx_xsxh: item.xdxx_xsxh,
          id: item.xdxx_id,
        },
      });
    },
  },
  mounted() {
    if (this.$route.params.id != "") {
      this.xdxn_id = this.$route.params.nj_id;
      this.xdxb_id = this.$route.params.bj_id;
      console.log(this.xdxn_id, this.xdxb_id, 2);
      this.getListData();
      this.gradeListData();
      this.classListData();
    }
  },
};
</script>

<style scoped>
::v-deep .el-input {
  width: 160px;
}
</style>