<!--
 * @Author: Do not edit
 * @Date: 2023-06-19 14:59:34
 * @LastEditTime: 2023-06-27 15:00:33
 * @LastEditors: DY
 * @Description: 
-->
<template>
  <div v-if="show">
    <h1>模板编辑</h1>
    <div class="addDiv">
      <div style="width: 300px">
        <el-steps direction="vertical" :active="activeStep" space="70px">
          <el-step title="定义"></el-step>
          <el-step title="项目"></el-step>
          <el-step title="排序"></el-step>
          <el-step title="权重&公式"></el-step>
          <el-step title="完成"></el-step>
        </el-steps>
      </div>
      <div style="width: 80vw">
        <el-button v-show="visibleList[0] === false && visibleList[4] !== true" style="margin: -20px 0 10px 0" @click="lastStep">上一步</el-button>
        <div v-if="visibleList[0]" style="width: 60%; border: 1px solid grey; padding: 20px">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="编码" prop="code">
              <el-input v-model="ruleForm.code"></el-input>
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" v-model="ruleForm.remark"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">下一步</el-button>
            </el-form-item>
          </el-form>
        </div>
        <div v-if="visibleList[1]">
          <el-form :inline="true" :model="formInline" class="demo-form-inline" >
            <el-form-item>
              <el-input v-model="formInline.title" size="small" placeholder="标题"></el-input>
            </el-form-item>
            <el-form-item>
              <el-input v-model="formInline.requirement" size="small" placeholder="要求"></el-input>
            </el-form-item>
            <el-form-item>
              <el-select v-model="formInline.projectTypeId" clearable size="small" placeholder="请选择类型">
                <el-option v-for="(item, index) in typeList" :key="index" :label="item.name" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" size="small" @click="getEvaList">查询</el-button>
              <el-button type="primary" size="small" @click="saveData">保存</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="5">
            <el-col :span="18">
              <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                style="width: 80%; min-height: 26vh; overflow-y: auto"
                @selection-change="handleSelectionChange">
                <el-table-column prop="title" label="标题" width="120" />
                <el-table-column prop="projectTypeName" label="类型" width="120" />
                <el-table-column prop="requirement" label="要求" />
                <el-table-column prop="label" label="标签" />
                <el-table-column type="selection" label="全选" width="55" />
              </el-table>
            </el-col>
            <el-col :span="6">
              <div style="margin-top: -30px">
                <p style="font-size: 18px">已选</p>
                <div class="chooseDiv">
                  <div v-for="(item, index) in multipleSelection" :key="index" style="padding: 10px">
                    <span>{{ item.title }}</span>
                    <el-button style="float: right; padding: 3px 0" type="text" icon="el-icon-delete" />
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
        <div v-if="visibleList[2]">
          <div style="min-height: 26vh; overflow: auto">
            <draggable v-model="paramsList" group="projectTypeName" animation="1000" @start="onStart" @end="onEnd1">
              <transition-group>
                <!-- <div v-for="item in paramsList" :key="item.id">
                  {{ item.projectTypeName }}
                </div> -->
                <div class="typeDiv" v-for="element in paramsList" :key="element.projectTypeId">
                  <div slot="header" class="clearfix">
                    <el-card>
                      <span>{{ element.projectTypeName }}</span>
                      <el-button style="float: right; padding: 3px 0" type="text" @click="unfold(element)">子项</el-button>
                    </el-card>
                  </div>
                  <draggable v-show="element.dragVisible" v-model="element.list" group="title" animation="1000" @start="onStart" @end="onEnd2">
                    <transition-group>
                      <div v-for="item in element.list" :key="item.id" style="margin: 5px 15px; border-bottom: 1px solid grey">
                        <p>{{ item.title }}</p>
                      </div>
                    </transition-group>
                  </draggable>
                </div>
              </transition-group>
            </draggable>
          </div>
          <div>
            <el-button type="primary" @click="updateData">更新</el-button>
          </div>
        </div>
        <div v-if="visibleList[3]">
          <el-button type="primary" size="small" @click="finishData">完成</el-button>
          <div>
            <div v-for="(item, index) in paramsList" :key="index" class="projectDiv">
              <p style="width: 100px">{{ item.projectTypeName }}</p>
              <el-input type="number" style="width: 100px" v-model="item.weight" placeholder="权重"></el-input>
              <div  class="smallDiv">
                <div v-for="(it, keyValue) in item.list" :key="keyValue" class="contentDiv">
                  <p style="border: 1px solid grey; width: 80px; text-align: center">{{ it.title }}</p>
                  <el-input type="number" style="width: 100px" v-model="it.benchmarkScore" placeholder="基准分"></el-input>
                </div>
              </div>
              <div>
                <el-input style="width: 200px; margin-right: 20px" v-model="item.calculationScoreFormula" placeholder="计算得分公式"></el-input>
                <el-button @click="updateButton">更新</el-button>
              </div>
            </div>
          </div>
        </div>
        <div v-if="visibleList[4]">
          <el-result icon="success" title="操作成功">
            <template slot="extra">
              <el-button type="primary" size="medium" @click="newModel">新模板</el-button>
              <el-button size="medium" @click="closeModel">关闭</el-button>
            </template>
          </el-result>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import draggable from 'vuedraggable'

