<template>
  <div>
    <div class="query-box" style="border-bottom: none;">
      <ul>
        <li>
          <template v-if="IsOperation != 1">
            <el-button type="primary" class="fr normalBtn mb30" :loading="saveLogding" @click="saveList()">
              {{$t('pub.saveBtn')}}</el-button>
          </template>
          <template v-else>
            <span style="color:red;font-size:14px;">{{$t('ground.yizhidbnxg')}}!</span>
            <el-button v-if="EditBtn&&currentUserInfo.EmployeeId==615" class="fr normalBtn mb30" type="primary"
              :loading="saveLogding" @click="saveList()">{{$t('pub.saveBtn')}}</el-button>
          </template>
        </li>
      </ul>
    </div>
    <div style="width: 100%;  overflow-x: auto;padding-bottom: 10px; " class="ownScrollbarStyle">
      <table border="0" cellspacing="1" cellpadding="0" class="bookDinnerStatisticsTalbe" v-loading="loading">
        <tr>
          <th>
            {{$t('salesModule.CompanyNum')}}
          </th>
          <th colspan="17" style="text-align:left;">
            <p class="link" @click="goUrlT('productQuery',TCNUM,'产品查询')">{{TCNUM}}</p>
          </th>
        </tr>
        <tr>
          <th width="100">{{$t('restaurant.res_etTime')}}</th>
          <th width="200">{{$t('restaurant.res_resName')}}</th>
          <th width="150">{{$t('ground.xiugaict')}}</th>
          <th width="80">{{$t('ground.leibie')}}</th>
          <th width="70">{{$t('ground.zongrenshu')}}</th>
          <th width="70">{{$t('ground.yongcanren')}}<br />{{$t('advmanager.v_type')}}</th>
          <th width="70">{{$t('commonPickUp.Pick_Ding')}}<br />{{$t('admin.admin_personNumber')}}</th>
          <th width="120">{{$t('Operation.Op_price')}}</th>
          <th width="90">{{$t('ground.mianrenshu')}}</th>
          <th width="80">{{$t('ground.jinexiaoji')}}</th>
          <th width="90">{{$t('hotel.hotel_commission')}}</th>
          <th width="100">{{$t('fnc.a_zongjine')}}</th>
          <th width="210">{{$t('ground.fkfsdcfsdth')}}</th>
          <th width="170">{{$t('pub.pubRemark')}}</th>
        </tr>
        <template v-for="(item,index) in list">
          <template v-for="(subItem,subIndex) in item.DiningSummaryList">
            <tr v-for="(childItem,childIndex) in subItem.DiningPriceList" :key="`d_`+index+subIndex+childIndex">
              <td v-if="childIndex==0" :rowspan="3">
                {{item.UseTimeStr}}
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                <table class="dinnerTable">
                  <tr>
                    <td colspan="2">
                      <span style="padding-left:5px;" class="link"
                        @click="goUrlR('restaurantList',subItem.DiningID,'餐厅列表')">{{subItem.DiningName}}
                        <br />
                        {{$t('ground.zhenshimingcheng')}}: {{subItem.DiningRealName}}
                      </span>
                    </td>
                  </tr>
                  <tr>
                    <td width="40">
                      {{$t('hotel.table_tel')}}：
                    </td>
                    <td style="text-align:left;">
                      {{subItem.Tel}}
                    </td>
                  </tr>
                  <tr>
                    <td width="40">
                      {{$t('hotel.table_address')}}：
                    </td>
                    <td style="text-align:left;">
                      {{subItem.Address}}
                    </td>
                  </tr>
                </table>
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                <el-select style="display:none;" class='w160 sel' v-model='subItem.NewDiningID' filterable
                  :placeholder="$t('pub.pleaseSel')" @visible-change='getDingList(item)'
                  @change='sendValue(subItem,item)'>
                  <el-option v-for='itemHotel in item.DinnerList' :label='itemHotel.RealName' :value='itemHotel.ID'
                    :key='itemHotel.ID'></el-option>
                </el-select>
                <template v-if="subItem.NewDiningRealName">
                  <span style="color:green">{{subItem.NewDiningRealName}}</span><br />
                </template>
                <template v-else-if="subItem.NewDiningName">
                  <span style="color:green">{{subItem.NewDiningName}}</span><br />
                </template>
                <el-popover placement="right" width="540" trigger="click">
                  <comCheckDinner v-show="subItem.IsShowPop" :ref="'comCheckDinner'+index+subIndex+''"
                    :UseDate="item.UseTimeStr" v-on:childDinner="childDinnerList" :Country="651">
                  </comCheckDinner>
                  <el-button size="small" type="danger" :data-index="'comCheckDinner'+index+subIndex+''"
                    slot="reference" style="cursor:pointer;" @click="getChildDinner(index,subIndex,subItem)">
                    {{$t('ground.cantingxuanz')}}
                  </el-button>
                </el-popover>
                <template v-if="subItem.DinnerOfferPrice&&subItem.DinnerOfferPrice>0">
                  <br />
                  <span style="font-weight:bold;font-size:14px;">报价餐厅:<font style="color:red;">
                      {{subItem.DinnerOfferName}}({{subItem.DinnerOfferPrice}}/人)
                    </font></span>
                </template>
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                {{subItem.UseDinnerTypeStr}}
              </td>
              <td v-if="childIndex==0" :rowspan="3" style="display:none;">
                {{subItem.MealName}}
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                {{subItem.UseAccount}}
              </td>
              <td>
                {{personStrToWord(childItem.PeopleType)}}
              </td>
              <td>
                <template v-if="EditBtn||LineId==90">
                  <el-input class='w50' v-model='childItem.UsePeopleNum' @input='calculationPrice(subItem)'
                    @keyup.native="checkInteger(childItem,'UsePeopleNum')" type="text"></el-input>
                </template>
                <template v-else>
                  {{childItem.UsePeopleNum}}
                </template>
              </td>
              <td>
                <template v-if="EditBtn||LineId==90">
                  <el-input class='w110' v-model='childItem.PeoplePrice' @input='calculationPrice(subItem)'
                    @keyup.native="checkPrice(childItem,'PeoplePrice')" type="text"></el-input>
                </template>
                <template v-else>
                  <span class="spanlink" v-if='childItem.PeoplePrice==0'
                    @click="goUrl('RestaurantPackage',subItem,'套餐查询')">{{$t('leader.leader_Set')}}</span>
                  <template v-else>
                    <el-input class='w110 tcenter' @input='calculationPrice(subItem)' v-model='childItem.PeoplePrice'
                      @keyup.native="checkPrice(childItem,'PeoplePrice')"></el-input>
                  </template>
                </template>
              </td>
              <td>
                <el-input class='w110 tcenter' @input='calculationPrice(subItem)' maxlength='2'
                  v-model='childItem.Discount' @keyup.native="checkPrice(childItem,'Discount')"></el-input>
              </td>
              <td>
                {{(childItem.UsePeopleNum-childItem.Discount)*childItem.PeoplePrice}}
              </td>
              <td>
                <!--返佣-->
                <el-input class='w50 tcenter' @input='calculationPrice(subItem)' v-model='childItem.DiscountPrice'
                  @keyup.native="checkInteger(childItem,'DiscountPrice')"></el-input>%
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                {{subItem.TotalPrice}}
                <template v-if="subItem.NewTotalPrice">
                  <br /><span style="color:red;">{{subItem.NewTotalPrice}}</span>
                </template>
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                <table class="dinnerTable">
                  <tr>
                    <td width="70" style="text-align:right;">{{$t('hotel.hotel_Currency')}}：</td>
                    <td>
                      <el-select v-model="subItem.CurrencyId" :placeholder="$t('pub.pleaseSel')" class='w135 sel'
                        @change="calculationPrice(subItem)">
                        <el-option :label="$t('pub.unlimitedSel')" :value='0'></el-option>
                        <el-option v-for="(item,index) in allCurrencyList" :key="index" :label="item.Name"
                          :value="item.ID">
                        </el-option>
                      </el-select>
                    </td>
                  </tr>
                  <tr>
                    <td width="60" style="text-align:right;">
                      {{$t('fnc.fkfangshi')}}：
                    </td>
                    <td style="text-align:left;">
                      <el-select class='w135 sel' v-model='subItem.PayStyle' :placeholder="$t('pub.pleaseSel')">
                        <el-option :label="$t('pub.pleaseSel')" :value='0'></el-option>
                        <!--v-if="subItem.PayStyleExt==1||LineId==90||LineId==168"-->
                        <el-option :label='$t("ground.xianfu")' :value='1'>
                        </el-option>
                        <!--v-if="subItem.PayStyleExt==2||LineId==90||LineId==168"-->
                        <el-option :label='$t("ground.qiandan")' :value='2'>
                        </el-option>
                        <!--v-if="subItem.PayStyleExt==3"-->
                        <el-option :label='$t("ground.shiwudk")' :value='3'></el-option>
                        <!--v-if="subItem.PayStyleExt==4"-->
                        <el-option :label='$t("ground.yufu")' :value='4'></el-option>
                        <!--v-if="subItem.PayStyleExt==5"-->
                        <el-option :label='$t("ground.yufukuandk")' :value='5'>
                        </el-option>
                        <el-option :label='$t("ground.lingduidydf")' :value='10'></el-option>
                        <el-option :label='$t("ground.gongsihetzf")' :value='6'></el-option>
                        <el-option :label='$t("ground.shuaka")' :value='11'></el-option>
                      </el-select>
                    </td>
                  </tr>
                  <tr>
                    <td width="60" style="text-align:right;">
                      {{$t('ground.dingcanfangs')}}：
                    </td>
                    <td style="text-align:left;">
                      <el-select v-model='subItem.DiningReserveType' class='w135 sel'
                        :placeholder="$t('pub.pleaseSel')">
                        <el-option :label="$t('pub.pleaseSel')" value='0'></el-option>
                        <el-option :label='$t("ground.gongsiyuding")' value='1'></el-option>
                        <el-option :label='$t("ground.daoyouyuding")' value='2'></el-option>
                      </el-select>
                    </td>
                  </tr>
                  <tr>
                    <td width="60" style="text-align:right;">
                      订餐状态：
                    </td>
                    <td style="text-align:left;">
                      <el-select v-model='subItem.OrderState' class='w135 sel' :placeholder="$t('pub.pleaseSel')">
                        <el-option :label="$t('pub.pleaseSel')" :value='-1'></el-option>
                        <el-option :label='$t("pub.sureBtn")' :value='1'></el-option>
                        <el-option :label='$t("visa.v_zanding")' :value='2'></el-option>
                      </el-select>
                    </td>
                  </tr>
                  <template v-if="subItem.OrderState==1">
                    <tr>
                      <td width="60" style="text-align:right;">
                        确认时间：
                      </td>
                      <td style="text-align:left;">
                        <el-date-picker class='w135' v-model="subItem.SureTime" type="date" placeholder="确认时间"
                          value-format="yyyy-MM-dd">
                        </el-date-picker>
                      </td>
                    </tr>
                    <tr>
                      <td width="60" style="text-align:right;">
                        预约时间：
                      </td>
                      <td style="text-align:left;">
                        <!--午餐-->
                        <template v-if="subItem.UseDinnerType==2">
                          <el-time-select class='w135' placeholder="预约时间" v-model="subItem.BookTime"
                            :picker-options="{ start: '11:00',step: '00:15',end: '14:00'}"
                            @keyup.native="checkTime(subItem,'BookTime')">
                          </el-time-select>
                        </template>
                        <!--晚餐-->
                        <template v-if="subItem.UseDinnerType==3">
                          <el-time-select class='w135' placeholder="预约时间" v-model="subItem.BookTime"
                            :picker-options="{ start: '17:00',step: '00:15', end: '20:00'}"
                            @keyup.native="checkTime(subItem,'BookTime')">
                          </el-time-select>
                        </template>
                      </td>
                    </tr>
                  </template>
                  <tr v-show="subItem.PayStyle==6">
                    <td width="60" style="text-align:right;">
                      {{$t('ground.fukuanth')}}:
                    </td>
                    <td style="text-align:left;">
                      <el-input class='w135' v-model='subItem.PayTypeTCNUM' type="text"></el-input>
                    </td>
                  </tr>
                  <tr>
                    <td width="60" style="text-align:right;">
                      {{$t('ground.dingtuanhao')}}:
                    </td>
                    <td style="text-align:left;">
                      <el-input class='w135 tcenter' v-model='subItem.ReserveNo'></el-input>
                    </td>
                  </tr>
                </table>
              </td>
              <td v-if="childIndex==0" :rowspan="3">
                <table class="dinnerTable">
                  <tr>
                    <td width="80" style="text-align:right;">{{$t('pub.pubRemark')}}：</td>
                    <td>
                      <el-input type='textarea' rows="6" class='w120' v-model='subItem.Remarks'></el-input>
                    </td>
                  </tr>
                  <tr>
                    <td width="80" style="text-align:right;">
                      {{$t('ground.canpeibz')}}：
                    </td>
                    <td style="text-align:left;">
                      <el-input type='textarea' rows="2" class='w120' v-model='subItem.DinnerRemark'></el-input>
                    </td>
                  </tr>
                </table>
              </td>
            </tr>
          </template>
        </template>
      </table>
    </div>
  </div>
