<template>
  <div class="CostManagement">
    <div class="head-title">
      {{$t('objFill.v101.chengbengli')}}
      <el-button style="float:right" size="small" @click="creatDanju" type="primary">{{$t('ground.scdanju')}}
      </el-button>
    </div>
    <div class="content">
      <div >

        <span>{{$t('restaurant.res_oderTime')}}：</span>
        <el-date-picker v-model="dateList" @change="msg.pageIndex=1,getList()" size="small" type="datetimerange"
          :range-separator="$t('OrderList.zhi')" value-format="yyyy-MM-dd HH:mm:ss" :start-placeholder="$t('OrderList.star')" :end-placeholder="$t('OrderList.end')">
        </el-date-picker>
        <span style="margin-left:15px">{{$t('hotel.order_Form')}}：</span>
        <el-select class="w120" @change="msg.pageIndex=1,getList()" v-model="msg.OrderSource" size="small"
          :placeholder="$t('pub.pleaseSel')">
          <el-option :label="$t('pub.unlimitedSel')" :value="0"></el-option>
          <el-option v-for="item in platList" :key="item.Id" :label="item.Name" :value="item.Id">
          </el-option>
        </el-select>
        <span style="margin-left:15px">{{$t('active.cl_orderType')}}：</span>
        <el-select class="w120" @change="msg.pageIndex=1,getList()" v-model="msg.OrderType" size="small"
          :placeholder="$t('pub.pleaseSel')">
          <el-option :label="$t('OrderList.orderStatus.normal')" :value="0"></el-option>
          <el-option v-for="item in orderTypeList" :key="item.Id" :label="item.Name" :value="item.Id">
          </el-option>
        </el-select>
          <span style="margin-left:15px">{{$t('objFill.v101.shifouzhid')}}：</span>
        <el-select class="w120" @change="msg.pageIndex=1,getList()" v-model="msg.IsSelectCostFinance" size="small"
          :placeholder="$t('pub.pleaseSel')">
          <el-option :label="$t('pub.unlimitedSel')" :value="0"></el-option>
          <el-option :label="$t('objFill.v101.yizhidan')" :value="1"></el-option>
          <el-option :label="$t('objFill.v101.FinancialModule.weizhidan')" :value="2"></el-option>

        </el-select>

        <el-input @clear="msg.pageIndex=1,getList()" @keyup.enter.native="msg.pageIndex=1,getList()" clearable
          style="margin-left:15px;width:300px" class="input-with-select" :placeholder="$t('fnc.qsrneirong')" v-model="msgVal"
          size="small">
          <el-select style="width:100px" @change="ChangeId" v-model="msgId" slot="prepend" :placeholder="$t('pub.pleaseSel')">
            <el-option v-for="item in option_t" :key="item.Id" :label="item.name" :value="item.Id">
            </el-option>
          </el-select>
        </el-input>

      </div>

      <el-table :data="tableData" v-loading="loading" border style="width: 100%;margin-top:15px"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" style="text-align:center;" :selectable="checkSelectable">
        </el-table-column>
        <el-table-column prop="OrderNo" :label="$t('hotel.order_Number')" width="220">
        </el-table-column>
        <el-table-column prop="GoodsName" :label="$t('MarketingActi.commName')">
            <template slot-scope="scope">
                <div style="display: flex;align-items: center;">
                    <span v-if="scope.row.OrderType==1 && scope.row.HotelList.length>0">{{scope.row.HotelList[0].HotelName}}</span>
                    <span v-if="scope.row.OrderType==2 && scope.row.DiningList.length>0">{{scope.row.DiningList[0].MealName}}</span>
                    <span v-if="scope.row.OrderType==3 && scope.row.TicketList.length>0">{{scope.row.TicketList[0].TicketName}}</span>
                    <span v-if="scope.row.OrderType==4 && scope.row.CarList.length>0">{{scope.row.CarList[0].StartCityName}}-{{scope.row.CarList[0].ArriveCityName}}</span>
                    <span v-if="scope.row.OrderType==5 && scope.row.SDGoodsList.length>0">{{scope.row.SDGoodsList[0].GoodsName}}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column prop="Income" :label="$t('fnc.jine')" width="80">
        </el-table-column>
        <el-table-column prop="CouponMoney" :label="$t('hotel.hotel_cost')" width="80">
           <template slot-scope="scope">
              <div style="display: flex;align-items: center;">
                  <span v-if="scope.row.OrderType==1">{{scope.row.HotelList[0].CostMoney?scope.row.HotelList[0].CostMoney:0}}</span>
                  <span v-if="scope.row.OrderType==2">{{scope.row.DiningList[0].CostMoney?scope.row.DiningList[0].CostMoney:0}}</span>
                  <span v-if="scope.row.OrderType==3">{{scope.row.TicketList[0].CostMoney?scope.row.TicketList[0].CostMoney:0}}</span>
                  <span v-if="scope.row.OrderType==4">{{scope.row.CarList[0].CostMoney?scope.row.CarList[0].CostMoney:0}}</span>
                  <span v-if="scope.row.OrderType==5">{{scope.row.SDGoodsList[0].CostMoney?scope.row.SDGoodsList[0].CostMoney:0}}</span>
              </div>
            </template>
        </el-table-column>

        <el-table-column prop="CouponMoney" :label="$t('objFill.v101.baoxiancben')" width="80">
           <template slot-scope="scope">
              <div style="display: flex;align-items: center;">

                  <span v-if="scope.row.OrderType==5">{{scope.row.SDGoodsList[0].InsuranceCostMoney}}</span>
                  <span v-else>-</span>
              </div>
            </template>
        </el-table-column>

        <el-table-column prop="CostFinanceId" :label="$t('objFill.costdocument')" width="100">
            <template slot-scope="scope">
                    <div style="display: flex;align-items: center;">
                       {{scope.row.CostFinanceId>0?scope.row.CostFinanceId:'-'}}
                    </div>
                  </template>
        </el-table-column>



      </el-table>
      <el-pagination style="text-align:right" background @current-change="handleCurrentChange" :page-size="msg.pageSize" :current-page.sync="msg.pageIndex"
                     :page-sizes="[15, 30, 45, 60,75,90,100]"
                     @size-change="handleSizeChange"
        layout="sizes,prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <!-- 选择是否公账 -->
    <el-dialog :title="$t('ground.scdanju')" :visible.sync="choiceFin" width="400px">
        <div>
            <span style="margin-right:10px">{{$t('objFill.fukuantype')}}</span>
             <el-radio v-model="finMsg.IsPublic" label="0">{{$t('fnc.sizhang')}}</el-radio>
            <el-radio v-model="finMsg.IsPublic" label="1">{{$t('fnc.gongzhang')}}</el-radio>
        </div>

      <div style="margin-top:10px">
          <span style="margin-right:10px">{{$t('fnc.khleixing')}}</span>
            <el-select class="w200"  v-model="finMsg.ClientType" size="small"  :placeholder="$t('pub.pleaseSel')" @change="financeinfo_post_GetClientAccountList(finMsg.ClientType)">
                <el-option v-for="item in ClientTypeList" :key="item.ID" :label="item.Name" :value="item.ID">
                </el-option>
            </el-select>
      </div>
       <div style="margin-top:10px">
          <span style="margin-right:10px">{{$t('fnc.khmingcheng')}}</span>
            <el-select class="w200"  v-model="finMsg.ClientID" size="small" filterable   :placeholder="$t('pub.pleaseSel')" @change="getName(finMsg.ClientID)">
                <el-option v-for="item in ClientAccountList" :key="item.ID" :label="item.Name" :value="item.ID">
                </el-option>
            </el-select>
      </div>
      <div style="margin-top:10px">
          <span style="margin-right:10px">{{objName}}</span>

      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="choiceFin=false">{{$t('pub.cancelBtn')}}</el-button>
        <el-button type="primary" size="small" @click="setFince()">{{$t('pub.sureBtn')}}</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        loading: false,
        //选择公账弹窗
        choiceFin: false,
        msg: {
          pageIndex: 1,
          pageSize: 15,
          OrderSource:0,
          OrderType: 0,
          OrderId: 0,
          StartTime: '',
          EndTime: '',
          OrderStatus: 0,
          OrderNo: '',
          UserName: '',
          UserId: '',
          Consignee: '',
          Mobile: '',
          ProductCode: '',
          Recycled: 2,
          IsSelectCostFinance: 0, //是否制单 0-全部 1-已制单 2-未制单

        },
        SupplierData: [], //供应商data
        MakingType:[
          {Id:0,Name: this.$t('advmanager.v_all')},
          {Id:1,Name:this.$t('hotel.hotel_cost')},
          {Id:2,Name:this.$t('objFill.v101.FinancialModule.yunfei')},
          {Id:3,Name:this.$t('Operation.Op_insurance')},
        ],
        OrderClass:[
          {Id:0,Name:this.$t('objFill.v101.shangpindind')},
          {Id:1,Name:this.$t('objFill.v101.sidaodind')},

        ],
        dateList: [], //日期
        total: 0,
        tableData: [], //列表数据
        finMsg: {

          OrderList: [],
          IsPublic: '0', //0-私账1-公账
          ClientType:'',//客人类型
          ClientID:'',//账户id
        },
        ERPEmpId:0,
        platList: [],
        orderTypeList:[],
        msgId: 1,
         option_t: [{Id: 1,name: this.$t('hotel.order_Number') },
          { Id: 2, name: this.$t('objFill.yonghuming') },
          {Id: 3,name: this.$t('objFill.v101.FinancialModule.yonghuid') },
          {Id: 4,name: this.$t('hotel.suplier_contact') },
          {Id: 5,name: this.$t('restaurant.res_ContactNumber') },
        ],
        msgVal: '',
        ClientTypeList:[],
        ClientAccountList:[],
        objName:'',

      }
    },
    created() {
       let currentUser = this.getLocalStorage()
      this.ERPEmpId =  currentUser.ERPEmpId
    },
    mounted() {
      this.getclientList()
      this.getList();
      this.getplat();
      this.getOrderType();
    },
    methods: {

      getList() {
        this.ChangeId(this.msgId);
        if (!this.msgVal || this.msgVal == '') {
          this.msg.OrderNo = '';
          this.msg.UserName = '';
          this.msg.UserId = '';
          this.msg.Consignee = '';
          this.msg.Mobile = '';
        }
        if (this.dateList && this.dateList.length > 0) {
          this.msg.StartTime = this.dateList[0];
          this.msg.EndTime = this.dateList[1];
        } else {
          this.msg.StartTime = '';
          this.msg.EndTime = '';
        }
        this.loading = true;
        this.lxymallapipost("/api/DmcOrder/GetDmcOrderCostPageList", this.msg, res => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            this.total = res.data.data.count;
            this.tableData = res.data.data.pageData;
          }
        })
      },
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      handleSizeChange(val){
        this.msg.pageIndex = 1;
        this.msg.pageSize = val;
        this.getList();

      },
      //点击生成单据
      creatDanju() {
        if (this.finMsg.OrderList.length == 0) {
          this.Info(this.$t('fnc.a_qxzdanju'));
        } else {
          this.choiceFin = true;
        }
      },
      handleSelectionChange(val) {
        var selectRow = JSON.parse(JSON.stringify(val));
        this.finMsg.OrderList = selectRow
      },
      //设置单据
      setFince() {
        if(this.finMsg.ClientType == ''){
            this.Info(this.$t('objFill.qingxuanzhekhlx'));
            return
        }
        if(this.finMsg.ClientID == ''){
            this.Info(this.$t('rule.qxzkhmingcheng'));
            return
        }
          const loading = this.$loading({
              lock: true,
              text: this.$t('objFill.v101.shengccwdjuz'),
              spinner: 'el-icon-loading',
            });
        this.lxymallapipost("/api/DmcOrder/SetDmcOrderCostFinance", this.finMsg, res => {
          if (res.data.resultCode == 1) {
            this.Success(this.$t('ground.shezhichenggong'));
            this.choiceFin = false;
            loading.close();
             this.finMsg= {
                OrderList: [],
                IsPublic: '0', //0-私账1-公账
                ClientType:'',//客人类型
                ClientID:'',//账户id
              }
          }else {
            this.Error(res.data.message)
            this.choiceFin = false;
            loading.close();
          }
        })
      },
      getOrderType() {
        this.lxymallapipost("/api/DmcOrder/GetOrderTypeEnumList", {}, res => {
          this.orderTypeList = res.data.data;
        })
      },
      getplat() {
        this.lxymallapipost("/api/DmcOrder/GetUserSourceEnumList", {}, res => {
          this.platList = res.data.data;
        })
      },
      ChangeId(val) {
        let msgVal = this.msgVal;
        if (val == 1) {
           this.msg.OrderNo = msgVal;
          this.msg.UserName = '';
          this.msg.UserId = '';
          this.msg.Consignee = '';
          this.msg.Mobile = '';
        } else if (val == 2) {
          this.msg.OrderNo = '';
          this.msg.UserName = msgVal;
          this.msg.UserId = '';
          this.msg.Consignee = '';
          this.msg.Mobile = '';
        } else if (val == 3) {
          this.msg.OrderNo = '';
          this.msg.UserName = '';
          this.msg.UserId = msgVal;
          this.msg.Consignee = '';
          this.msg.Mobile = '';
        } else if (val == 4) {
          this.msg.OrderNo = '';
          this.msg.UserName = '';
          this.msg.UserId = '';
          this.msg.Consignee = msgVal;
          this.msg.Mobile = '';
        } else if (val == 5) {
          this.msg.OrderNo = '';
          this.msg.UserName = '';
          this.msg.UserId = '';
          this.msg.Consignee = '';
          this.msg.Mobile = msgVal;
        }

      },
        getclientList(){ // 获取对象类型
            this.apipost('financeinfo_post_GetClientTypeList',{}, res => {
                if(res.data.resultCode == 1) {
                    this.ClientTypeList = res.data.data;
                }
            }, err => {})
        },
        financeinfo_post_GetClientAccountList(t){
             let msg = {
                ID:0,
                Type:t,
                ObjID:0,
                CardNum:''
            }
             this.apipost('financeinfo_post_GetClientAccountList',msg, res => {
                if(res.data.resultCode == 1) {
                    this.finMsg.ClientID = '';
                    this.objName = '';
                    let data = res.data.data;
                    let ClientAccountList = [];
                    data.forEach(x=>{
                    let obj = {
                        ID:x.ID,
                        Name:x.AccountHolder + `（${x.AccountAlias}）`,
                        Nom:x.CardNum,
                        OpenBankName:x.OpenBankName,
                        ObjID:x.ObjID,
                    }
                    ClientAccountList.push(obj)
                })
                this.ClientAccountList = ClientAccountList;
                }
            }, err => {})
        },
        getName(id){
          this.objName='';
          this.ClientAccountList.map(x=>{
            if(x.ID == id){
              this.objName=x.Nom+' '+x.OpenBankName;
              return
            }
          })
        },

         checkSelectable(row){//判断是否可以选择
          return row.CostFinanceId==0
      },

    }
  };

</script>
<style>
  @import '../Lxy/css/index.css';

  .CostManagement .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
 .CostManagement span{
    font-size: 13px;
    font-weight: normal;
}

</style>
