<template>
  <div>
    <paymentCycle
      ref="cycle"
      :form="cycle"
      :isUserType="true"
      :isCommon="true"
      :isDisabled="true"
      :hideMoney="true"
    />
    <otherform :form="other" ref="other" />
    <el-table
      :data="mergeInfoList"
      :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
      max-height="300px"
    >
      <el-table-column
        prop="userId"
        align="center"
        width="120"
        label="学员编号"
      ></el-table-column>
      <el-table-column
        prop="username"
        align="center"
        width="120"
        label="学员姓名"
      ></el-table-column>
      <el-table-column prop="phone" align="center" width="120" label="课程类型">
        <template slot-scope="scope">
          <div>
            {{ scope.row.courseType | coursesType }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="subjectNames" align="center" label="原价">
        <template slot-scope="scope">
          <div>
            <el-input v-model="scope.row.courseOriginalPrice"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="现价">
        <template slot-scope="scope">
          <div>
            <el-input v-model="scope.row.courseCurrentPrice"></el-input>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import paymentCycle from "@/views/resetTeaming/modals/payment-cycle";
import otherform from "@/views/resetTeaming/modals/other";
import { getTimes } from "@/utils";
import { spanGroupMergeClassSplitClassAffirm } from "@/api/buildTeam";
export default {
  props: [
    "studentIds",
    "masterClassGroupId",
    "classGroupStudents",
    "classList",
    "mergeInfo",
  ],
  components: {
    paymentCycle,
    otherform,
  },
  data() {
    return {
      activeNames: [],
      mergeInfoList: [],
      cycle: {
        paymentAmount: null,
        paymentPattern: null,
      },
      other: {},
    };
  },
  mounted() {
    // console.log(this.studentIds, this.masterClassGroupId, this.classGroupStudents)
    // for (let item in this.mergeInfo) {
    //   this.activeNames.push(item);
    // }
    this.mergeInfoList = [];
    for (let merge in this.mergeInfo) {
      this.mergeInfoList = this.mergeInfoList.concat(this.mergeInfo[merge]);
    }
  },
  methods: {
    getCLassName(key) {
      let str = "";
      this.classList.forEach((classes) => {
        if (classes.id == key) {
          str = classes.name;
        }
      });
      return str;
    },
    async submit() {
      let obj = {};
      let some = this.getData()
      if(some){
        obj.musicGroupPaymentCalenderDtos = [some];

      }else{
        return
      }

      obj.classGroupIds = this.classList.map((classes) => {
        return classes.id;
      });
      obj.masterClassGroupId = this.masterClassGroupId;
      obj.classGroupStudents = this.classGroupStudents;
      obj.studentIds = this.studentIds
      // console.log(obj);
      try {
        const reset = await spanGroupMergeClassSplitClassAffirm(obj);
        this.$message.success("合并成功");
        // 1.关闭弹窗
        // 2.清空合并班
        // 3.刷新列表
        this.$emit('refresh')
        console.log(reset);
      } catch (e) {
        console.log(e);
      }
    },
    getForms() {
      const { $refs: refs } = this;
      // [refs.base, refs.eclass, refs.cycle, ...(refs.cycles || []), refs.other, refs.payment]
      return [refs.cycle, refs.other]
        .filter((item) => !!item)
        .map((item) => item.$refs.form || item);
    },
    getData() {
      const forms = this.getForms();
      const valided = [];
      for (const form of forms) {
        form.validate((valid) => {
          if (valid) {
            valided.push(form);
          }
        });
      }
      if (valided.length === forms.length) {
        const { leixing, ...rest } = {
          ...this.form,
          ...this.other,
        };

        if (this.$refs.cycle) {
          const {
            paymentDate,
            paymentValid,
            paymentPattern,
            ...other
          } = this.cycle;
          rest.paymentPattern = paymentPattern;
          rest.payUserType = "STUDENT";
          rest.paymentType = "SPAN_GROUP_CLASS_ADJUST";
          rest.musicGroupPaymentCalenderStudentDetails = this.mergeInfoList;
          rest.musicGroupPaymentDateRangeList = [
            {
              ...other,
              ...getTimes(paymentDate, [
                "startPaymentDate",
                "deadlinePaymentDate",
              ]),
              ...getTimes(paymentValid, [
                "paymentValidStartDate",
                "paymentValidEndDate",
              ]),
            },
          ];
        }
        const data = {
          ...rest,
        };
        return data;

        // 说明验证通过
      } else {
        this.$message.error("请填写必要信息");
        return "error";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/ .header {
  display: flex;
  align-items: center;
  width: 100%;
  justify-content: space-between;
  // margin-bottom: 10px;
  > span:first-child {
    display: flex;
    &::before {
      content: "";
      display: block;
      width: 5px;
      background-color: #14928a;
      margin-right: 10px;
      border-radius: 2px;
      height: 48px;
    }
  }
  .icon {
    font-size: 18px;
    font-weight: normal;
    margin-right: 20px;
  }
  /deep/.el-collapse-item__wrap {
    border-bottom: none !important;
  }
}
</style>
