<style>
  .LM_mainDiv {
    overflow-y: auto;
  }

  .LP_table {
    margin-top: 10px;
    background-color: #fff;
     border-top: 1px solid #d1d1d1;
    /*border-left: 1px solid #d1d1d1;*/
    /* border-right: 1px solid #d1d1d1;  */
    font-size: 14px;
    width:100%;
    min-width: 1200px;
    padding-bottom: 5px;
  }

  .LP_table tr {
    height: 40px;
  }


  .LP_table tr th {
    border-left: 1px solid #d1d1d1;
    border-bottom: 1px solid #d1d1d1;
    /* border-right: 1px solid #d1d1d1; */
    position: relative;
    background-color: #E6E6E6;
  }
  .LP_table tr th:last-child{

  border-right: 1px solid #d1d1d1;
}
  .LP_table td {
    border-left: 1px solid #d1d1d1;
    border-bottom: 1px solid #d1d1d1;
    /* border-right: 1px solid #d1d1d1; */
    text-align: center;
    padding: 5px 0;
  }
 .LP_table  td:last-child{

  border-right: 1px solid #d1d1d1;
}
  .LP_table .el-button--primary {
    padding: 5px;
  }

  .Lp_role {
    position: absolute;
    top: 15px;
    left: 30px;
  }

  .Lp_company {
    position: absolute;
    right: 30px;
    top: 5px;
  }
  .comLessDiv{
    width:100%;
    height:40px;
    margin-top:5px;
    padding-bottom:5px;
    border-bottom:1px solid #d1d1d1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .comLessDiv:last-child{
    border:0;
    padding-bottom:0;
  }
  .comLessDiv input{
    padding: 0 5px;
  }
  .comLessDiv .el-input-group__append,.comLessDiv .el-input-group__append button{
    padding: 0 8px;
    cursor: pointer;
  }
  .addLessPrice{
    margin-left:10px;
    color:seagreen;
    cursor: pointer;
  }
  .lessBtn{
    margin-top:5px;
  }
</style>

<template>
  <div>
    <el-row>
      <el-col :offset="23">
        <input type='button' class="normalBtn lessBtn" @click="SaveLessPrice(null)" value="保存"></input>
      </el-col>
    </el-row>
    <div class="LM_mainDiv">
      <!--保存-->
      <table border="0" cellspacing="0" cellpadding="0" class="LP_table" v-loading="this.loading">
        <tr>
          <th width="150">公司名称</th> 
          <th width="40%">人数</th>
          <th width="40%">奖励金额</th>
          <th width="150">操作</th>
        </tr>
        <tr v-for="(item, index) in dataList">
          <td>
            {{item.BName}} <i class="iconfont icon-bumenguanli-tianjia addLessPrice" @click="addLessPrice(item)"></i>
          </td>
          <td>
            <div v-for="(subItem, subIndex) in item.AwardList" class="comLessDiv">
              <el-input class="w200" type="number" v-model="subItem.MinPeopleNum" @blur="minChange(subItem, index, subIndex)"> 
                <el-button slot="append">人</el-button>
              </el-input>
              -
              <el-input class="w200" type="number" v-model="subItem.MaxPeopleNum" @blur="maxChange(subItem)">
                <el-button slot="append">人</el-button>
              </el-input>
            </div>
          </td>

          <td>
            <div v-for="subItem in item.AwardList" class="comLessDiv">
              <el-input class="w80"  placeholder="奖励金额" v-model="subItem.AwardMoney" @keyup.native=""> <el-button slot="append">元</el-button></el-input>
            </div>
          </td>
          <td>
            <div v-for="(subItem,subIndex) in item.AwardList" class="comLessDiv">
              <input type='button' class="normalBtn lessBtn" @click="SaveLessPrice(subItem)" value="保存"></input>
              <input type='button' class="normalBtn lessBtn" @click="RemoveItem(item, subItem, subIndex)" value="删除"></input>
            </div>
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        //分公司列表
        dataList: [],
        //角色列表
        RoleList: [],
        loading:false,
        LineList: [],
        commonSelect:0,
      };
    },
    methods: {
      maxChange: function (item) {
        if (item.MinPeopleNum === '') {
          item.MinPeopleNum = 0
        }
        if (item.MaxPeopleNum === '') {
          item.MaxPeopleNum = 0
        }
        if (parseInt(item.MaxPeopleNum) <= parseInt(item.MinPeopleNum)) {
          item.MaxPeopleNum = parseInt(item.MinPeopleNum) + 1
          return this.Error('必须大于最少人数!');
        }
      },
      minChange: function (item, fIndex, sIndex) {
        if (this.dataList[fIndex].AwardList.length > 1 && sIndex > 0) {
          if (parseInt(this.dataList[fIndex].AwardList[sIndex-1].MaxPeopleNum) > parseInt(item.MinPeopleNum)) {
            item.MinPeopleNum = parseInt(this.dataList[fIndex].AwardList[sIndex-1].MaxPeopleNum) + 1
            return this.Error('必须大于上一次最多人数!');
          }
        } else {
          if (item.MinPeopleNum === '') {
            item.MinPeopleNum = 0
          }
          if (item.MaxPeopleNum === '') {
            item.MaxPeopleNum = 0
          }
          if (parseInt(item.MaxPeopleNum) <= parseInt(item.MinPeopleNum)) {
            return item.MaxPeopleNum = parseInt(item.MinPeopleNum) + 1
          }
        }
      },
      //保存少价管理
      SaveLessPrice(subItem) {
        let List = [];
        if (subItem) {
          if (subItem.MaxPeopleNum>0) {
            List = [subItem]
          } else {
            return this.Error('请填写人数');
          }
        } else{
          this.dataList.forEach(x=>{
            x.AwardList.forEach(y=>{
              if (y.MinPeopleNum>0 && y.MaxPeopleNum>0)
              List.push(y)
            })
          })
        }
        this.apipost(
          "award_get_SetService", List,
          res => {
            if (res.data.resultCode == 1) {
              this.Success(res.data.message);
              this.getList();
            }else{
              this.Error(res.data.message);
            }
          },
          err => {}
        );
      },
      //获取列表
      getList() {
        this.loading = true;
        var that=this;
        that.apipost(
          "award_get_GetAllListService", {},
          res => {
            that.loading = false;
            if (res.data.resultCode == 1) {
              var data = res.data.data;
              that.dataList=data;
            }else{
              that.Error(res.data.message);
            }
          },
          err => {}
        );
      },
      //获取列表
      RemoveItem(item, subItem, index) {
        if(subItem.ID==0){
          item.AwardList.splice(index,1);
          return;
        }
        var that = this;
        this.Confirm("是否删除此少价信息?", function () {
          if (subItem.ID > 0) {
            that.apipost(
              "award_get_DelAward", [subItem.ID],
              res => {
                if (res.data.resultCode == 1) {
                  that.Success("操作成功!");
                  that.getList();
                } else {
                  that.Error(res.data.message);
                }
              },
              err => {}
            );
          }
        });
      },
      //点击添加
      addLessPrice(item){
        var obj = {
          RB_Branch_id: item.Id,
          MinPeopleNum: 0,
          MaxPeopleNum: 1,
          AwardMoney: 0,
          ID: 0,
        }
        item.AwardList.push(obj);
        this.$forceUpdate()
      }
    },
    mounted() { 
      this.getList();
    },
  };

</script>