</template>
<script>
  import comCheckDinner from "../commonPage/comCheckDinner.vue";
  export default {
    data() {
      return {
        EditBtn: false, //房餐景点修改权限
        IsEditDinner: 0, //1有权限操作餐配
        defaultSelectValue: 0,
        list: [],
        roomList: [],
        TCNUM: "",
        flightTotal: 0,
        GuestNum: 0,
        loading: false,
        IsOperation: '',
        saveLogding: false,
        allCurrencyList: [],
        findex: 0,
        childIndex: 0,
        LineId: 0,
        currentUserInfo: {}
      };
    },
    components: {
      comCheckDinner,
    },
    methods: {
      getChildDinner(index, subIndex, subItem) {
        this.findex = index;
        this.childIndex = subIndex;
        let str = `comCheckDinner${index}${subIndex}`
        this.$refs[str][0].getCheckDinner();
        subItem.IsShowPop = true;
      },
      //选中赋值
      childDinnerList(obj) {
        var oldData = this.list[this.findex].DiningSummaryList[this.childIndex];
        oldData.Address = obj.Address;
        oldData.Tel = obj.Tel;
        oldData.PayStyle = obj.PayStyle;
        oldData.PayStyleExt = obj.PayStyle;
        oldData.NewDiningID = obj.ID;
        oldData.NewDiningName = obj.Name;
        oldData.NewDiningRealName = obj.RealName;
        this.list.forEach(x => {
          x.DiningSummaryList.forEach(y => {
            y.IsShowPop = false;
          })
        });
      },
      //页面跳转[大写]
      goUrlR(path, obj, title) {
        this.$router.push({
          name: path,
          query: {
            ID: obj,
            blank: 'y',
            tab: title
          }
        });
      },
      //页面跳转[小写]
      goUrlT(path, obj, title) {
        this.$router.push({
          name: path,
          query: {
            id: obj,
            blank: 'y',
            tab: title
          }
        });
      },
      goUrl(path, obj, name) {
        this.$router.push({
          path: path,
          query: {
            id: obj.DiningID,
            blank: "y",
            tab: name
          }
        });
      },
      personStrToWord(str) {
        if (str == 1)
          return '大人'
        if (str == 2)
          return '中人'
        if (str == 3)
          return '小人'
      },
      getList() {
        this.loading = true;
        this.apipost(
          "dmcstatistics_get_GetDinnerStaticsDetail", {
            TCIDs: this.$route.query.id
          },
          res => {
            this.loading = false;
            if (res.data.resultCode == 1) {
              var resultArray = res.data.data.DiningList;
              this.IsOperation = res.data.data.IsOperation;
              resultArray.forEach(item => {
                item.DinnerList = [];
                item.DiningSummaryList.forEach(x => {
                  x.DiningReserveType = x.DiningReserveType.toString();
                  this.calculationPrice(x);
                  var str = x.NewDiningName;
                  if (x.RealName) {
                    str = x.RealName;
                  }
                  item.DinnerList.push({
                    RealName: str,
                    ID: x.NewDiningID
                  })

                  if (x.ReserveNo == '' || x.ReserveNo == null) {
                    x.ReserveNo = this.$route.query.NewCombinationNum;;
                  }
                  if (x.SureTime) {
                    if (x.SureTime == '0001-01-01T00:00:00') {
                      x.SureTime = ''
                    } else {
                      x.SureTime = x.SureTime.replace("T", " ");
                    }
                  }

                })
              });
              this.list = resultArray;
            } else {
              this.Error(res.data.message);
            }
          },
          err => {}
        );
      },
      calculationPrice(obj) {
        let totalPrice = 0;
        //获取当前选中的对象
        let currentObj = {};
        if (obj.CurrencyId != 0) {
          currentObj = this.allCurrencyList.find(item => {
            return item.ID === obj.CurrencyId; //筛选出匹配数据
          });
        }
        //日元
        let jpaObj = this.allCurrencyList.find(item => {
          return item.ID === 3; //筛选出匹配数据
        });
        obj.DiningPriceList.forEach((item, index) => {
          var tempPrice = 0;
          var coefficient = 1;
          totalPrice += item.PeoplePrice * (item.UsePeopleNum - item.Discount) * (coefficient - item.DiscountPrice /
            100);
          totalPrice += tempPrice;
        })
        obj.TotalPrice = totalPrice.toFixed(2);
        if (this.LineId == 14) {
          if (currentObj && currentObj.ID > 0) {
            //人民币
            if (currentObj.ID == 1) {
              obj.NewTotalPrice = currentObj.Name + ":" + (totalPrice * jpaObj.PayRate).toFixed(2);
            }
            //日元
            else if (currentObj.ID == 3) {
              obj.NewTotalPrice = "";
            } else {
              if (currentObj.CurrentRate > 1) {
                obj.NewTotalPrice = currentObj.Name + ":" + ((totalPrice * jpaObj.PayRate) / currentObj.CurrentRate)
                  .toFixed(2);
              } else {
                obj.NewTotalPrice = currentObj.Name + ":" + ((totalPrice * jpaObj.PayRate) * currentObj.CurrentRate)
                  .toFixed(2);
              }
            }
          } else {
            obj.NewTotalPrice = "";
          }
        }
        this.$forceUpdate();
      },
      saveList() {
        this.saveLogding = true;
        this.list.forEach(item => {
          item.DiningSummaryList.forEach(insideItem => {
            if (insideItem.DiningPriceList) {
              insideItem.DiningPriceList.forEach(subItem => {
                if (!subItem.Discount) {
                  subItem.Discount = 0;
                }
                if (!subItem.DiscountPrice) {
                  subItem.DiscountPrice = 0;
                }
              });
            }
          })
        })
        this.apipost('dmcstatistics_get_SetDiningOrder', this.list, res => {
          this.saveLogding = false;
          if (res.data.resultCode == 1) {
            this.$message.success(res.data.message);
            this.getList();
          } else {
            this.$message.error(res.data.message);
          }
        }, err => {})
      },
      //获取餐厅下拉
      getDingList(item) {
        let msg = {
          QCity: item.DiningSummaryList[0].QCity
        }
        this.apipost('dining_post_GetLineDinnerList', msg, res => {
          if (res.data.resultCode == 1) {
            item.DinnerList = res.data.data;
            this.$forceUpdate();
          } else {
            this.Error(res.data.message)
          }
        }, err => {})
      },
      //重置电话地址
      sendValue(subItem, item) {
        item.DinnerList.forEach(x => {
          if (subItem.NewDiningID == x.ID) {
            subItem.Address = x.Address;
            subItem.Tel = x.Tel;
            subItem.PayStyle = x.PayStyle;
            subItem.PayStyleExt = x.PayStyle;
          }
        })
      },
      GetAuth() {
        this.apipost('dmcstatistics_get_GetGetDmcTotalTablePageAuth', {}, res => {
          if (res.data.resultCode == 1) {
            this.IsEditDinner = res.data.data.IsEditDinner;
          } else {
            this.$message.error(res.data.message);
          }
        }, err => {})
      },
      CheckAuth() {
        var actionCode = this.$AuthCode.EditRoomDinnerTIcket;
        this.CheckUserAuth(actionCode, res => {
          if (res.data.resultCode == 1 && res.data.data == 1) {
            this.EditBtn = true;
          }
        });
      },
    },
    mounted() {
      this.currentUserInfo = this.getLocalStorage();
      this.CheckAuth();
      this.GetAuth();
      this.TCNUM = this.$route.query.TCNUM;
      this.GuestNum = this.$route.query.GuestNum;
      if (this.$route.query.LineId) {
        this.LineId = this.$route.query.LineId;
      }
      this.getList();
      this.apipost("financeinfo_post_GetList", {}, res => {
        if (res.data.resultCode === 1) {
          this.allCurrencyList = res.data.data;
        }
      }, err => {});
    }
  };

</script>

<style>
  .bookDinnerStatisticsTalbe {
    border-collapse: collapse;
    background: #fff;
    text-align: center;
  }

  .bookDinnerStatisticsTalbe tr th {
    background: #eee;
    height: 40px;
    font-size: 12px;
    color: #333;
    border: 1px solid #d1d1d1;
  }

  .bookDinnerStatisticsTalbe tr td {
    font-size: 12px;
    height: 25px;
    line-height: 25px;
    border: 1px solid #d1d1d1;
  }

  .bookDinnerStatisticsTalbe tr td input {
    height: 20px !important;
    padding: 0;
    text-align: center;
  }

  .bookDinnerStatisticsTalbe tr td .sel input {
    height: 34px !important;
  }

  .bookDinnerStatisticsTalbe tr td .link:hover {
    text-decoration: underline;
    cursor: pointer;
  }

  .bookDinnerStatisticsTalbe tr td .spanlink:hover {
    text-decoration: underline;
    cursor: pointer;
  }

  .bookDinnerStatisticsTalbe .dinnerTable {
    border: none;
  }

  .bookDinnerStatisticsTalbe .dinnerTable tr td {
    border: none;
  }

</style>
