index.vue 6.41 KB
<template>
  <el-scrollbar class="step-box">
    <div class="step-content">
      <el-form
        ref="formRef"
        label-width="154px"
        :rules="rule"
        :model="configForm"
      >
        <el-form-item
          label="问卷标题:"
          prop="title"
          class="m-t-20"
        >
          <el-input
            style="width:432px"
            size="small"
            v-model="configForm.title"
            placeholder="请输入"
            maxlength="8"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="问卷说明:"
          prop="desc"
          class="m-t-20"
        >
          <el-input
            style="width:580px"
            type="textarea"
            rows="3"
            v-model="configForm.desc"
            placeholder="请输入"
            maxlength="50"
            show-word-limit
          ></el-input>
        </el-form-item>
        <el-form-item
          label="题目配置:"
          prop="questions"
          class="m-t-20"
        >
          <div class="config-box">
            <template v-for="(i,index) in questionsList">
              <template>
                <div
                  :key="index"
                  class="item"
                >
                  <div :class="['label',{'required':i.required}]">
                    {{i.text}}:
                  </div>
                  <template v-if="i.type == 1">
                    <el-radio-group
                      v-model="i.itemValue"
                      class="radio"
                    >
                      <template v-for="(item,ind) in i.data">
                        <template>
                          <el-radio
                            :key="ind"
                            :label="item"
                          >{{item}}</el-radio>
                        </template>
                      </template>
                    </el-radio-group>
                  </template>
                  <template v-if="i.type == 2">
                    <el-checkbox-group v-model="i.itemValue">
                      <template v-for="(item,ind) in i.data">
                        <template>
                          <el-checkbox
                            :label="item"
                            :key="ind"
                          ></el-checkbox>
                        </template>
                      </template>
                    </el-checkbox-group>
                  </template>
                  <template v-if="i.type == 3">
                    <el-input
                      style="width:580px"
                      v-model="i.itemValue"
                      placeholder="请输入"
                      type="textarea"
                      :autosize="{ minRows: 3,maxRows:3}"
                      :maxlength="i.data"
                      show-word-limit
                    ></el-input>
                  </template>
                  <template v-if="i.type == 4">
                    <el-select
                      v-model="i.itemValue"
                      placeholder="请选择"
                      style="width:432px;"
                    >
                      <el-option
                        v-for="(item,ind) in i.data"
                        :key="ind"
                        :label="item"
                        :value="item"
                      ></el-option>
                    </el-select>
                  </template>
                  <template v-if="i.type == 5">
                    <el-cascader
                      v-model="i.itemValue"
                      :options="i.data"
                      style="width:432px;"
                    ></el-cascader>
                  </template>
                </div>
              </template>
            </template>
            <el-button
              icon="el-icon-plus"
              size="small"
              @click="showEditQuestions = true"
            >添加题目</el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <edit-questions
      :show.sync="showEditQuestions"
      @addQuestions="addQuestions"
      :userInfoFieldList="userInfoFieldList"
    />
  </el-scrollbar>
</template>

<script type='text/ecmascript-6'>
import EditQuestions from './EditQuestions.vue'
export default {
  name: 'StepTwo',
  components: { EditQuestions },
  data () {
    return {
      configForm: {
        title: '',
        desc: '',
        questions: [],
      },
      rule: {
        title: [{ required: true, message: '请输入问卷标题', trigger: 'change' }],
        desc: [{ required: true, message: '请输入问卷说明', trigger: 'change' }],
        questions: [{ required: true, message: '请添加题目', trigger: 'change' }],
      },
      showEditQuestions: false,
      questionsList: [],
      userInfoFieldList: []
    }
  },
  mounted () {
    this.getUserInfoFieldList()
  },
  methods: {
    formRef () {
      return this.$refs.formRef
    },
    async getUserInfoFieldList () {
      const res = await this.$apis.questionnaire.getUserInfoFieldList()
      this.userInfoFieldList = res || []
    },
    addQuestions (params) {
      this.questionsList.push(params)
      const obj = {
        type: params.type,
        required: params.required,
        text: params.text,
        userInfoField: params.userInfoField
      }
      if (params.type == 3) obj.maxLength = params.data
      else obj.option = params.option
      this.configForm.questions.push(obj)
      this.formRef().validateField('questions')
    }
  }
}
</script>

<style scoped lang='scss'>
.step-box {
  width: 100%;
  height: 100%;
  box-sizing: border-box;

  /deep/.el-scrollbar__wrap {
    &::-webkit-scrollbar {
      width: 0;
      height: 0;
      color: transparent;
    }
    .el-form-item__content {
      width: calc(100% - 154px);
    }
  }
  .step-content {
    padding-top: 32px;
  }
  .config-box {
    width: 100%;
  }
  .item {
    width: 870px;
    background: #f9f9f9;
    display: flex;
    margin-bottom: 16px;
    padding: 20px;
    .label {
      font-size: 14px;
      color: #333333;
      width: 154px;
      display: flex;
      justify-content: flex-end;
      flex-shrink: 0;
    }
    /deep/.el-radio__label {
      word-break: break-all;
      white-space: break-spaces;
    }
    .required {
      &::before {
        content: '*';
        color: #eb4e4e;
        display: block;
        margin: 2px 4px 0 0;
      }
    }
    .radio {
      line-height: 22px;
      font-size: 40px;
    }
  }
}
</style>