<template>
  <div class="cmain" style="overflow: hidden">
    <div class="fv">
      <div class="f">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="模板名称" prop="title">
            <el-input v-model="form.title" placeholder="请输入标题" clearable />
          </el-form-item>
          <el-form-item label="问题选项" prop="op">
            <div class="mbg">
              <el-form-item :label="'' + (index + 1)" v-for="(item, index) in form.op" :key="item.key" :prop="'op.' + index + '.name'" :rules="rules.dynamic" class="moptions">
                <el-input class="minp" v-model="item.name" placeholder="请输入内容" @input="input()" clearable>
                  <el-select v-model="item.ifnull" slot="prepend" placeholder="是否必填" style="width: 90px">
                    <el-option :label="s" :value="s" v-for="s in nulls" :key="s"></el-option>
                  </el-select>
                  <el-select v-model="item.input" slot="append" placeholder="请选择" style="width: 101px" @change="change($event, item)">
                    <el-option :label="s" :value="s" v-for="s in selects" :key="s"></el-option>
                  </el-select>
                </el-input>
                <!--问题子选项start-->
                <el-form-item v-if="item.input == '单选' || item.input == '多选' || item.input == '判断'" :prop="'op.' + index + '.selects'" :rules="rules.selects" class="selects">
                  <el-tag v-for="(tag, index) in item.selects" :key="index" :closable="item.input != '判断'" @close="tagClose(item, tag)">{{ tag.name }}</el-tag>
                  <el-input class="input-new-tag" v-if="item.show" v-model="item.s_value" :ref="'tag' + index" @keyup.enter.native="confirm(item)" @input="input()" @blur="confirm(item)"></el-input>
                  <el-button v-else class="button-new-tag" v-show="item.input != '判断'" size="small" @click="showInput(item, index)">+ 输入选项</el-button>
                </el-form-item>
                <!--问题子选项end-->
                <div class="mdel" @click="del(item)">移除</div>
                <div class="mcopy" @click="copy(item)">复制</div>
              </el-form-item>
              <div class="addBtn">
                <el-button icon="el-icon-plus" @click="add()">添加内容</el-button>
              </div>
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="f" style="flex: 0.5">
        <div class="div-mc">
          <div class="guise-mobile guise-mobile-2">
            <div class="guise-mobile-show" ref="scrollbar">
              <!--手机展示内容放置区-->
              <div class="mttv">{{ form.title }}</div>
              <el-divider>开始</el-divider>
              <div class="items" v-for="(item, index) in form.op" :key="index">
                <div class="vtitle" style="font-weight: normal; font-size: 14px">
                  <span class="ifnull" v-if="item.ifnull == '必填'">*</span>
                  <span class="index">{{ index + 1 }},</span>
                  <span class="tm">{{ item.name }} ({{ item.input }})</span>
                </div>
                <div class="mts">
                  <input v-if="item.input == '填空'" placeholder="请输入" :disabled="true" />
                  <textarea v-if="item.input == '多行文本'" :disabled="true" placeholder="请输入"></textarea>
                  <input type="number" v-if="item.input == '电话'" placeholder="请输入" :disabled="true" />
                  <div class="mchoice" v-if="item.input == '单选' || item.input == '多选' || item.input == '判断'">
                    <div v-if="item.selects.length <= 4">
                      <div v-for="(op, i) in item.selects" :key="op.name">
                        <div class="op" :class="{ active: op.check }" @click="check(item, op)">{{ op.name }}</div>
                        <!--如果选项要输入显示手动输入框-->
                        <textarea v-if="op.input && op.check" v-model="op.s_value" :placeholder="'请输入' + op.name" style="margin-top: 5px; float: left"></textarea>
                      </div>
                    </div>
                    <div v-else>
                      <span>点击选择</span>
                      <i class="el-icon-caret-bottom icon"></i>
                    </div>
                  </div>
                  <div v-if="item.input == '附件'"><el-button style="width: 100%" icon="el-icon-upload">选择文件</el-button></div>
                </div>
              </div>
              <el-divider>结束</el-divider>
              <el-button type="primary" style="width: 100%; background-color: #4581fb; border: 0px">提交</el-button>
              <!--手机展示内容放置区-->
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: { tag: 1, state: 0, op: [] },
      nulls: ['必填', '非必填'],
      selects: ['填空', '单选', '多选', '判断', '附件', '多行文本', '电话'],
      rules: {
        title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
        op: [{ required: true, message: '内容不能为空', trigger: 'blur' }],
        dynamic: [
          { required: true, message: '请输入问题名称', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              this.util.checkSame(this.form, value, callback);
            }
          }
        ],
        selects: [{ required: true, message: '请输入问题内容', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      this.ajax({ url: '/work/follow/template/detail/' + this.param.id }).then((response) => {
        response.data.op = JSON.parse(response.data.op);
        this.form = response.data;
      });
    }
  },
  methods: {
    //添加内容
    add() {
      this.form.op.push({ name: '', ifnull: '必填', input: '填空', selects: [], show: false });
      /* this.$nextTick(() => {
        let scrollElem = this.$refs.scrollbar;
        scrollElem.scrollTo({ top: scrollElem.scrollHeight, behavior: 'smooth' });
      }); */
    },
    //删除问题
    del(item) {
      this.form.op.splice(this.form.op.indexOf(item), 1);
    },
    //复制问题
    copy(item) {
      this.form.op.push(JSON.parse(JSON.stringify(item)));
      this.msgSuccess('已复制');
      this.$forceUpdate();
    },
    //判断只能选择是或否
    change(e, item) {
      if (item.input == '判断') {
        item.selects = [{ name: '是' }, { name: '否' }];
      } else {
        item.selects = [];
      }
    },
    //解决input有时无法输入的问题
    input(e) {
      this.$forceUpdate();
    },
    showInput(item, index) {
      item.show = true;
      this.$nextTick(() => {
        this.$refs['tag' + index][0].$refs.input.focus();
      });
      this.$forceUpdate();
    },
    //删除问题子选项
    tagClose(item, tag) {
      item.selects.splice(item.selects.indexOf(tag), 1);
    },
    confirm(item) {
      if (item.s_value) {
        item.selects.push({ name: item.s_value });
        item.selects = this.util.duplicate(item.selects);
      }
      item.show = false;
      item.s_value = '';
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          data.op = JSON.stringify(data.op);
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/follow/template/edit', data: data }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/follow/template/add', data: data }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
