<template>
  <div>
    <el-table :data='activeSoundList'
              :header-cell-style="{background:'#EDEEF0',color:'#444'}">
      <el-table-column align='center'
                       width="180px;"
                       prop="sound"
                       label="声部名称">
      </el-table-column>
      <el-table-column align='center'
                       width="180px;"
                       prop="yuji"
                       label="预计收费">
        <template slot-scope="scope">
          <div>
            <el-input style="width:100px"
                      v-model="scope.row.yuji"></el-input>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="可选乐器规格"
                       align="center"
                       prop="zhonglei"
                       width="300">
        <template slot-scope="scope">
          <!-- 'typeVisible':false,'provideVisible':false,'markVisible':false -->
          <div>
            <span>{{scope.row.zhonglei | zhongleiFilter(scope.row.goodsList)}}</span>
            <el-popover placement="bottom"
                        @show='chioseType(scope.row)'
                        v-model="scope.row.typeVisible"
                        v-if='scope.row.type == 1'>
              <el-button type="text"
                         slot='reference'>
                修改
              </el-button>
              <!-- zhonglei -->
              <el-select v-model="scope.row.zhonglei"
                         multiple
                         clearable
                         collapse-tags>
                <el-option v-for="(item,index) in scope.row.goodsList"
                           :key='index'
                           :label="item.name"
                           :value="item.id"></el-option>
              </el-select>
              <div style="text-align: right; margin-top: 20px">
                <!-- <el-button size="mini"
                             type="text"
                             @click="closechioseType(scope.row)">取消</el-button> -->
                <el-button type="primary"
                           size="mini"
                           @click="scope.row.typeVisible  = false">确定</el-button>
              </div>
            </el-popover>

          </div>
        </template>

      </el-table-column>
      <el-table-column label="乐器提供方式"
                       align="center"
                       prop="fangshi"
                       width="180">
        <template slot-scope="scope">
          <div>
            <!-- <div class="chiose"
                  >选择</div> -->
            <span v-if="scope.row.fangshi">{{scope.row.fangshi | fangshiFilter(scope.row.fangshiprice)}}</span>
            <el-popover placement="bottom"
                        v-model="scope.row.provideVisible"
                        v-if='scope.row.type == 1'>
              <i class='el-icon-edit'
                 slot='reference'>
              </i>

              <el-select v-model="scope.row.fangshi"
                         clearable>
                <el-option label="团购"
                           value="GROUP"></el-option>
                <el-option label="借用"
                           value="LEASE"></el-option>
                <el-option label="免费"
                           value="FREE"></el-option>

              </el-select>
              <el-input placeholder="请输入费用"
                        v-if='scope.row.fangshi == "LEASE"'
                        v-model="scope.row.fangshiprice"
                        style='width:180px; margin-top:20px'></el-input>
              <div style="text-align: right; margin-top: 20px">
                <!-- <el-button size="mini"
                             type="text"
                             @click="scope.row.provideVisible = false">取消</el-button> -->
                <el-button type="primary"
                           size="mini"
                           @click="scope.row.provideVisible  = false">确定</el-button>
              </div>
            </el-popover>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="教辅组合"
                       align="center"
                       prop="jiaopu">
        <template slot-scope="scope">
          <div>
            <el-dialog :visible.sync="scope.row.markVisible"
                       style="text-align:left;"
                       width="60%">
              <el-button type='text'
                         @click="addMark(scope.row)">添加教辅</el-button>
              <div class="markWrap">
                <div class="markItem"
                     v-for="(item, index) in scope.row.markList"
                     :key='index'>
                  <el-form :model="item"
                           :inline="true">
                    <el-form-item label="教辅名称">
                      <el-input type="text"
                                v-model="item.name"></el-input>
                    </el-form-item>
                    <el-form-item label="教辅商品">
                      <el-select v-model="item.goods"
                                 multiple
                                 collapse-tags>
                        <el-option v-for='(item,index) in scope.row.markChioseList'
                                   :key='index'
                                   :label="item.name"
                                   :value="item.id"></el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="教辅打包价">
                      <el-input type='number'
                                v-model="item.price"></el-input>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="danger"
                                 icon="el-icon-delete"
                                 circle
                                 @click='removeMartItem(scope.row,index)'>
                      </el-button>
                    </el-form-item>
                  </el-form>
                </div>
              </div>
              <span slot="footer"
                    class="dialog-footer">
                <!-- <el-button @click="scope.row.markVisible = false">取 消</el-button> -->
                <el-button type="primary"
                           @click="scope.row.markVisible = false">确 定</el-button>
              </span>
            </el-dialog>
            <span>
              <span v-for='(item,index) in scope.row.markList'
                    v-if="item.name && scope.row.markChioseList.length>0"
                    :key='index'>{{item.name+':'}}{{item.goods|goodsFilter(scope.row.markChioseList)}}{{' 价格 '+item.price }} </span>
            </span>
            <i class='el-icon-edit'
               slot='reference'
               @click="chioseMark(scope.row)">
            </i>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="btnWrap"
         style="margin-top:30px">
      <div class="closeBtn"
           @click="getSound">取消</div>
      <div class="okBtn"
           @click="saveInfo">保存</div>
    </div>
  </div>
