<template>
  <div class="questionnaireList">
    <div class="el-card__header">
      <div class="head-title" style="padding: 0;">
        <span @click="CommonJump('activityList')" class="blue point">活动管理</span> / 问卷调查
      </div>
      <div style="display: flex;flex-direction: row;align-items: center">
        <el-button type="primary" class="el-button--small" @click="addRecharge">新增</el-button>
      </div>
    </div>
    <div class="content">
      <div style="display: flex;flex-direction: row;align-items: center">
        <div class="block">
          <div class="searchInput" style="width:250px">
            <el-input style="display:inline-block;width:225px;height:30px"
                      placeholder="请输入问卷调查名称搜索"
                      v-model="msg.Title"
                      size="small"
                      @clear="getList"
                      @keyup.enter.native="getList"
                      clearable>
            </el-input>
            <span @click="getList" class="el-icon-search" style="color:#979dad;font-size:14px;position:relative;top:1px"></span>
          </div>
          <span>类型</span>
          <el-select class="w100" @change="getList()" style="margin-right: 10px;"
                     v-model="msg.SurveyType" size="small" placeholder="请选择">
            <el-option label="不限" :value="0"></el-option>
            <el-option v-for="item in platList" :key="item.Id" :label="item.Name" :value="item.Id">
            </el-option>
          </el-select>
          <span>是否显示</span>
          <el-select class="w100" @change="getList()" style="margin-right: 10px;"
                     v-model="msg.IsShow" size="small" placeholder="请选择">
            <el-option label="不限" value="-1"></el-option>
            <el-option label="是" value="0"></el-option>
            <el-option label="否" value="1"></el-option>

          </el-select>
        </div>
      </div>
    </div>


    <div style="padding: 20px;background: #fff;">
      <el-table
        :data="tableData"
        header-cell-class-name="headClass"
        style="width: 100%"
        border
      >

        <el-table-column
          prop="ID"
          label="ID"
          width="100">
        </el-table-column>

        <el-table-column
          prop="Title"
          label="问题名称">

        </el-table-column>
        <el-table-column
          prop="SurveyType"
          label="类型">
          <template slot-scope="scope">
            <div v-if="scope.row.SurveyType == 1">打分</div>
            <div v-if="scope.row.SurveyType == 2">单选</div>
            <div v-if="scope.row.SurveyType == 3">多选</div>
            <div v-if="scope.row.SurveyType == 4">文本</div>
          </template>

        </el-table-column>
        <el-table-column
          prop="FormTypeStr"
          label="问卷类型">

        </el-table-column>
        <el-table-column
          prop="IsRequired"
          label="是否必填">
          <template slot-scope="scope">
            <div v-if="scope.row.IsRequired == 0">不必填</div>
            <div v-if="scope.row.IsRequired == 1">必填</div>

          </template>

        </el-table-column>



        <el-table-column
          label="排序"
          prop="Sort">

        </el-table-column>
        <el-table-column
          label="是否显示"
          prop="PrizeAddress">

          <template slot-scope="scope">
            <div v-if="scope.row.IsShow == 0">显示</div>
            <div v-if="scope.row.IsShow == 1">不显示</div>
          </template>
        </el-table-column>


        <el-table-column
          fixed="right"
          label="操作"
          width="180"
        >
          <template slot-scope="scope">

            <el-tooltip class="item" effect="dark" content="编辑" placement="top" >
              <img src="../../assets/img/setup/edit.png" alt="" class="imgstyle" @click="Edit(scope.row)">
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top" >
              <img src="../../assets/img/setup/del.png" alt="" class="imgstyle" @click="delete_b(scope.row)">
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination style="text-align:right"
                     background
                     @current-change="handleCurrentChange"
                     :page-size="msg.pageSize"
                     layout="prev, pager, next"
                     :current-page.sync="msg.pageIndex"
                     :total="count">
      </el-pagination>
    </div>
    <el-dialog title="新增/修改问卷调查" :visible.sync="dialogVisible" width="600px">
      <el-form :model="addMsg" :rules="addstoragerules" ref="addMsg">
        <el-form-item label="标题" prop="Title" class="is-required" size="small" label-width="80px">
          <el-input v-model="addMsg.Title" placeholder="请输入标题" class="w400"/>
        </el-form-item>
        <el-form-item label="类型" size="small" class="is-required" label-width="80px">
          <el-select class="w100"
                     v-model="addMsg.SurveyType" size="small" placeholder="请选择" @change="setSurveyOptions">
            <el-option v-for="item in platList" :key="item.Id" :label="item.Name" :value="item.Id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问卷类型" size="small" class="is-required" label-width="80px">
          <el-radio-group v-model="addMsg.FormType">
            <el-radio :label="x.Id" v-for="(x,y) in FormTypeList" :key="y">{{x.Name}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否显示" size="small" class="is-required" label-width="80px">
          <el-radio-group v-model="addMsg.IsShow">
            <el-radio label="0" >是</el-radio>
            <el-radio label="1" >否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否必填" size="small" class="is-required" label-width="80px">
          <el-radio-group v-model="addMsg.IsRequired">
            <el-radio label="1" >必填</el-radio>
            <el-radio label="0" >不必填</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="Sort" class="is-required" size="small" label-width="80px">
          <el-input v-model="addMsg.Sort" placeholder="请输入排序" class="w400"/>
        </el-form-item>
        <template >
          <div class="addListstyle" style="margin-top: 30px" v-if="showoption==true" v-for="(item,index) in addMsg.SurveyOptionsList" :key="index">
            <el-form-item :label="'选项'+(index+1)" prop="Sort" label-width="80px" >
              <el-input v-model="item.OptionsName" placeholder="请输入选项名称" class="w400" size="small">
                <el-button slot="append" icon="el-icon-delete" @click="scList(index)"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="排序" prop="Sort" label-width="80px" >
              <el-input v-model="item.Sort" placeholder="请输入排序" class="w400" size="small"/>
            </el-form-item>

          </div>

        </template>
        <el-form-item v-if="showoption==true" label-width="80px" >
          <el-button type="primary" @click="addList()" size="small">增加</el-button>
        </el-form-item>


      </el-form>
      <el-button type="primary" @click="addstorage('addMsg')" :loading="addloading">保存</el-button>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "questionnaireList",
    data(){
      return{
        msg:{
          pageIndex:1,
          pageSize:20,
          PrizeType:0,
          Title:'',
          IsShow:'-1',
          SurveyType:0,
          ActivitySurveyId:0,//活动id
        },
        tableData:[],
        count:0,
        loading:false,
        platList:[],
        FormTypeList:[],
        dialogVisible:false,
        addMsg:{
          ID:0,
          SurveyType:0,
          Title:'',
          Sort:0,
          IsShow:'0',
          FormType:'1',
          ActivitySurveyId:0,
          SurveyOptionsList:[],
          IsRequired:'0'
        },
        addloading:false,
        addstoragerules: {
          Title: [{
            required: true,
            message: '请输入标题',
            trigger: 'blur'
          }],
          Sort: [{
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }],
        },
        editObj:{},
        showoption:false,
      }
    },
    created(){
      if(this.$route.query.ID){
        this.msg.ActivitySurveyId = this.$route.query.ID
        this.addMsg.ActivitySurveyId = this.$route.query.ID
        this.getDateList()
      }
      this.GetSurveyTypeEnumList()
      this.GetFormTypeEnumEnumList()//问卷类型
    },
    methods:{
      getDateList(){
        this.loading=true;
        this.apipost("/api/Survey/GetPageList", this.msg, res => {
          this.loading=false;
          if(res.data.resultCode==1){
            this.tableData = res.data.data.pageData;
            this.count = res.data.data.count;
          }else {
            this.Info(res.data.message);
          }

        })
      },
      GetSurveyTypeEnumList(){
        this.apipost("/api/Survey/GetSurveyTypeEnumList", {}, res => {
          if(res.data.resultCode==1){
            this.platList = res.data.data
          }else {
            this.Info(res.data.message);
          }

        })
      },
      GetFormTypeEnumEnumList(){
        this.apipost("/api/Survey/GetFormTypeEnumEnumList", {}, res => {
          if(res.data.resultCode==1){
            this.FormTypeList = res.data.data
          }else {
            this.Info(res.data.message);
          }

        })
      },
      addRecharge(){
          this.dialogVisible=true;
          this.addMsg.ID = 0;
          this.addMsg.SurveyType = this.platList[0].Id;
          this.addMsg.Title = '';
          this.addMsg.Sort = 0;
          this.addMsg.IsShow = '0';
          this.addMsg.IsRequired = '0';
          this.addMsg.FormType = '0';
          this.addMsg.SurveyOptionsList = [];
        if(this.addMsg.SurveyType==2 || this.addMsg.SurveyType==3){
          this.showoption=true;
          this.addMsg.SurveyOptionsList= [
            {ID:0,SurveyID:0,OptionsName:'',Sort:0,}
          ]
        }else {
          this.showoption=false;
          this.addMsg.SurveyOptionsList= []
        }

      },

      Edit(row){
        this.apipost("/api/Survey/GetSurvey", {SurveyID:row.ID}, res => {
          if(res.data.resultCode==1){
            this.addMsg = res.data.data;
            this.addMsg.SurveyType = this.addMsg.SurveyType.toString()
            this.addMsg.IsShow = this.addMsg.IsShow.toString()
            this.addMsg.IsRequired = this.addMsg.IsRequired.toString()
            this.addMsg.FormType = this.addMsg.FormType.toString()
            if(this.addMsg.SurveyType==2 || this.addMsg.SurveyType==3){
              this.showoption=true;
            }else {
              this.showoption=false;
            }
            this.dialogVisible=true

          }else {
            this.Info(res.data.message);
          }

        })
      },
      delete_b(row){
        let that=this;
        that.Confirm("是否删除?", function () {
          that.apipost(
            "/api/Survey/DelSurvey",
            {SurveyID:row.ID},
            res => {
              if (res.data.resultCode == 1) {
                that.Success(res.data.message);
                that.getDateList();
              } else {
                that.Error(res.data.message);
              }
            },
          );
        });
      },
      SetUp(){
        this.$router.push('/rechargeSet');
      },
      getList(){
        this.msg.pageIndex = 1
        this.getDateList()
      },
      dianswitch(row){
        this.apipost("/api/Education/UpdatePickupType", {'Id':row.ID}, res => {
          if(res.data.resultCode==1){
            this.Success(res.data.message);
          }else {
            this.Info(res.data.message);
          }

        })
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getDateList();
      },
      addstorage(formName) {
        if(this.addMsg.FormType == 0){
          this.Error('请选择问卷类型')
          return false
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.apipost("/api/Survey/SetSurvey", this.addMsg, res => {
              if (res.data.resultCode == 1) {
                this.getList()
                this.dialogVisible=false
                this.Success(res.data.message);
              } else {
                this.Error(res.data.message);
              }
            })

          } else {
            return false;
          }
        });
      },
      setSurveyOptions(){
        if(this.addMsg.SurveyType==2 || this.addMsg.SurveyType==3){
          this.showoption=true;
          this.addMsg.SurveyOptionsList= [
            {ID:0,SurveyID:0,OptionsName:'',Sort:0,}
          ]
        }else {
          this.showoption=false;
          this.addMsg.SurveyOptionsList=[]
        };

      },
      addList(){
        this.addMsg.SurveyOptionsList.push({ID:0,SurveyID:0,OptionsName:'',Sort:0,})
      },
      scList(index){
        this.addMsg.SurveyOptionsList.splice(index,1)
      }
    },
  }
</script>

<style >
  .questionnaireList .el-card__header{
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    background: #fff;

  }
  .questionnaireList .el-button--small{
    padding: 9px 15px;
  }
  .questionnaireList .content .searchInput{
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    margin-left: 20px;
  }
  .questionnaireList .content .searchInput .el-input__inner{
    border:none;outline:none;
    height: 30px;
    line-height: 30px;
  }
  .questionnaireList .content .searchInput{
    line-height: normal;
    display: inline-table;
    border-collapse: separate;
    border-spacing: 0;
    width:250px;
  }
  .questionnaireList .content{
    background: #fff;
    margin-top:10px;
    padding: 15px;
    box-sizing: border-box;
  }
  .questionnaireList .app-image{
    background-position: center center;
    width: 50px;
    height: 50px;
    border-radius:0%;
    float: left;
    margin-right: 8px;
  }
  .questionnaireList .addListstyle .el-form-item {
    margin-bottom: 10px;
  }
</style>
