<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable filled v-model="msg.StuName" label="学员名称" maxlength="20" />
        </div>
        <div class="col-3">
          <q-input @input="resetSearch" clearable filled v-model="msg.StuTel" label="学员电话" maxlength="20" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" clearable filled stack-label option-value="SId" option-label="SName"
            v-model="schoolTemp" ref="School_Id" :options="schoolList" label="所属校区" :dense="false" />
        </div>
        <div class="col-3">
          <q-select @input="resetSearch" filled v-model="msg.Status" :options="AuditOpts"
            emit-value map-options label="状态" />
        </div>
      </div>
      <div class="page-option">
        <q-btn color="accent" class="q-mr-md" icon="add" label="新增学员" @click="EditStudent(null)" />
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-right-column-table sticky-tow-column-table" separator="none" title="学员信息" :data="data"
        :columns="columns" row-key="name">
        <template v-slot:body-cell-StuIcon="props">
          <q-td auto-width :props="props">
            <q-avatar size="md" v-if="props.value">
              <img :src="props.value" />
            </q-avatar>
            <q-avatar size="md" color="teal-10" text-color="white" v-if="!props.value">
              {{props.row.StuName}}</q-avatar>
          </q-td>
        </template>
        <template v-slot:body-cell-StuName="props">
          <q-td :props="props">
            <div class="text-blue cursor-pointer">{{props.value}}</div>
          </q-td>
        </template>
        <template v-slot:body-cell-StuSex="props">
          <q-td :props="props">
            <q-badge :color="props.value==1?'negative':'primary'" :label="props.value==1?'女':'男'" />
          </q-td>
        </template>
        <template v-slot:body-cell-Status="props">
          <q-td :props="props">
            <q-toggle size="md" color="primary" :false-value="1" :true-value="0" v-model="props.row.Status"
              title="注意：关闭后，学员将无法正常使用." @input="deleteStudentInfo(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td :props="props">
            <div style="min-width:190px;width:100%">
              <q-btn v-if="props.row.StudentAccount==''" flat size="xs" icon="iconfont icon-ziyuan" color="warning"
                style="font-weight:400" class="q-mr-xs" label="创建账号" @click="createAccount(props.row.StuId)" />
              <q-btn flat size="xs" icon="iconfont icon-ziyuan" color="warning" style="font-weight:400" class="q-mr-xs"
                label="重置密码" @click="resetPw(props.row.StuId)" />
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="EditStudent(props.row)" />
            </div>
          </q-td>
        </template>
      </q-table>
      <student-form v-if="isShowStuForm" :save-obj="stuOption" @close="closeStuForm" @success="refreshPage">
      </student-form>
    </div>
  </div>
</template>
<script>
  import {
    getStudentPage,
    getSchoolDropdown,
    deleteStudent,
    createStudentAccount
  } from '../../api/school/index'
  import {
    resetPassword
  } from '../../api/users/user.js'
  import studentForm from '../../components/school/student/student-form'
  export default {
    meta: {
      title: "学员管理"
    },
    components: {
      studentForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'StuIcon',
            label: '',
            field: 'StuIcon',
            align: 'left',
          },
          {
            name: 'StuName',
            required: true,
            label: '学员昵称',
            align: 'left',
            field: row => row.StuName
          },
          {
            name: 'StuSex',
            label: '性别',
            field: 'StuSex',
            align: 'left'
          },
          {
            name: 'StuBirthStr',
            label: '出生日期',
            field: 'StuBirthStr',
            align: 'left'
          },
          {
            name: 'StuTel',
            label: '手机号码',
            field: 'StuTel',
            align: 'left'
          },
          {
            name: 'SName',
            label: '所属校区',
            field: 'SName',
            align: 'left',
          },
          {
            name: 'AreaName',
            label: '区域',
            field: 'AreaName',
            align: 'left',
          },
          {
            name: 'Status',
            label: '状态',
            field: 'Status',
            align: 'left',
          },
          {
            name: 'CreateByName',
            label: '创建人',
            align: 'left',
            field: 'CreateByName'
          },
          {
            name: 'CreateTimeStr',
            label: '入学时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'StudentAccount',
            label: '账号',
            align: 'left',
            field: 'StudentAccount'
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'StuId'
          }
        ],
        data: [],
        loading: true,
        AuditOpts: [{
            label: '全部',
            value: '-1'
          },
          {
            label: '正常',
            value: '0'
          },
          {
            label: '禁用',
            value: '1'
          },
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          StuName: "",
          StuTel: "",
          Status: "-1",
          School_Id: 0
        },
        schoolTemp: "",
        pageCount: 0,
        stuOption: null,
        schoolList: [],
        isShowStuForm: false,
        resetLoading: false
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getSchool()
      this.getStudent()
    },
    methods: {
      resetSearch() {
        this.msg.pageIndex = 1;
        if (this.schoolTemp) {
          this.msg.School_Id = this.schoolTemp.SId
        } else {
          this.msg.School_Id = 0
        }
        this.getStudent()
      },
      //创建账号
      createAccount(id) {
        let that = this
        createStudentAccount({
          StuId: id,
        }).then(res => {
          that.$q.dialog({
            title: "创建账号",
            message: res.Message,
            persistent: true,
            ok: {
              label: "确认",
              flat: true,
              focus: true
            }
          }).onOk(() => {
            that.getStudent();
          }).onCancel(() => {
            that.getStudent();
          })
        })
      },
      resetPw(id) {
        let that = this
        this.$q.dialog({
          title: "重置密码",
          message: "你正在进行重置密码行为，是否确认执行",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认重置",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          that.resetLoading = true
          resetPassword({
            AccountType: 4,
            AccountId: id
          }).then(res => {
            that.resetLoading = false
            that.$q.dialog({
              title: "密码重置成功",
              message: res.Message,
              persistent: true,
              ok: {
                label: "确认",
                flat: true,
                focus: true
              }
            })
          }).catch(() => {
            that.resetLoading = false
          });
        });
      },
      deleteStudentInfo(obj) {
        let that = this;
        var str = (obj.Status == 1 ? "【禁用】" : "【启用】");
        this.$q.dialog({
          title: str + "学员",
          message: "你正在进行" + str + "学员行为，一旦执行无法找回，是否确认执行？",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          that.resetLoading = true
          deleteStudent({
            StuId: obj.StuId,
            Status: obj.Status
          }).then(res => {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据操作成功！',
              position: 'top'
            })
            this.getStudent()
          })
        }).onCancel(() => {
          obj.Status = obj.Status == 1 ? 0 : 1;
        });
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.getStudent();
      },
      getSchool() {
        getSchoolDropdown({}).then(res => {
          this.schoolList = res.Data;
        })
      },
      getStudent() {
        this.loading = true;
        getStudentPage(this.msg).then(res => {
          this.loading = false;
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
      refreshPage() {
        if (!this.stuOption) {
          this.msg.pageIndex = 1;
          this.msg.StuName = "";
          this.msg.StuTel = "";
          this.msg.Status = "-1";
          this.msg.School_Id = 0;
        }
        this.getStudent()
      },
      EditStudent(obj) {
        if (obj) {
          this.stuOption = obj;
        } else {
          this.stuOption = null;
        }
        this.isShowStuForm = true;
      },
      closeStuForm() {
        this.isShowStuForm = false;
      }
    },
  }

</script>
<style lang="sass">
  @import url('~assets/css/table.sass')

</style>
