<template>
  <div class="page-index preview">
    <ul>
      <li v-for="(item,index) in editList" :key="index">
        <div class="project" :style="{'margin-left': getLeftWidth(item.level)}">
          <div class="project1" v-if="item.level === -1">
            <div class="projectLabel">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span>{{item.label}}</span>
            </div>
            <div class="projectType">
              <el-input
                type="textarea"
                :rows="2"
                v-model="item.inputDefaultValue"
                :placeholder="item.inputPlaceholderValue"
                size="small"
              ></el-input>
            </div>
          </div>
          <div class="project3">
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[0].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
            </div>
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[1].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
              <div class="project3LeftDes">
                <el-input
                  :placeholder="item.inputPlaceholderValue"
                  v-model="item.inputDefaultValue"
                  size="small"
                ></el-input>
                <span class="project3LeftDepart">{{item.depart}}</span>
              </div>
            </div>
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[2].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
              <el-input
                :placeholder="item.inputPlaceholderValue"
                v-model="item.inputDefaultValue"
                type="textarea"
                :rows="2"
                size="small"
              ></el-input>
            </div>
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[3].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
              <div class="radioChild">
                <el-radio-group
                  v-model="item.radioListValue"
                  @input="()=>onChangeRadioListValue(ind, index)"
                >
                  <el-radio
                    v-for="(con,ind) in item.radioList"
                    :key="ind"
                    :label="con.label"
                  >{{con.label}}</el-radio>
                </el-radio-group>
                <div v-for="(con,ind) in item.radioList.filter(con=>con.radioDefault)" :key="ind">
                  <div v-for="(conn,indd) in con.child" :key="indd">
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[1].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div class="project3LeftDes">
                        <el-input
                          :placeholder="conn.inputPlaceholderValue"
                          v-model="conn.inputDefaultValue"
                          size="small"
                        ></el-input>
                        <span class="project3LeftDepart">{{conn.depart}}</span>
                      </div>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[2].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div class="project3LeftDes">
                        <el-input
                          :placeholder="conn.inputPlaceholderValue"
                          v-model="conn.inputDefaultValue"
                          type="textarea"
                          :rows="2"
                          size="small"
                        ></el-input>
                      </div>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[3].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div>
                        <el-radio-group
                          v-model="conn.radioListValue"
                          @input="()=>onChangeRadioListValue(ind, index, indd)"
                        >
                          <el-radio
                            v-for="(connn,inddd) in conn.radioList"
                            :key="inddd"
                            :label="connn.label"
                          >{{connn.label}}</el-radio>
                        </el-radio-group>
                      </div>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[4].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div>
                        <el-checkbox
                          v-for="(connn,inddd) in conn.radioList"
                          :key="inddd"
                          v-model="conn.radioList[inddd].radioDefault"
                          :label="connn.label"
                          @change="()=>changeCheckbox(ind, index, indd, 'checkbox', inddd)"
                        >{{connn.label}}</el-checkbox>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[4].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
              <div class="radioChild">
                <el-checkbox
                  v-for="(con,ind) in item.radioList"
                  v-model="item.radioList[ind].radioDefault"
                  @change="()=>changeCheckbox(ind, index)"
                  :key="ind"
                >{{con.label}}</el-checkbox>
                <div v-for="(con,ind) in item.radioList.filter(con=>con.radioDefault)" :key="ind">
                  <div v-for="(conn,indd) in con.child" :key="indd">
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[1].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div class="project3LeftDes">
                        <el-input
                          :placeholder="conn.inputPlaceholderValue"
                          v-model="conn.inputDefaultValue"
                          size="small"
                        ></el-input>
                        <span class="project3LeftDepart">{{conn.depart}}</span>
                      </div>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[2].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <el-input
                        :placeholder="conn.inputPlaceholderValue"
                        v-model="conn.inputDefaultValue"
                        type="textarea"
                        :rows="2"
                        size="small"
                      ></el-input>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[3].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div>
                        <el-radio-group
                          v-model="conn.radioListValue"
                          @input="()=>onChangeRadioListValue(ind, index, indd)"
                        >
                          <el-radio
                            v-for="(connn,inddd) in conn.radioList"
                            :key="inddd"
                            :label="connn.label"
                          >{{connn.label}}</el-radio>
                        </el-radio-group>
                      </div>
                    </div>
                    <div class="project3Left" v-if="conn.selectValue === isDoingSelectList[4].type">
                      <span style="color: #f00" v-if="conn.isReq">*</span>
                      <span class="project3LeftName">{{conn.inputValue}}</span>
                      <div>
                        <el-checkbox
                          v-for="(connn,inddd) in conn.radioList"
                          :key="inddd"
                          v-model="conn.radioList[inddd].radioDefault"
                          :label="connn.label"
                          @change="()=>changeCheckbox(ind, index, indd, 'checkbox', inddd)"
                        >{{connn.label}}</el-checkbox>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
            <div class="project3Left" v-if="item.selectValue === isDoingSelectList[5].type">
              <span style="color: #f00" v-if="item.isReq">*</span>
              <span class="project3LeftName">{{item.inputValue}}</span>
              <div style="flex-grow: 1">
                <div>
                  <el-checkbox
                    v-for="(con,ind) in item.radioList"
                    :key="ind"
                    v-model="item.radioList[ind].radioDefault"
                    @change="()=>changeCheckbox(ind, index, null, item)"
                  >{{con.label}}</el-checkbox>
                </div>
                <el-select
                  v-model="item.fillValues"
                  multiple
                  :placeholder="item.inputPlaceholderValue"
                  size="small"
                  popper-class="hiddenPopup"
                  @remove-tag="()=>removeTag(index, item)"
                  @change="()=>changeTagValue(index,item)"
                  allow-create
                  filterable
                  default-first-option
                >
                  <el-option
                    v-for="ele in item.radioList"
                    :key="ele.label"
                    :label="ele.label"
                    :value="ele.label"
                  ></el-option>
                </el-select>
              </div>
            </div>
            <div class="project3Left" v-if="item.selectValue === 'upload'">
              <span class="project3LeftName">{{item.label}}</span>
              <div class="imgs">
                <div v-for="(con,ind) in imageUrl" :key="ind" class="imgsLi">
                  <el-image
                    :preview-src-list="imageUrl"
                    style="width:100px;height:100px"
                    :src="con"
                    class="avatar"
                  />
                  <i class="el-icon-error" @click="()=>deleteImgList(ind)"></i>
                </div>
                <el-upload
                  v-if="item.selectValue === 'upload'"
                  class="avatar-uploader"
                  action="https://jsonplaceholder.typicode.com/posts/"
                  :show-file-list="false"
                  :on-success="handleAvatarSuccess"
                  :before-upload="beforeAvatarUpload"
                >
                  <i class="el-icon-plus avatar-uploader-icon"></i>
                  <div class="el-upload__tip" slot="tip">支持上传jpg/png/jpeg格式文件</div>
                </el-upload>
              </div>
            </div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import { fileUpload } from "@/api/upload.js";
