<style lang="scss">
	.area{
		.el-select{
			width: 120px;
			display: inline-block;
			margin-right: 6px;
		}
	}
	.upload_demo{
		display: inline-block;
		margin: 0 5px;
	}
</style>
<template>
  <section class="content">
    <div class="breadcrumb">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item><a href="/">学校管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>学生管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <el-form :model="queryParams" size="mini"  :inline="true" style="padding-left: 20px;">
     <el-form-item label="姓名">
     	<el-input placeholder="请输入姓名" clearable v-model="queryParams.name"></el-input>
     </el-form-item>
	 <el-form-item label="学校">
	 	<el-select
	 	  v-model="queryParams.school_id"
	 	  placeholder="请选择学校"
	 	  clearable
	 	  filterable  
	 		  @change="getClass"
	 	>
	 	  <el-option
	 	    v-for="item in schoolList"
	 	    :key="item.value"
	 	    :label="item.name"
	 	    :value="item.id"
	 	  >
	 	  </el-option>
	 	</el-select>
	 </el-form-item>
	 <el-form-item label="年级">
	 	<el-select
	 	  v-model="queryParams.grade_name"
	 	  placeholder="请选择年级"
	 	  clearable
	 	  filterable  
	 		  @change="getClass"
	 	>
	 	  <el-option
	 	    v-for="item in gradeList"
	 	    :key="item"
	 	    :label="item"
	 	    :value="item"
	 	  >
	 	  </el-option>
	 	</el-select>
	 </el-form-item>
	 <el-form-item label="班级">
	 	<el-select
	 	  v-model="queryParams.class_id"
	 	  placeholder="请选择班级"
	 	  clearable
	 	  filterable  
	 	>
	 	  <el-option
	 	    v-for="item in classList"
	 	    :key="item.value"
	 	    :label="item.name"
	 	    :value="item.id"
	 	  >
	 	  </el-option>
	 	</el-select>
	 </el-form-item>
	 <el-form-item label-width="10" style="float: right;">
	   <el-button type="primary" @click="getData" size="mini"
	     >搜索</el-button
	   >
	   <el-button
	     type="info"
	     size="mini"
	     @click="queryParams={},getData()"
	     >重置</el-button
	   >
	   <el-button
	     type="info"
	     size="mini"  icon="el-icon-download"
	     @click="download"
	     >下载模版</el-button
	   >
	   <el-upload
	     class="upload_demo"
	     action="/api/admin/school/student/upload"
		 :on-success='uploadSuccess'
		 :show-file-list='false'
	     multiple
	     :limit="3">
	     <el-button icon="el-icon-upload" size="mini" type="primary">批量导入</el-button>
	   </el-upload>
	   <el-button  
	 			  type="primary"
	 			  plain 
	 			  icon="el-icon-plus"
	 			  size="mini"
	 			  @click="handleAdd"
	 			  >添加学生</el-button>
		<el-button type="primary" size="mini" @click="del(-1)">批量删除</el-button>
	 </el-form-item>

    </el-form>
    <el-table
      v-loading="loading"
      :data="list"
      style="width: 100%; margin-top: 10px"
      height="55vh"
    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column align="center" prop="name" label="学生姓名" />
      <el-table-column align="center" prop="student_no" label="学号" />
	  <el-table-column label="性别" prop="sex"></el-table-column>
      <el-table-column label="年龄" prop="age"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
	  <el-table-column label="所属班级" prop="grade_name">
	  		  <template slot-scope="scope">{{scope.row.grade_name}}{{scope.row.class_name}}</template>
	  </el-table-column>
	  <el-table-column label="所属学校" prop="school_name"></el-table-column>
	  
      <el-table-column align="center" prop="ctime" label="创建时间" />
      <el-table-column align="center" prop="date" label="操作" width="320">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" size="mini" type="info" plain
            >编辑</el-button
          >
          <el-button @click="del(scope.row.id)" size="mini" type="info" plain
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Page
      ref="pageButton"
      :current="form.page"
      :page_size="form.page_size"
      :total="total"
      @pageChange="gopage"
    />
    <!-- 新增学生-->
	<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
	  <el-form   ref="form" :model="form" :rules="rules" label-width="80px">
		  <el-form-item label="学生姓名" prop="name">
		    <el-input
		      clearable
		      v-model="form.name"
		      placeholder="请输入名称"
		    ></el-input>
		  </el-form-item>
		  <el-form-item label="学号" prop="student_no">
		    <el-input
		      clearable
		      v-model="form.student_no"
		      placeholder="请输入学号"
		    ></el-input>
		  </el-form-item>
		  <el-form-item label="性别" prop="sex">
		      <el-select
		        v-model="form.sex"
		        placeholder="请选择"
		        clearable
		        filterable  
		      >
		  		<el-option label="男" value="男"></el-option>
		  		<el-option label="女" value="女"></el-option>
		        </el-option>
		      </el-select>
		    </el-form-item>
		    <el-form-item label="年龄" prop="age">
		      <el-input
		        clearable
		        v-model="form.age"
		        placeholder="请输入年龄"
		      ></el-input>
		    </el-form-item>
			<el-form-item label="电话" prop="age">
			  <el-input
			    clearable
			    v-model="form.phone"
			    placeholder="请输入电话"
			  ></el-input>
			</el-form-item>
		  <el-form-item label="学校" prop="school_id">
		    <el-select
		      v-model="form.school_id"
		      placeholder="请选择学校"
		      clearable
		      filterable  
		  	@change="getClass1"
		    >
		      <el-option
		        v-for="item in schoolList"
		        :key="item.value"
		        :label="item.name"
		        :value="item.id"
		      >
		      </el-option>
		    </el-select>
		  </el-form-item>
		  <el-form-item label="年级" prop='grade_name'>
		    <el-select 
		      v-model="form.grade_name"
		      placeholder="请选择"
		      clearable
		      filterable  
		  	@change="getClass1"
		    >
		      <el-option
		        v-for="item in gradeList"
		        :key="item"
		        :label="item"
		        :value="item"
		      >
		      </el-option>
		    </el-select>
		  </el-form-item>
		  <el-form-item label="班级">
		    <el-select 
		      v-model="form.class_id"
		      placeholder="请选择"
		      clearable
		      filterable  
		    >
		      <el-option
		        v-for="item in classList1"
		        :key="item.value"
		        :label="item.name"
		        :value="item.id"
		      >
		      </el-option>
		    </el-select>
		  </el-form-item>
	  </el-form>
	  <div slot="footer" class="dialog-footer">
	    <el-button type="primary" @click="submitForm">保存</el-button>
	    <el-button @click="open = false">取 消</el-button>
	  </div>
	</el-dialog>
  </section>
