<template>
  <div class="app-container">
    <h4 class="form-header h4">学生信息</h4>
    <el-form ref="stuInfoForm" :model="stuInfoForm" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="姓名：">
            <el-input v-model="stuInfoForm.studentName" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="出生日期：">
            <el-date-picker
              v-model="stuInfoForm.birthday"
              type="date"
              placeholder=""
              disabled
              style="width: 100%;"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="身份证号：">
            <el-input v-model="stuInfoForm.idCard" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="注册序号：">
            <el-input v-model="stuInfoForm.schoolNo" clearable disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <h4 class="form-header h4">家长信息</h4>
    <el-row v-if="!allowEdit" :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addparentRow">增加
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="editparentRow">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="delparentRow">删除
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="info"-->
      <!--          plain-->
      <!--          icon="el-icon-close"-->
      <!--          size="mini"-->
      <!--          @click="closePage">关闭-->
      <!--        </el-button>-->
      <!--      </el-col>-->
    </el-row>

    <el-table :data="parentsList" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column type="index" align="center" label="序号"></el-table-column>
      <el-table-column header-align="center" align="center" prop="parentName" label="家长姓名"/>
      <el-table-column header-align="center" align="center" prop="relationship" label="与学生关系">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.relationship" :value="scope.row.relationship"></dict-tag>
        </template>
      </el-table-column>
      <el-table-column header-align="center" align="center" prop="telephone" label="联系方式"/>
      <el-table-column header-align="center" align="center" prop="idCard" label="身份证号"/>
      <el-table-column header-align="center" align="center" prop="workAddress" label="工作地址"/>
      <el-table-column header-align="center" align="center" prop="contactAddress" label="联系地址"/>
      <el-table-column header-align="center" align="center" prop="postalCode" label="邮政编码"/>
    </el-table>
    <!-- 新增家长信息对话框 -->

    <el-dialog :title="title" :visible.sync="openParent" width="800px">
      <el-form ref="parentForm" :model="parentForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="11">
            <el-form-item label="家长姓名：" prop="parentName">
              <el-input v-model.trim="parentForm.parentName" placeholder="请输入家长姓名" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系方式：" prop="telephone">
              <el-input v-model.trim="parentForm.telephone" placeholder="请输入联系方式" clearable></el-input>
            </el-form-item>
            <el-form-item label="联系地址：" prop="contactAddress">
              <el-input v-model.trim="parentForm.contactAddress" placeholder="请输入联系地址" clearable></el-input>
            </el-form-item>
            <el-form-item label="邮政编码：" prop="postalCode">
              <el-input v-model.trim="parentForm.postalCode" placeholder="请输入邮政编码" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="与学生关系：" prop="relationship">
              <el-select v-model="parentForm.relationship" placeholder="请选择与学生关系" style="width: 100%;">
                <el-option
                  v-for="item in dict.type.relationship"
                  :key="item.value"
                  :value="item.value"
                  :label="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="身份证号：" prop="idCard">
              <el-input v-model.trim="parentForm.idCard" placeholder="请输入身份证号" clearable></el-input>
            </el-form-item>
            <el-form-item label="工作地址：" prop="workAddress">
              <el-input v-model.trim="parentForm.workAddress" placeholder="请输入工作地址" clearable></el-input>
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <div slot="footer">
        <el-button type="primary" @click="submitparentForm">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getStudent,
  getParentsList,
  addParent,
  editParent,
  getParentDetail,
  delParent
} from "@/api/smartSchool/studentManage/index";

export default {
  name: "",
  dicts: ["Grade", "Class", "relationship"],
  data() {
    return {
      allowEdit: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 学生id
      studentId: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //   标题
      title: "",
      // 增加家长信息列表
      parentsList: [],
      //   学生信息表单
      stuInfoForm: {},
      // 新增家长信息对话框
      openParent: false,
      // 新增家长信息表单
      parentForm: {},
      // 表单校验
      rules: {
        parentName: [{required: true, message: "家长姓名不能为空", trigger: "blur"}],
        telephone: [{required: true, message: "联系方式不能为空", trigger: "blur"}],
        relationship: [{required: true, message: "与学生关系不能为空", trigger: "blur"}],
        contactAddress: [{required: true, message: "联系地址不能为空", trigger: "blur"}],
        // idCard: [{required: true, message: "身份证号不能为空", trigger: "blur"}],
      }
    };
  },
  watch: {},
  computed: {},
  methods: {
    // 获取学生个人信息
    getStuInfo() {
      const studentId = this.$route.query.studentId;
      getStudent(studentId).then((response) => {
        this.stuInfoForm = response.data;
      });
    },
    // 获取家长信息列表
    getList() {
      const studentId = this.$route.query.studentId;
      getParentsList({studentId}).then((response) => {
        this.parentsList = response.rows;
        this.dialogVisibleForClass = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.studentId = selection.map((item) => item.studentId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 增加家长信息记录
    addparentRow() {
      this.reset();
      this.title = "添加家长信息";
      this.openParent = true;
    },
    // 编辑学生个人家长详情
    editparentRow() {
      this.reset();
      const id = this.ids;
      getParentDetail(id).then(response => {
        this.parentForm = response.data;
        this.openParent = true;
        this.title = "修改家长信息";
      })
    },
    // 删除家长信息记录
    delparentRow() {
      const id = this.ids || [];
      if (id.length > 0) {
        this.$modal.confirm(`确认删除选中的${id.length}条数据？`).then(() => {
          this.$modal.loading('正在处理数据，请稍等...');
          return delParent(id);
        }).then(() => {
          this.$modal.closeLoading();
          this.$modal.msgSuccess("删除成功");
          this.getList();
        }).catch(() => {
          this.$modal.closeLoading();
        });
      }
    },
    // 添加家长信息确认提交
    submitparentForm() {
      this.$refs.parentForm.validate(valid => {
        this.parentForm.studentId = this.$route.query.studentId;
        if (valid) {
          this.$modal.loading('正在上传数据，请稍等...');
          if (this.parentForm.id != undefined) {
            editParent(this.parentForm).then((response) => {
              this.$modal.closeLoading();
              this.$modal.msgSuccess("修改成功");
              this.handleClose();
              this.getList();
            }).catch(error => {
              this.$modal.closeLoading();
            })
          } else {
            // this.parentForm.studentId = this.$route.query.studentId;
            addParent(this.parentForm).then((response) => {
              this.$modal.closeLoading();
              this.$modal.msgSuccess("新增成功");
              this.handleClose();
              this.getList();
            }).catch(error => {
              this.$modal.closeLoading();
            })
          }
        }
      })
    },

    // 关闭对话框
    handleClose() {
      this.reset();
      this.openParent = false;
    },
    // 重置表单
    reset() {
      this.parentForm = {
        studentId: '',
        parentName: '',
        telephone: '',
        relationship: ''
      };
      this.resetForm('parentForm');
    },
  },
  created() {
    this.allowEdit = this.$route.query.allowEdit == 'true' ? true : false
    this.getStuInfo();
    this.getList();
  },
  activated() {
    this.allowEdit = this.$route.query.allowEdit == 'true' ? true : false
    this.getStuInfo();
    this.getList();
  }


};
</script>
<style scoped lang="less">
</style>