import { formatPicture } from "@/utils/picture";
export default {
  props: {
    editListF: {
      type: Array,
      default: () => {
        return [];
      }
    },
    imageUrl: {
      type: Array,
      default: () => { return []; }
    }
  },
  model: {
    prop: "imageUrl",
    event: "upload",
  },
  created() {
    this.editList = JSON.parse(JSON.stringify(this.editListF));
    if (this.$route.path == "/index/emr") {
      setTimeout(() => {
        this.$parent.isEdit = false;
      }, 500);
    }
  },
  watch: {
    editListF: {
      deep: true,
      handler: function() {
        this.editList = JSON.parse(JSON.stringify(this.editListF));
        this.editList.forEach(item => {
          if (item.level == 1 && item.id == 4) {
            item.inputDefaultValue = "";
          }
        });
      }
    },
    editList: {
      deep: true,
      handler: function() {
        if (this.$route.path == "/index/emr") {
          this.$parent.isEdit = true;
        }
      }
    }
  },
  data() {
    return {
      editList: [
        {
          label: "主诉",
          isReq: true,
          type: "textarea",
          level: 1,
          id: "1",
          child: [],
          selectValue: "textarea",
          inputValue: "主诉",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "现病史",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "2",
          child: [],
          selectValue: "textarea",
          inputValue: "现病史",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "既往史",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "3",
          child: [],
          selectValue: "textarea",
          inputValue: "既往史",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "中医四诊",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "4",
          child: [],
          selectValue: "textarea",
          inputValue: "中医四诊",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "体格检查",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "5",
          child: [],
          selectValue: "textarea",
          inputValue: "体格检查",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "辅助检查",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "6",
          child: [],
          selectValue: "textarea",
          inputValue: "辅助检查",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "报告上传",
          isReq: false,
          type: "upload",
          level: 1,
          id: "7",
          child: [],
          selectValue: "upload",
          inputValue: "",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        }
      ],
      // 模板
      editListInit: [
        {
          label: "主诉",
          isReq: true,
          type: "textarea",
          level: 1,
          id: "1",
          child: [],
          selectValue: "textarea",
          inputValue: "主诉",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "现病史",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "2",
          child: [],
          selectValue: "textarea",
          inputValue: "现病史",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "既往史",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "3",
          child: [],
          selectValue: "textarea",
          inputValue: "既往史",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "中医四诊",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "4",
          child: [],
          selectValue: "textarea",
          inputValue: "中医四诊",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "体格检查",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "5",
          child: [],
          selectValue: "textarea",
          inputValue: "体格检查",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "辅助检查",
          isReq: false,
          type: "textarea",
          level: 1,
          id: "6",
          child: [],
          selectValue: "textarea",
          inputValue: "辅助检查",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        },
        {
          label: "报告上传",
          isReq: false,
          type: "upload",
          level: 1,
          id: "7",
          child: [],
          selectValue: "upload",
          inputValue: "",
          inputDefaultValue: "",
          inputPlaceholderValue: "",
          radioList: [
            {
              label: "",
              radioDefault: false,
              child: []
            }
          ],
          radioListValue: "",
          fillValues: []
        }
      ],
      isDoingSelectList: [
        {
          label: "标题",
          type: "subject"
        },
        {
          label: "单行文本",
          type: "input"
        },
        {
          label: "多行文本",
          type: "textarea"
        },
        {
          label: "单选",
          type: "radio"
        },
        {
          label: "多选",
          type: "checkbox"
        },
        {
          label: "选项填入输入框",
          type: "fill"
        }
      ],
      commonParams: {
        inputValue: "",
        selectValue: "",
        radioList: [
          {
            label: "",
            radioDefault: false,
            child: []
          }
        ],
        radioListValue: "",
        fillValues: []
      },
      dialogVisible: false,
      dialogVisibleConfirm: false,
    };
  },
  methods: {
    async fileUpload(data) {
      let res = await fileUpload(data);
      if (res.ResultCode == 0) {
        this.$message({
          type: "success",
          message: "上传成功",
          showClose: true
        });
        this.$emit('upload', [...this.imageUrl, ...formatPicture(res.ResultInfo),]);
      }
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      console.log(file, "file文件");
      if (
        file.type === "image/jpg" ||
        file.type === "image/png" ||
        file.type === "image/jpeg"
      ) {
        let formData = new FormData();
        formData.append("file", file);
        this.fileUpload(formData);
      } else {
        this.$message.error("文件类型有误");
      }
      return false;
    },
    deleteImgList(ind) {
      const value = this.imageUrl[ind];
      this.$emit('upload', this.imageUrl.filter(url => url !== value ));
    },
    add(type, index, item) {
      const arr = {
        inputValue: "",
        selectValue: "",
        radioList: [
          {
            label: "",
            radioDefault: false,
            child: []
          }
        ],
        radioListValue: "",
        fillValues: []
      };
      const { id, level, father, countId } = item;
      const callback = v => {
        let count = 0;
        let countTemplate = 0;
        this.editList.forEach(ele => {
          if (ele.father === v) {
            count += 1;
            ele.id = `${v}-${count}`;
            ele.countId = count;
          }
        });
        this.editListInit.forEach(ele => {
          if (ele.father === v) {
            countTemplate += 1;
            ele.id = `${v}-${countTemplate}`;
            ele.countId = countTemplate;
          }
        });
      };
      if (type === "inner") {
        this.editList.splice(index + 1, 0, {
          isDoing: true,
          level: level + 1,
          father: id,
          id: `${id}-1`,
          countId: 1,
          ...arr
        });
        this.editListInit.splice(index + 1, 0, {
          isDoing: true,
          level: level + 1,
          father: id,
          id: `${id}-1`,
          countId: 1,
          ...arr
        });
        callback(id);
      } else if (type === "before") {
        this.editList.splice(index, 0, {
          isDoing: true,
          level: level,
          father,
          id: `${father}-${countId}`,
          countId,
          ...arr
        });
        this.editListInit.splice(index, 0, {
          isDoing: true,
          level: level,
          father,
          id: `${father}-${countId}`,
          countId,
          ...arr
        });
        callback(father);
      } else {
        this.editList.splice(index + 1, 0, {
          isDoing: true,
          level: level,
          father,
          id: `${father}-${countId + 1}`,
          countId: countId + 1,
          ...arr
        });
        this.editListInit.splice(index + 1, 0, {
          isDoing: true,
          level: level,
          father,
          id: `${father}-${countId + 1}`,
          countId: countId + 1,
          ...arr
        });
        callback(father);
      }
    },
    getInitFrom(index, arr) {
      // 从表象获取模版
      const arrNew = [...this.editListInit];
      arrNew.forEach((ele, ind) => {
        if (ind === index) {
          // ele = this.editList[index]
          ele.fillValues = arr[index].fillValues || [];
          ele.inputDefaultValue = arr[index].inputDefaultValue;
          ele.inputPlaceholderValue = arr[index].inputPlaceholderValue;
          ele.inputValue = arr[index].inputValue;
          ele.radioList = JSON.parse(
            JSON.stringify(arr[index].radioList || [])
          );
          ele.radioListValue = arr[index].radioListValue || "";
          ele.isReq = arr[index].isReq;
          ele.selectValue = arr[index].selectValue;
          ele.isEditing = arr[index].isEditing;
          ele.isDoing = arr[index].isDoing;
        }
      });
      this.editListInit = arrNew;
    },
    getFromInit(index, arr) {
      // 从模版获取表象
      const arrNew = [...this.editList];
      arrNew.forEach((ele, ind) => {
        if (ind === index) {
          ele.fillValues = arr[index].fillValues || [];
          ele.inputDefaultValue = arr[index].inputDefaultValue;
          ele.inputPlaceholderValue = arr[index].inputPlaceholderValue;
          ele.inputValue = arr[index].inputValue;
          ele.radioList = JSON.parse(
            JSON.stringify(arr[index].radioList || [])
          );
          ele.radioListValue = arr[index].radioListValue || "";
          ele.isReq = arr[index].isReq;
          ele.selectValue = arr[index].selectValue;
          ele.isEditing = true;
          ele.isDoing = true;
        }
      });
      this.editList = arrNew;
    },
    save(v, index) {
      if (!v.inputValue || v.inputValue.trim() === "") {
        this.$message.error("项目名称不能为空");
        return;
      } else if (!v.selectValue || v.selectValue.trim() === "") {
        this.$message.error("备选项形式不能为空");
        return;
      } else if (
        v.selectValue === "radio" ||
        v.selectValue === "checkbox" ||
        v.selectValue === "fill"
      ) {
        let flag = false;
        let flagChild = false;
        v.radioList.forEach(ele => {
          if (ele.label.trim() === "") {
            flag = true;
          }
          if (ele.child) {
            ele.child.forEach(con => {
              if (
                !con.inputValue ||
                con.inputValue.trim() === "" ||
                !con.selectValue ||
                con.selectValue.trim() === ""
              ) {
                flagChild = true;
              }
              if (
                con.selectValue === "radio" ||
                con.selectValue === "checkbox"
              ) {
                con.radioList.forEach(conn => {
                  if (conn.label.trim() === "") {
                    flagChild = true;
                  }
                });
              }
            });
          }
        });
        if (flag) {
          this.$message.error("备选项不能为空");
          return;
        }
        if (flagChild) {
          this.$message.error("子备选项不能为空");
          return;
        }
      }
      const arr = JSON.parse(JSON.stringify([...this.editList]));
      arr.forEach(ele => {
        if (ele.id === v.id) {
          ele.isDoing = false;
          ele.isEditing = false;
          if (v.selectValue === "radio") {
            ele.radioListValue =
              ele.radioList.filter(con => con.radioDefault === "1").length > 0
                ? ele.radioList.filter(con => con.radioDefault === "1")[0].label
                : "";
            ele.radioList.forEach(con => {
              if (con.child && con.child.length > 0) {
                con.child.forEach(conn => {
                  if (conn.selectValue === "radio") {
                    conn.radioListValue =
                      conn.radioList.filter(con => con.radioDefault === "1")
                        .length > 0
                        ? conn.radioList.filter(
                            con => con.radioDefault === "1"
                          )[0].label
                        : "";
                  }
                });
              }
            });
          }
          if (v.selectValue === "checkbox") {
            ele.radioList.forEach(con => {
              if (con.child && con.child.length > 0) {
                con.child.forEach(conn => {
                  if (conn.selectValue === "radio") {
                    conn.radioListValue =
                      conn.radioList.filter(con => con.radioDefault === "1")
                        .length > 0
                        ? conn.radioList.filter(
                            con => con.radioDefault === "1"
                          )[0].label
                        : "";
                  }
                });
              }
            });
          }
          if (v.selectValue === "fill") {
            ele.fillValues = ele.radioList
              .filter(ele => ele.radioDefault)
              .map(ele => ele.label);
          }
        }
      });
      // 赋值模板
      this.getInitFrom(index, arr);
      this.editList = arr;
    },
    cancel(index, item) {
      if (item.isEditing) {
        const arr = JSON.parse(JSON.stringify([...this.editListInit]));
        const arrOld = JSON.parse(JSON.stringify([...this.editList]));
        arrOld.forEach((ele, ind) => {
          if (ind === index) {
            ele.fillValues = arr[index].fillValues || [];
            ele.inputDefaultValue = arr[index].inputDefaultValue;
            ele.inputPlaceholderValue = arr[index].inputPlaceholderValue;
            ele.inputValue = arr[index].inputValue;
            ele.radioList = JSON.parse(
              JSON.stringify(arr[index].radioList || [])
            );
            ele.radioListValue = arr[index].radioListValue || "";
            ele.isReq = arr[index].isReq;
            ele.selectValue = arr[index].selectValue;
            ele.isEditing = false;
            ele.isDoing = false;
          }
        });
        this.editList = [...arrOld];
      } else {
        this.editList.splice(index, 1);
        this.editListInit.splice(index, 1);
      }
    },
    deleteLine(index) {
      this.editList.splice(index, 1);
      this.editListInit.splice(index, 1);
    },
    edit(index) {
      // 赋值表面模板
      const arr = JSON.parse(JSON.stringify([...this.editListInit]));
      this.getFromInit(index, arr);
    },
    onChangeDownTemplate(index, end, endMore) {
      const deleteList = this.editListInit.filter((ele, ind) =>
        endMore === 0 ? ind >= end : ind >= end && ind < endMore
      ); // 需要前置的项
      const arr = [];
      this.editListInit.forEach((ele, ind) => {
        if (ind < end || (endMore !== 0 && ind >= endMore)) {
          arr.push(ele);
        }
      });
      arr.splice(index, 0, ...deleteList);
      this.editListInit = arr;
    },
    down(index, item) {
      let end = 0;
      this.editList.forEach((ele, ind) => {
        if (
          ind > index &&
          ele.level === item.level &&
          end === 0 &&
          ele.father === item.father
        ) {
          end = ind;
        }
      });
      let endMore = 0;
      this.editList.forEach((ele, ind) => {
        if (
          ind > end &&
          ele.level === item.level &&
          endMore === 0 &&
          ele.father === item.father
        ) {
          endMore = ind;
        }
      });
      if (endMore === 0) {
        this.editList.forEach((ele, ind) => {
          if (ind > end && endMore === 0 && ele.level < item.level) {
            endMore = ind;
          }
        });
      }
      const deleteList = this.editList.filter((ele, ind) =>
        endMore === 0 ? ind >= end : ind >= end && ind < endMore
      ); // 需要前置的项
      const arr = [];
      this.editList.forEach((ele, ind) => {
        if (ind < end || (endMore !== 0 && ind >= endMore)) {
          arr.push(ele);
        }
      });
      arr.splice(index, 0, ...deleteList);
      this.editList = arr;
      this.onChangeDownTemplate(index, end, endMore);
    },
    onChangeUpTemplate(index, end, endMore) {
      const deleteList = this.editListInit.filter((ele, ind) =>
        endMore === 0 ? ind >= index : ind >= index && ind < endMore
      ); // 需要前置的项
      const arr = [];
      this.editListInit.forEach((ele, ind) => {
        if (ind < index || (endMore !== 0 && ind >= endMore)) {
          arr.push(ele);
        }
      });
      arr.splice(end, 0, ...deleteList);
      this.editListInit = arr;
    },
    up(index, item) {
      let end = 0;
      this.editList.forEach((ele, ind) => {
        if (
          ind < index &&
          ele.level === item.level &&
          ele.father === item.father
        ) {
          end = ind;
        }
      });
      let endMore = 0;
      this.editList.forEach((ele, ind) => {
        if (
          ind > index &&
          ele.level === item.level &&
          endMore === 0 &&
          ele.father === item.father
        ) {
          endMore = ind;
        }
      });
      if (endMore === 0) {
        this.editList.forEach((ele, ind) => {
          if (ind > index && endMore === 0 && ele.level < item.level) {
            endMore = ind;
          }
        });
      }
      const deleteList = this.editList.filter((ele, ind) =>
        endMore === 0 ? ind >= index : ind >= index && ind < endMore
      ); // 需要前置的项
      const arr = [];
      this.editList.forEach((ele, ind) => {
        if (ind < index || (endMore !== 0 && ind >= endMore)) {
          arr.push(ele);
        }
      });
      arr.splice(end, 0, ...deleteList);
      this.editList = arr;
      this.onChangeUpTemplate(index, end, endMore);
    },
    radioAdd(ind, index, indd, type, inddd) {
      const commonParams = {
        label: "",
        child: [],
        radioDefault: type === "checkbox" ? false : "",
        radioList: [
          {
            label: "",
            radioDefault: false,
            child: []
          }
        ],
        radioListValue: ""
      };
      if (inddd || inddd === 0) {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.forEach((conn, indxxx) => {
                  if (indxxx === indd) {
                    conn.radioList.splice(inddd + 1, 0, {
                      ...commonParams
                    });
                  }
                });
              }
            });
          }
        });
      } else if (indd || indd === 0) {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.splice(indd + 1, 0, {
                  ...commonParams
                });
              }
            });
          }
        });
      } else {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.splice(ind + 1, 0, {
              ...commonParams
            });
          }
        });
      }
    },
    radioAddChild(ind, index) {
      this.editList.forEach((ele, indx) => {
        if (indx === index) {
          ele.radioList.forEach((con, indd) => {
            if (indd === ind) {
              // const arr = {...this.commonParams}
              con.child.unshift({
                inputValue: "",
                selectValue: "",
                radioList: [
                  {
                    label: "",
                    radioDefault: false,
                    child: []
                  }
                ],
                fillValues: []
              });
            }
          });
        }
      });
    },
    radioDelete(ind, index, indd, inddd) {
      if (inddd || inddd === 0) {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.forEach((conn, indxxx) => {
                  if (indxxx === indd) {
                    conn.radioList.splice(inddd, 1);
                  }
                });
              }
            });
          }
        });
      } else if (indd || indd === 0) {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.splice(indd, 1);
              }
            });
          }
        });
      } else {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.splice(ind, 1);
          }
        });
      }
    },
    radioDefaultSelect(ind, index, indd, inddd) {
      if (inddd || inddd === 0) {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.forEach((conn, indxxx) => {
                  if (indxxx === indd) {
                    conn.radioList.forEach((connn, indxxxx) => {
                      if (indxxxx !== inddd) {
                        connn.radioDefault = "";
                      }
                    });
                  }
                });
              }
            });
          }
        });
      } else {
        this.editList.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, inde) => {
              if (inde !== ind) {
                con.radioDefault = "";
              }
            });
          }
        });
      }
    },
    changeCheckbox(ind, index, indd, item, inddd) {
      const arr = JSON.parse(JSON.stringify([...this.editList]));
      if (indd || indd === 0) {
        const flag = this.editList
          .filter((ele, indx) => indx === index)[0]
          .radioList.filter((ele, indx) => indx === ind)[0]
          .child.filter((ele, indx) => indx === indd)[0]
          .radioList.filter((ele, indxx) => indxx === inddd)[0]
          .radioDefaultOther;
        arr.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.forEach((conn, indxxx) => {
                  conn.radioList.forEach((connn, indxxxx) => {
                    if (indxxxx !== inddd && flag) {
                      connn.radioDefault = false;
                    } else if (indxxxx !== inddd && !flag) {
                      connn.radioDefaultOther
                        ? (connn.radioDefault = false)
                        : null;
                    }
                  });
                });
              }
            });
          }
        });
        this.editList = arr;
      } else {
        const flag = this.editList
          .filter((ele, indx) => indx === index)[0]
          .radioList.filter((ele, indx) => indx === ind)[0].radioDefaultOther;
        arr.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx !== ind && flag) {
                con.radioDefault = false;
              } else if (indxx !== ind && !flag) {
                con.radioDefaultOther ? (con.radioDefault = false) : null;
              }
            });
          }
        });
        if (item && item.selectValue === "fill") {
          arr.forEach((ele, indx) => {
            if (indx === index) {
              let labels = ele.radioList.map(elee => {
                return elee.label;
              });
              let valuesSave = JSON.parse(JSON.stringify(ele.fillValues));
              let inputArr = valuesSave.filter(ele1 => {
                return !labels.includes(ele1) ? ele1 : "";
              });
              let arr = ele.radioList
                .filter(ele => ele.radioDefault)
                .map(ele => ele.label);
              let arr1 = new Set([...arr, ...inputArr]);
              ele.fillValues = Array.from(arr1);
            }
          });
        }
        this.editList = arr;
      }
    },
    changeCheckboxOther(v, ind, index, indd, inddd) {
      if (!v) {
        return;
      }
      this.changeCheckbox(ind, index, indd, null, inddd);
    },
    onChangeRadioListValue(ind, index, indd) {
      const arr = JSON.parse(JSON.stringify([...this.editList]));
      if (indd || indd === 0) {
        const radioListValue = this.editList
          .filter((ele, indx) => indx === index)[0]
          .radioList.filter((ele, indx) => indx === ind)[0]
          .child.filter((ele, indx) => indx === indd)[0].radioListValue;
        arr.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (indxx === ind) {
                con.child.forEach((conn, indxxx) => {
                  if (indxxx === indd) {
                    conn.radioList.forEach(connn => {
                      if (radioListValue === connn.label) {
                        connn.radioDefault = "1";
                      } else {
                        connn.radioDefault = "";
                      }
                    });
                  }
                });
              }
            });
          }
        });
        this.editList = arr;
      } else {
        const radioListValue = this.editList.filter(
          (ele, indx) => indx === index
        )[0].radioListValue;
        arr.forEach((ele, indx) => {
          if (indx === index) {
            ele.radioList.forEach((con, indxx) => {
              if (radioListValue === con.label) {
                con.radioDefault = "1";
              } else {
                con.radioDefault = "";
              }
            });
          }
        });
        this.editList = arr;
      }
    },
    removeTag(index, item) {
      this.editList.forEach((ele, ind) => {
        if (ind === index) {
          ele.radioList.forEach(con => {
            if (item.fillValues.includes(con.label)) {
              con.radioDefault = true;
            } else {
              con.radioDefault = false;
            }
          });
        }
      });
    },
    changeTagValue(index, item) {
      this.editList.forEach((ele, ind) => {
        if (ind === index) {
          ele.radioList.forEach(con => {
            if (item.fillValues.includes(con.label)) {
              con.radioDefault = true;
            } else {
              con.radioDefault = false;
            }
          });
        }
      });
    },
    isHasDown(index, item) {
      let endMore = -1;
      this.editList.forEach((ele, ind) => {
        if (
          ind > index &&
          ele.level === item.level &&
          ele.father === item.father
        ) {
          endMore = ind;
        }
      });
      return endMore !== -1;
    },
    isHasUp(index, item) {
      let end = -1;
      this.editList.forEach((ele, ind) => {
        if (
          ind < index &&
          ele.level === item.level &&
          ele.father === item.father
        ) {
          end = ind;
        }
      });
      return end !== -1;
    },
    getLeftWidth(level) {
      return (level - 1) * 40 + "px";
    },
    saveAll() {
      this.$message.success(`提交信息：${JSON.stringify(this.editListInit)}`);
    },
    onChangeDialogVisibleConfirm() {
      this.dialogVisibleConfirm = false;
      this.editList = JSON.parse(JSON.stringify([...this.editListInit]));
      const arr = [];
      this.editList.forEach(ele => {
        if (!ele.isDoing || (ele.isDoing && ele.isEditing)) {
          arr.push(ele);
        }
      });
      this.editList = JSON.parse(JSON.stringify([...arr]));
      this.editListInit = JSON.parse(JSON.stringify([...arr]));
      this.dialogVisible = true;
    },
    seeAll() {
      let flag = false;
      this.editList.forEach(ele => {
        if (ele.isDoing) {
          flag = true;
        }
      });
      this.imageUrl = [];
      if (flag) {
        this.dialogVisibleConfirm = true;
      } else {
        this.editList = JSON.parse(JSON.stringify([...this.editListInit]));
        const arr = [];
        this.editList.forEach(ele => {
          if (!ele.isDoing || (ele.isDoing && ele.isEditing)) {
            arr.push(ele);
          }
        });
        this.editList = arr;
        this.dialogVisible = true;
      }
    },
    handleClose() {
      this.dialogVisible = false;
    }
  }
};
</script>
<style lang="scss" scoped>
.page-index {
  width: 1200px;
  line-height: 32px;
  background-color: #fff;
  margin: auto;

  > ul {
    > li {
      display: flex;
      padding: 10px;

      > div {
        margin: 0 5px;
      }

      .ind {
        width: 40px;
        text-align: center;
      }

      .level {
        width: 40px;
        text-align: center;
      }

      .order {
        width: 40px;
        text-align: center;

        i {
          cursor: pointer;
        }
      }

      .isReq {
        width: 40px;
        text-align: center;
      }

      .project {
        flex-grow: 1;
        display: flex;

        .project1 {
          display: inline-flex;
          width: 100%;

          .projectLabel {
            width: 70px;
            text-align: right;
            padding-right: 10px;
            font-weight: bold;
          }

          .projectType {
            flex-grow: 1;
          }
        }

        .project2 {
          width: 100%;

          .project2Left {
            display: flex;
            margin-bottom: 5px;

            .project2LeftName {
              width: 100px;
              text-align: right;
              padding-right: 10px;
            }

            .radioAdd {
              margin-left: 10px;
              cursor: pointer;

              &:hover {
                color: #5386f6;
              }
            }

            .radioAddChild {
              margin-left: 10px;
              cursor: pointer;

              &:hover {
                color: #5386f6;
              }
            }

            .radioDelete {
              margin-left: 10px;
              cursor: pointer;

              &:hover {
                color: #5386f6;
              }
            }

            .bei {
              display: flex;

              .el-checkbox {
                margin-left: 5px;
              }
            }

            .childBei {
              margin-top: 5px;
              margin-left: 80px;
              border: 1px solid #ccc;

              .el-checkbox {
                margin-left: 5px;
              }

              .project2LeftName {
                width: 105px;
              }
            }

            .el-input {
              width: 200px;
            }

            .el-select {
              width: 200px;
            }

            .el-checkbox {
              display: inline-flex;
              align-items: center;
              margin-right: 5px;
            }

            .el-radio {
              margin: 0 5px;
              align-items: center;
              display: inline-flex;
            }
          }
        }

        .project3 {
          width: 100%;

          .project3Left {
            display: flex;

            .project3LeftName {
              flex-shrink: 0;
              width: 100px;
              text-align: right;
              padding-right: 10px;
            }

            .radioChild {
              flex-grow: 1;

              .project3LeftName {
                width: auto;
              }

              .project3Left {
                margin: 3px 0;
              }
            }

            .project3LeftDes {
              flex-grow: 1;
              display: flex;
              // .el-input {
              //   width: 200px;
              // }
              // .el-textarea {
              //   width: 200px;
              // }
            }

            .project3LeftDepart {
              flex-shrink: 0;
              margin-left: 3px;
            }

            .el-radio-group {
              display: inline-flex;
              align-items: center;

              .el-radio {
                margin-left: 10px;
                margin-right: 0;
              }
            }

            .el-checkbox {
              margin-left: 0;
              margin-right: 10px;
            }

            .el-select {
              width: 100%;
            }
          }
        }
      }

      .action {
        width: 130px;

        span {
          margin: 0 5px;
          cursor: pointer;

          &:hover {
            color: #5386f6;
          }
        }

        .add {
          color: #2c3e50;
          font-size: 16px;
        }
      }
    }
  }

  .disabled {
    color: #c0c4cc;
    cursor: not-allowed !important;

    &:hover {
      color: #c0c4cc !important;
    }
  }

  .btns {
    text-align: center;
    margin: 20px 0;
  }
}
</style>
<style lang="scss">
.hiddenPopup {
  display: none;
}

.project3Left .el-input__suffix {
  display: none !important;
}

.preview {
  width: 100% !important;

  .projectLabel {
    width: unset !important;
  }

  .project3LeftName {
    width: unset !important;
  }
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px !important;
  text-align: center;
}

.avatar-uploader .el-upload__tip {
  margin-top: 0;
}

.avatar {
  width: 100px;
  height: 100px;
  display: block;
}

.imgs {
  display: flex;
}

.imgsLi {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  position: relative;
  width: 100px;
  height: 100px;
  margin-right: 10px;
}

.el-icon-error {
  cursor: pointer;
  position: absolute;
  right: -8px;
  top: -8px;
}
</style>