</template>
<script>
import Page from "../../components/Page";
export default {
  components: {
    Page,
  },
  data() {
    return {
      loading: false,
      queryParams: {
        page: 1,
      },
      form: {},
      form1: {},
      form2: {},
      list: [{}, {}],
      total: 0,
      title: "新增学校",
      open: false,
	  open2: false,
      doctorList: [],
      rules: {},
	  schoolList: [],
	  gradeList:[],
	  classList:[],
	  classList1:[],
	  ids:''
    };
  },
  methods: {
	  handleSelectionChange(val) {
	  	const ids=[]
	  	val.forEach(item=>{
	  		ids.push(item.id)
	  	})
	  	this.ids=ids.join(',')
	  },
	download(){
		window.open('http://test.scxjc.club/supermark_students.xlsx')  
	},
	uploadSuccess(){
		this.msgSuccess("上传成功");
		this.getData();
	},
    del(id) {
		if(id == -1){
			if(this.ids){
				id=this.ids
			}else{
				this.msgInfo('请选择删除的数据');
				return
			}
		}
      this.$confirm("确认删除?", "提示", {
        type: "warning",
      }).then(() => {
        this.$api
          .delStudent({
            id: id,
          })
          .then((res) => {
            if (!res.data.code) {
              this.msgSuccess("删除成功");
              this.getData();
            } else {
              this.msgError(res.data.message);
            }
          });
      });
    },
    gopage(size) {
      if (size) {
        this.queryParams.page_size = size;
      }
      this.queryParams.page = this.$refs.pageButton.page;
      this.getData();
    },
	// 获取学校 年级
	getList(){
		this.$api.schoolList().then((res) => {
		  this.schoolList = res.data.data.list;
		});
		this.$api.gradeList().then((res) => {
		  this.gradeList = res.data.data;
		  
		});
	},
	// 获取班级
	getClass(){
		this.$api.classList({school_id:this.queryParams.school_id,grade_name:this.queryParams.grade_name}).then((res) => {
		  this.classList = res.data.data.list;
		});
	},
	getClass1(){
		this.$api.classList({school_id:this.form.school_id,grade_name:this.form.grade_name}).then((res) => {
		  this.classList1 = res.data.data.list;
		});
	},
    getData() {
      this.loading = true;
      this.$api.studentList(this.queryParams).then((res) => {
        this.list = res.data.data.list;
        this.total = res.data.data.total;
        this.loading = false;
      });
    },
    handleAdd() {
      this.open = true;
      this.title = "新增学生";
      this.form = {student_no:''};
    },
    edit(item) {
      this.title = "编辑";
	  item.school_id=Number(item.school_id)
	  item.class_id=Number(item.class_id)
      this.form = item;
      this.open = true;
	  this.getClass1()
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$api.editStudent(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          } else {
            this.$api.addStudent(this.form).then((res) => {
              if (res.data.code == 0) {
                this.msgSuccess("成功!");
                this.open = false;
                this.getData();
              } else {
                this.msgError(res.data.message);
              }
            });
          }
        }
      });
    },
  },
  created() {
	this.getList()
    this.getData();
    
  },
};
</script>