<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 800px;max-width:900px;">
      <q-card-section>
        <div class="text-h6">{{objOption.MenuId==0?'新增老师问卷调查信息':'修改老师问卷调查信息'}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <div class="text-caption q-mb-lg q-px-md text-grey-6">老师问卷调查信息</div>
        <div class="row wrap">
          <q-input filled stack-label maxlength="40" :dense="false" v-model="objOption.Title" ref="Title"
            class="col-6 q-pr-lg q-pb-lg" label="问题名称" :rules="[val => !!val || '请填问题名称']" />
          <q-select filled stack-label class="col-6 q-pr-lg" option-value="Id" option-label="Name"
            v-model="objOption.SurveyType" :options="SurveyTypeEnumList" emit-value map-options label="问题类型"
            @input="changeType()" />
        </div>
        <div class="row wrap">
          <q-field label="是否显示" stack-label filled class="col-6 q-pr-lg q-pb-lg">
            <template v-slot:control>
              <div class="row col-12">
                <q-radio v-model="objOption.IsShow" :val="1" label="显示" size="xs" />
                <q-radio v-model="objOption.IsShow" :val="0" label="不显示" size="xs" />
              </div>
            </template>
          </q-field>
          <q-input filled stack-label maxlength="20" :dense="false" v-model="objOption.Sort"
            class="col-6 q-pr-lg q-pb-lg" label="排序" :rules="[val => !!val || '请输入排序']" />
        </div>

        <template v-if="objOption.SurveyType==2||objOption.SurveyType==3">
          <div class="q-pb-xs">
            增加选项
            <q-btn round dense flat icon="add" @click="addSurveyOptions" />
          </div>
          <div v-for="(sItem,sIndex) in objOption.SurveyOptionsList" :key="sIndex">
            <div class="row wrap">
              <q-input filled class="col-10 q-pr-lg q-pb-lg" v-model="sItem.OptionsName" :label="`选项${sIndex+1}`"
                :dense="false"></q-input>
              <q-input filled stack-label :dense="false" v-model="sItem.Sort" class="col-2 q-pr-lg q-pb-lg" label="排序"
                :rules="[val => !!val || '请输入排序']">
                <template v-slot:append>
                  <q-btn round dense flat icon="delete" @click="delSurveyOptions(sIndex)" />
                </template>
              </q-input>
            </div>
          </div>
        </template>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" :loading="saveLoading"
          @click="setSurvey" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>

<script>
  import {
    number
  } from 'echarts';
  import {
    querySurveyTypeEnumList,
    saveSurveyQuestion,
    querySurveyQuestion
  } from '../../api/common/common'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      },
      MainId: {
        type: number,
        default: 0,
      }
    },
    data() {
      return {
        persistent: true,
        objOption: {
          MainId: 0,
          ID: 0, //编号
          Title: "", //问题名称
          SurveyType: 1, //问题类型见枚举参数
          Sort: 0, //排序
          IsShow: 1, //0-不显示，1-显示
          SurveyOptionsList: [],
        },
        optionTitle: "",
        SurveyTypeEnumList: [], //问题类型
        saveLoading: false,
        SurveyOptionsItem: {
          ID: 0,
          OptionsName: "",
          Sort: 0,
        }
      }
    },

    created() {
      this.getSurveyTypeEnumList();
    },
    mounted() {
      this.initObj()
    },
    methods: {
      delSurveyOptions(index) {
        this.objOption.SurveyOptionsList.splice(index, 1);
        if (this.objOption && this.objOption.SurveyOptionsList && this.objOption.SurveyOptionsList.length > 0) {
          this.objOption.SurveyOptionsList.forEach((item, index) => {
            item.Sort = index + 1;
          });
        }
      },
      addSurveyOptions() {
        this.objOption.SurveyOptionsList.push(JSON.parse(JSON.stringify(this.SurveyOptionsItem)));
        this.objOption.SurveyOptionsList.forEach((item, index) => {
          item.Sort = index + 1;
        });
      },

      //获取类型列表
      getSurveyTypeEnumList() {
        querySurveyTypeEnumList({}).then(res => {
          this.SurveyTypeEnumList = res.Data;
        })
      },
      //类型切换
      changeType() {
        this.objOption.CountryId = "";
        this.objOption.ProviceId = "";
        this.objOption.CityId = "";
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.ID > 0) {
          querySurveyQuestion({
            SurveyID: this.saveObj.ID
          }).then(res => {
            var tempData = res.Data;
            this.objOption.MainId=tempData.MainId;
            this.objOption.ID = tempData.ID;
            this.objOption.Title = tempData.Title;
            this.objOption.SurveyType = tempData.SurveyType;
            this.objOption.Sort = tempData.Sort;
            this.objOption.IsShow = tempData.IsShow;
            this.objOption.SurveyOptionsList = tempData.SurveyOptionsList;
          })
          this.optionTitle = "修改学生意见调查"
        } else {
          this.optionTitle = "新增学生意见调查"
          this.objOption.MainId=this.MainId;
          this.objOption.ID = 0;
          this.objOption.Title = '';
          this.objOption.SurveyType = 1;
          this.objOption.Sort = 0;
          this.objOption.IsShow = 1;
          this.objOption.SurveyOptionsList = [];
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
        this.persistent = false
      },
      //保存菜单
      setSurvey() {
        this.saveLoading = true;
        saveSurveyQuestion(this.objOption).then(res => {
          this.saveLoading = false
          this.$q.notify({
            icon: 'iconfont icon-chenggong',
            color: 'accent',
            timeout: 2000,
            message: '数据保存成功！',
            position: 'top'
          })
          this.$emit("success")
          this.closeSaveForm()
        }).catch(() => {
          this.saveLoading = false
        })
      }

    },

  }

</script>
