<template>
  <div class="cmain" style="overflow: hidden">
    <el-row>
      <el-col :span="16">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="18">
              <el-form-item label="模板名称" prop="title">
                <el-input v-model="form.title" placeholder="请输入标题" clearable />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="状态" prop="state">
                <el-select v-model="form.state" placeholder="请选择">
                  <el-option label="启用" :value="0"></el-option>
                  <el-option label="停用" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <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>
      </el-col>
      <el-col :span="8">
        <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="ops">
                    <div v-for="(op, i) in item.selects" :key="op.name">
                      <div class="op">{{ op.name }}</div>
                    </div>
                  </div>
                </div>
              </div>
              <el-divider>结束</el-divider>
              <el-button type="primary" class="tjiao">提交</el-button>
              <!--手机展示内容放置区-->
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <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: { 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' }]
      }
    };
  },
  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>
