<!--问卷配置-->
<template>
  <div class="app-container">
    <el-form ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="问题" prop="question">
        <el-input v-model="queryParams.question"/>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" clearable>
          <el-option v-for="type in typeList" :label="type" :value="type"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否必填" prop="isRequired">
        <el-select v-model="queryParams.isRequired" clearable>
          <el-option :value="true" label="是"/>
          <el-option :value="false" label="否"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd">新增
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleQuestionnaire">填写问卷
        </el-button>
      </el-col>-->
    </el-row>
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" type="index"/>
      <el-table-column label="问题" prop="question"/>
      <el-table-column label="类型" prop="type"/>
      <el-table-column label="是否必填">
        <template slot-scope="scope">
          {{ scope.row.isRequired ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" prop="orderNum"/>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row.id)">修改</el-button>
          <el-button type="text" @click="handleRemove(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" append-to-body width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问题" prop="question">
          <el-input v-model="form.question"/>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-select v-model="form.type" style="width: 100%">
            <el-option v-for="type in typeList" :label="type" :value="type"/>
          </el-select>
        </el-form-item>
        <el-form-item label="是否必填" prop="isRequired">
          <el-select v-model="form.isRequired" style="width: 100%">
            <el-option :value="true" label="是"/>
            <el-option :value="false" label="否"/>
          </el-select>
        </el-form-item>
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" style="width: 100%"/>
        </el-form-item>
        <div v-if="form.type === '单选'||form.type === '多选'">
          <el-form-item label="选项" prop="orderNum">
            <el-button icon="el-icon-plus"
                       plain
                       size="mini"
                       type="primary"
                       @click="handleOptionAdd">新增
            </el-button>
            <el-table :data="form.optionList">
              <el-table-column label="序号" width="50">
                <template slot-scope="scope">
                  {{ numberToLetter(scope.$index) }}
                </template>
              </el-table-column>
              <el-table-column label="选项">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.option"/>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="50">
                <template slot-scope="scope">
                  <el-button type="text" @click="handleOptionDelete(scope.$index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="questionnaireOpen" append-to-body title="填写问卷" width="1000px">
      <el-form ref="questionnaireForm" :model="questionnaireForm" label-width="80px">
        <el-form-item v-for="(questionnaire, index) in questionnaireForm.questionnaireList"
                      :label="index + 1 + '.'"
                      :prop="'questionnaireList.' + index + '.result'"
                      :rules="{required: questionnaire.isRequired, message: '问题未提交', trigger: 'blur'}">
          <b>{{ questionnaire.question }}</b>
          <div v-if="questionnaire.type === '单选'">
            <el-radio-group v-model="questionnaire.result">
              <el-row v-for="(option, optionIndex) in questionnaire.optionList" :gutter="10">
                <el-radio :label="option.option">
                  {{ numberToLetter(optionIndex) + '：' + option.option }}
                </el-radio>
              </el-row>
            </el-radio-group>
          </div>
          <div v-if="questionnaire.type === '多选'">
            <el-checkbox-group v-model="questionnaire.result">
              <el-row v-for="(option, optionIndex) in questionnaire.optionList" :gutter="10">
                <el-checkbox :label="option.option">
                  {{ numberToLetter(optionIndex) + '：' + option.option }}
                </el-checkbox>
              </el-row>
            </el-checkbox-group>
          </div>
          <div v-if="questionnaire.type === '日期'">
            <el-date-picker v-model="questionnaire.result" value-format="yyyy-MM-dd"></el-date-picker>
          </div>
          <div v-if="questionnaire.type === '地点'">
            <el-cascader
              v-model="questionnaire.result"
              :options="pcaTextArr"
              filterable
              size="large">
            </el-cascader>
          </div>
          <div v-if="questionnaire.type === '打分'">
            <el-rate v-model="questionnaire.result" :max="10"></el-rate>
          </div>
          <div v-if="questionnaire.type === '文本'">
            <el-input v-model="questionnaire.result"></el-input>
          </div>
          <div v-if="questionnaire.type === '富文本'">
            <el-input v-model="questionnaire.result" type="textarea"></el-input>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitQuestionnaire">确 定</el-button>
        <el-button @click="questionnaireOpen = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import {add, detail, edit, getQuestionnaire, page, remove, submitQuestionnaire} from '@/api/liandao/questionnaire.js'
import {pcaTextArr} from 'element-china-area-data'

export default {
  name: "QuestionnaireConfig",
  created() {
    this.getList()
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        question: '',
        type: '',
        isRequired: undefined,
      },
      //表格数据
      tableData: [],
      // 总数
      total: 0,
      // 表单数据
      form: {},
      // 弹出层标题
      title: '',
      open: false,
      rules: {
        question: [{required: true, message: "问题不能为空", trigger: "blur"},],
        type: [{required: true, message: "类型不能为空", trigger: "blur"},],
        isRequired: [{required: true, message: "是否必填不能为空", trigger: "blur"},],
        orderNum: [{required: true, message: "显示顺序不能为空", trigger: "blur"},],
      },
      typeList: ['单选', '多选', '日期', '地点', '打分', '文本', '富文本'],
      questionnaireForm: {
        questionnaireList: []
      },
      questionnaireOpen: false,
      pcaTextArr,
    }
  },
  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      page(this.queryParams).then(resp => {
          if (resp.code == 200) {
            this.tableData = resp.rows;
            this.total = resp.total;
            this.loading = false;
          }
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        question: undefined,
        type: undefined,
        isRequired: false,
        orderNum: undefined,
        optionList: [],
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleEdit(id) {
      this.reset();
      detail(id).then(resp => {
        if (resp.code === 200) {
          this.form = {...resp.data}
          this.open = true;
          this.title = "修改";
        }
      })
    },
    handleRemove(id) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        remove(id).then(resp => {
          if (resp.code === 200) {
            this.$message.success('删除成功！')
            this.resetQuery()
          }
        })
      });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            edit(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          } else {
            add(this.form).then(resp => {
              if (resp.code === 200) {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.$message.error(resp.msg);
              }
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    numberToLetter(num) {
      if (num > 25) {
        return num;
      }
      return String.fromCharCode(65 + num);
    },
    handleOptionAdd() {
      this.form.optionList.push({option: ''})
    },
    handleOptionDelete(index) {
      this.form.optionList.splice(index, 1);
    },
    handleQuestionnaire() {
      this.questionnaireForm.questionnaireList = [];
      getQuestionnaire().then(resp => {
        if (resp.code === 200) {
          this.questionnaireForm.questionnaireList = resp.data
          this.questionnaireOpen = true
        }
      })
    },
    submitQuestionnaire() {
      this.$refs["questionnaireForm"].validate(valid => {
        if (valid) {
          this.questionnaireForm.questionnaireList.forEach(data => {
            if (data.optionList) {
              data.optionList = null
            }
            if (data.type === '地点') {
              data.result = data.result.join(',')
            }
          })
          submitQuestionnaire(this.questionnaireForm).then(resp => {
            if (resp.code === 200) {
              this.$message.success("提交成功")
              this.questionnaireOpen = false
            }
          })
        }
      })
    }
  }
}
</script>