</template>
<script>
import { findMusicGroupSubjectInfo, resetSubjectPlan, getSoundTree, getGoods, updateSubjectInfo } from '@/api/buildTeam'
export default {
  data () {
    return {
      tableList: [],
      teamid: '',
      activeSoundList: [],
      soundList: [],
      checkList: [],
      multipleSelection: [], // 列表选择的集合

    }
  },
  mounted () {
    // 获取声部信息
    this.teamid = this.$route.query.id
    this.getSound();
    sessionStorage.setItem('resetCode', '2');

  },
  methods: {
    getSound () {

      findMusicGroupSubjectInfo({ musicGroupId: this.teamid }).then(res => {
        if (res.code == 200) {
          this.activeSoundList = res.data.musicGroupSubjectPlans.map(item => {
            // this.soundLists[0].push(item.subjectId)
            let obj = { 'id': item.subjectId, 'sound': item.subName, 'jihua': item.expectedStudentNum, 'yuji': item.fee, 'zhonglei': [], 'fangshi': item.kitGroupPurchaseType, 'fangshiprice': item.depositFee, 'jiaopu': '', 'type': 1, 'typeVisible': false, 'provideVisible': false, 'markVisible': false, 'goodsList': [], 'markList': [{ 'name': '', 'goods': [], 'price': '' }], 'markChioseList': [] }
            return obj;
          })
          // 循环列表里的声部 拿取商品
          for (let i in this.activeSoundList) {
            for (let j in res.data.musicGroupSubjectGoodsGroups) {
              if (this.activeSoundList[i].id == res.data.musicGroupSubjectGoodsGroups[j].subjectId) {
                // 
                if (res.data.musicGroupSubjectGoodsGroups[j].type == 'INSTRUMENT') {
                  this.activeSoundList[i].zhonglei = this.activeSoundList[i].zhonglei.concat(res.data.musicGroupSubjectGoodsGroups[j].goodsIdList)
                  // console.log(this.activeSoundList[i].zhonglei)
                  this.activeSoundList[i].zhonglei = this.activeSoundList[i].zhonglei.map(item => {
                    return parseInt(item);
                  })

                  this.activeSoundList[i].goodsList = this.activeSoundList[i].goodsList.concat(res.data.musicGroupSubjectGoodsGroups[j].goodsList);

                  // goodsList

                }
                // 拿辅件
                if (res.data.musicGroupSubjectGoodsGroups[j].type == 'ACCESSORIES') {
                  if (res.data.musicGroupSubjectGoodsGroups[j].goodsList.length >= 0) {
                    this.activeSoundList[i].markList = [];
                    let obj = {};
                    obj.goods = res.data.musicGroupSubjectGoodsGroups[j].goodsIdList.split(',');
                    obj.goods = obj.goods.map(item => {
                      return parseInt(item);
                    })
                    obj.name = res.data.musicGroupSubjectGoodsGroups[j].name
                    obj.price = res.data.musicGroupSubjectGoodsGroups[j].price;
                    this.activeSoundList[i].markList.push(obj);
                    // this.activeSoundList[i].markList.goodsList.goods = JSON.parse(res.data.musicGroupSubjectGoodsGroups[j].goodsIdList)
                    this.activeSoundList[i].markChioseList = this.activeSoundList[i].markChioseList.concat(res.data.musicGroupSubjectGoodsGroups[j].goodsList);
                  }

                }
              }
            }
          }
        }
      })
    },
    saveInfo () {
      // 只提交第二页的数据
      let obj = {};
      let activeSoundList = this.activeSoundList
      // 添加商品以及教辅
      obj.musicGroupId = this.teamid
      obj.musicGroupStatus = 'PROGRESS';
      obj.musicGroupSubjectGoodsGroups = []
      obj.musicGroupSubjectPlans = []
      activeSoundList.forEach(active => {
        // 乐器
        active.zhonglei.forEach(zl => {
          let goodsItem = []
          active.goodsList.forEach(goods => {
            if (goods.id == zl) {
              goodsItem = goods
            }
          })
          let some = {
            subjectId: active.id,
            type: 'INSTRUMENT',
            goodsIdList: zl,
            name: goodsItem.name,
            price: goodsItem.groupPurchasePrice
          }
          obj.musicGroupSubjectGoodsGroups.push(some);
        })

        // 遍历声部里的教辅
        active.markList.forEach(mark => {
          if (mark.goods.length >= 1) {
            let some = {
              subjectId: active.id,
              type: 'ACCESSORIES',
              goodsIdList: mark.goods.join(','),
              name: mark.name,
              price: mark.price
            }
            obj.musicGroupSubjectGoodsGroups.push(some);
          }
        })
        // 添加声部
        let item = {
          expectedStudentNum: active.jihua,
          fee: active.yuji,
          kitGroupPurchaseType: active.fangshi,
          subName: active.sound,
          subjectId: active.id,
          depositFee: active.fangshiprice // depositFee  只有租赁才有
        }
        obj.musicGroupSubjectPlans.push(item);
      })
      // 发请求修改声部信息
      updateSubjectInfo(obj).then(res => {
        if (res.code == 200) {
          this.$message.success('提交成功')
          // this.$router.push({ path: '/business/teamDetail' })
          this.getSound();
        }
      })

    },
    // 根据声部id查询可选教辅
    chioseMark (row) {
      row.markVisible = true
      let id = row.id;
      getGoods({ 'subjectId': id, 'type': 'ACCESSORIES' }).then(res => {
        if (res.code == 200) {
          row.markChioseList = res.data;

        }
      })
    },    // 根据声部id获取可选乐器种类
    chioseType (row) {
      // 根据id查询可选类型种类
      let id = row.id;
      // 'subjectId': id, ' type'='INSTRUMENT'
      getGoods({ 'subjectId': id, 'type': 'INSTRUMENT' }).then(res => {
        if (res.code == 200) {
          row.goodsList = res.data;
        }
      })
    },

  }, filters: {
    zhongleiFilter (val, list) {
      if (list && list.length <= 0) {
        return;
      }
      let arr = []
      for (let i = 0; i < list.length; i++) {
        for (let j = 0; j < val.length; j++) {
          if (val[j] == list[i].id) {
            arr.push(list[i].name)
          }
        }
      }
      if (arr.length > 0) {
        return arr.join(',')
      } else {
        return ''
      }

    },
    fangshiFilter (val, price) {
      let str = ''
      if (val == 'FREE') {
        str = '免费'
        return str;
      } else if (val == 'GROUP') {
        str = '团购'
        return str
      } else if (val == 'LEASE') {
        str = '借用'
      }
      return str + '费用:' + price
    },
    goodsFilter (val, list) {
      if (list && list.length <= 0) {
        return '';
      }
      let arr = [];
      for (let i = 0; i < val.length; i++) {
        for (let j = 0; j < list.length; j++) {
          if (val[i] == list[j].id) {
            arr.push(list[j].name);
          }
        }
      }
      if (arr.length > 0) {
        return arr.join(',')
      } else {
        return ''
      }
    }
    // markFilter(val){
    //   for
    //   let name = val.name;
    //   let goods = val.goods.join(',');
    //   let price = val.price
    // }
  },
}
</script>
<style lang="scss">
</style>