export default {
  components: { draggable },
  props: {
    show: Boolean
  },
  data() {
    return {
      urlOptions: {
        addtURL: "/supplier/qmsEvaluationTemplate",
        getEvaluationListURL: '/supplier/qmsEvaluationItemList/page',
        getTypeListURL: '/supplier/qmsProjectType/page',
        saveDataURL: '/supplier/qmsEvaluationTemplateProjectType/saveAll'
      },
      visibleList: [true, false, false, false, false],
      activeStep: 0,
      ruleForm: {
        code: '',
        name: '',
        remark: ''
      },
      tableData: [],
      typeList: [],
      formInline: {
        page: 1,
        limit: 100,
        title: undefined,
        requirement: undefined,
        projectTypeId: undefined
      },
      multipleSelection: [],
      paramsList: [],
      dragVisible: false,
      unfoldId: '',
      rules: {
        code: [
          { required: true, message: '请输入code', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
      }
    }
  },
  mounted() {
    this.ruleForm = {
      code: '',
      name: '',
      remark: ''
    }
  },
  mounted() {
    this.getTypeList()
  },
  methods: {
    closeModel() {
      this.show = false
      this.$emit('info', false)
    },
    newModel() {
      this.activeStep = 0
      // this.visibleList[4] = false
      // this.visibleList[0] = true
      this.$set(this.visibleList, 4, false)
      this.$set(this.visibleList, 0, true)
    },
    updateButton() {
      console.log('啦啦啦', this.paramsList)
    },
    finishData() {
      // 保存数据
      this.$http["post"](this.urlOptions.saveDataURL, this.paramsList)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.$message({
          message: this.$t("prompt.success"),
          type: "success",
          duration: 500
        })
        this.activeStep = 4
        // this.visibleList[3] = false
        // this.visibleList[4] = true
        this.$set(this.visibleList, 3, false)
        this.$set(this.visibleList, 4, true)
      })
    },
    lastStep() {
      console.log('thi', this.activeStep)
      this.paramsList = []
      this.activeStep --
      for (let i = 0; i < this.visibleList.length; i ++) {
        if (i === this.activeStep) {
          this.visibleList[i] = true
        } else {
          this.visibleList[i] = false
        }
      }
      console.log('wokjkj11', this.visibleList)
    },
    updateData() {
      console.log('更新')
      this.$set(this.visibleList, 2, false)
      this.$set(this.visibleList, 3, true)
      console.log('wokjkj', this.visibleList)
    },
    unfold(item) {
      item.dragVisible = !item.dragVisible
      this.unfoldId = item.projectTypeId //项目类型id
      this.paramsList.forEach(it => {
        if (it.projectTypeId === this.unfoldId) {
          this.$set(it, 'draggable', true)
        } else {
          this.$set(it, 'draggable', false)
        }
      })
    },
    onEnd1(event) {
      let oldIndex = event.oldIndex //移动初始位置
      let newIndex = event.newIndex //运动终止位置
      let diff = Math.abs(newIndex - oldIndex) //差值绝对值
      if (eval(oldIndex) > eval(newIndex)) {
        for (let i = 0; i < diff; i ++) {
          this.paramsList[oldIndex - i].sort = oldIndex - i - 1
        }
        this.paramsList[newIndex].sort = oldIndex
      } else {
        for (let i = 0; i < diff; i ++) {
          this.paramsList[oldIndex + i].sort = oldIndex + i + 1
        }
        this.paramsList[newIndex].sort = oldIndex
      }
      console.log('打印一 下', event)
    },
    onEnd2(event) {
      let oldIndex = event.oldIndex //移动初始位置
      let newIndex = event.newIndex //运动终止位置
      let diff = Math.abs(newIndex - oldIndex) //差值绝对值
      let list = []
      this.paramsList.forEach(item => {
        if (item.projectTypeId === this.unfoldId) {
          list = item.list
        }
      })
      if (eval(oldIndex) > eval(newIndex)) {
        for (let i = 0; i < diff; i ++) {
          list[oldIndex - i].sort = oldIndex - i - 1
        }
        list[newIndex].sort = oldIndex
      } else {
        for (let i = 0; i < diff; i ++) {
          list[oldIndex + i].sort = oldIndex + i + 1
        }
        list[newIndex].sort = oldIndex
      }
      console.log('打印一 11111下', this.paramsList)
    },
    onStart() {},
    saveData() {
      console.log('保存，到步骤三', this.multipleSelection)
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择项目!')
      } else {
        const temp = []
        for (let item of this.multipleSelection) {
          temp.push(item.projectTypeName)
        }
        const typeList = Array.from(new Set(temp))
        console.log('aaaaaaa', typeList)
        for (let type of typeList) {
          let obj = {}
          obj.projectTypeName = type
          obj.dragVisible = false
          obj.list = this.multipleSelection.filter(ele => {
            if (ele.projectTypeName === type) {
              obj.projectTypeId = ele.projectTypeId
              return ele
            }
          })
          this.paramsList.push(obj)
        }
        console.log('kjkj', this.paramsList)
        this.activeStep = 2
        // this.visibleList[1] = false
        // this.visibleList[2] = true
        this.$set(this.visibleList, 1, false)
        this.$set(this.visibleList, 2, true)
      }
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      console.log('你好', this.multipleSelection)
    },
    getTypeList() {
      this.paramsList = []
      this.$http
        .get(this.urlOptions.getTypeListURL, {
          params: {
            'limit': 999,
            'page': 1
          },
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.typeList = [];
            return this.$message.error(res.msg);
          }
          this.typeList = res.data.list;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    getEvaList() {
      this.formInline.title = this.formInline.title=== '' ? undefined : this.formInline.title
      this.formInline.requirement = this.formInline.requirement=== '' ? undefined : this.formInline.requirement
      this.formInline.projectTypeId = this.formInline.projectTypeId=== '' ? undefined : this.formInline.projectTypeId
      this.$http
        .get(this.urlOptions.getEvaluationListURL, {
          params: this.formInline
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.tableData = [];
            this.listQuery.total = 0;
            return this.$message.error(res.msg);
          }
          this.tableData = res.data.list;
           this.tableData.map((item) => {
            item.label = ''
            if (item.list.length !== 0) {
              for (it in item.list) {
                item.label += it + ' '
              }
            }
          })
          this.listQuery.total = res.data.total;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http["post"](this.urlOptions.addtURL, this.ruleForm)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                console.log(1111);
                this.$emit("refreshDataList");
              },
            })
            this.activeStep ++
            // this.visibleList[0] = false
            // this.visibleList[1] = true
            this.$set(this.visibleList, 0, false)
            this.$set(this.visibleList, 1, true)
          })
        } else {
          console.log('error submit!!')
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
  .addDiv {
    display: flex;
    justify-content: space-between;
    min-height: 20vh;
  }
  .chooseDiv {
    border: 1px solid grey;
    font-size: 16px;
    /* min-height: 30vh; */
    min-height: 100px;
    overflow-y: auto;
  }
  .typeDiv {
    width: 40%;
    border: 1px solid grey;
    padding: 25px;
    margin-bottom: 10px;
  }
  .projectDiv {
    display: flex;
    justify-content: space-between;
    padding: 10px 40px 0 0;
  }
  .smallDiv {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    /* text-align: center; */
    /* width: 200px; */
  }
  .contentDiv {
    display: flex;
    flex-direction: row;
    width: 200px;
    justify-content: space-between;
    margin-left: 20px;
    text-align: center;
  }
</style>
