<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 standout="bg-primary text-white" class="col-6 q-pr-lg q-pr-lg"
            v-model="msg.Title" label="问题名称" maxlength="20" @clear="resetSearch" />
        </div>
        <div class="col-3">
          <q-select filled stack-label class="col-6 q-pr-lg" option-value="Id" option-label="Name"
            v-model="msg.SurveyType" :options="SurveyTypeEnumList" emit-value map-options label="问题类型"
            @input="refreshPage()" />
        </div>
        <div class="col-3">
          <q-select filled stack-label class="col-6 q-pr-lg" option-value="Id" option-label="Name" v-model="msg.IsShow"
            :options="ShowList" emit-value map-options label="是否显示" @input="refreshPage()" />
        </div>

      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-tow-column-table"
        separator="none" :data="data" :columns="columns" row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">问卷调查信息</div>
          <q-space />
          <div class="page-option">
            <q-btn push flat color="accent" class="q-mr-md bg-accent text-white" icon="edit" label="问卷调查二维码" size="sm"
              @click="getStudentSurveyImg()">
              <q-popup-proxy>
                <div style="width:185px;padding:10px;border-radius:8px">
                  <q-img :src="qrCodelUrl">
                    <template v-slot:loading>
                      <div class="text-subtitle1 text-white">
                        Loading...
                      </div>
                    </template>
                  </q-img>
                </div>
              </q-popup-proxy>
            </q-btn>
            <q-btn color="accent" class="q-mr-md" size="sm" icon="add" label="新增" @click="editSurvey(null)" />
          </div>
        </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">
            <q-btn flat size="small" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="editSurvey(props.row)" />
            <q-btn flat size="small" icon="delete" color="negative" style="font-weight:400" label="删除"
              @click="delSurvey(props.row)" />
          </q-td>
        </template>
      </q-table>
      <survey-form v-if="isShowAreaForm" :save-obj="areaObjOption" :MainId="msg.MainId" @close="refreshPage"
        @success="refreshPage">
      </survey-form>
    </div>
  </div>
</template>
<script>
  import {
    querySurveyQuestionPage,
    deleteSurveyQuestion,
    querySurveyTypeEnumList
  } from '../../api/common/common'
  import {
    queryQrCodeImage, //问卷调查二维码生成
  } from "../../api/school/index"
  import surveyForm from '../../components/system/survey-form'
  export default {
    meta: {
      title: "学生问卷调查管理"
    },
    components: {
      surveyForm,
    },
    data() {
      return {
        currentUrl: "",
        columns: [{
            name: 'Title',
            label: '问题名称',
            field: 'Title',
            align: 'left'
          },
          {
            name: 'SurveyTypeStr',
            label: '类型',
            field: 'SurveyTypeStr',
            align: 'left'
          },
          {
            name: 'Sort',
            label: '排序',
            field: 'Sort',
            align: 'left'
          },
          {
            name: 'IsShow',
            required: true,
            label: '是否显示',
            align: 'left',
            field: row => row.IsShow == 1 ? '显示' : "不显示"
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'MenuId'
          }
        ],
        data: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          Title: "",
          SurveyType: -1,
          IsShow: -1,
          MainId: 0,
        },
        pageCount: 0,
        isShowAreaForm: false, //是否显示新增修改地区表单
        areaObjOption: null,
        SurveyTypeEnumList: [], //问题类型
        ShowList: [{
          Id: -1,
          Name: '不限'
        }, {
          Id: 1,
          Name: '显示'
        }, {
          Id: 0,
          Name: '不显示'
        }], //问题类型
        qrCodelUrl: "",
      }
    },
    created() {
      if (this.$route.query) {
        if (this.$route.query.MainId) {
          this.msg.MainId = this.$route.query.MainId;
        }
      }
    },
    mounted() {
      this.currentUrl = this.$route.path
      this.getSurveypage();
      this.getSurveyTypeEnumList();
    },
    methods: {
      //获取类型列表
      getSurveyTypeEnumList() {
        querySurveyTypeEnumList({}).then(res => {
          res.Data.unshift({
            Id: -1,
            Name: "不限"
          });
          this.SurveyTypeEnumList = res.Data;
        })
      },
      //点击生成二维码
      getStudentSurveyImg() {
        if (this.qrCodelUrl === "") {
          let codeMsg = {
            pUrl: "system/surveyStudent",
            StudentId: 0,
            StuName: "",
            MainId: this.msg.MainId,
          }
          queryQrCodeImage(codeMsg).then(res => {
            if (res.Code == 1) {
              this.qrCodelUrl = "data:image/png;base64," + res.Data
              codeMsg = {};
            }
          });
        }
      },
      //删除问卷调查信息
      delSurvey(item) {
        var that = this;
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定删除？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          let delMsg = {
            SurveyID: item.ID,
          }
          deleteSurveyQuestion(delMsg).then(res => {
            if (res.Code == 1) {
              that.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功',
                position: 'top'
              })
              that.refreshPage();
            }
          })
        })
      },
      //刷新页面
      refreshPage() {
        this.isShowAreaForm = false;
        this.areaObjOption = null;
        this.getSurveypage();
      },
      //新增修改学生调查
      editSurvey(obj) {
        if (obj != null) {
          this.areaObjOption = obj;
        }
        this.isShowAreaForm = true;
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getSurveypage();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getSurveypage()
      },
      //获取地区分页列表
      getSurveypage() {
        this.loading = true;
        querySurveyQuestionPage(this.msg).then(res => {
          this.loading = false
          this.data = res.Data.PageData
          this.pageCount = res.Data.PageCount
        }).catch(() => {
          this.loading = false
        })
      },
    }
  }

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

</style>
