<template>
  <div class="RecommendBill">
    <template>
      <div class="head-title">
        推荐账单
        <el-button @click=" orderMsg.UserId = 0,orderMsg.SupplierId = 0,choiceOrder=true,UserMsg.Name='',getUserId()" style="float:right;margin: -5px 0 0 5px;" size="small"
          type="primary">
          新增
        </el-button>
        <el-button @click="msg.pageIndex=1,getList()" style="float:right;margin-top: -5px;" size="small" type="primary">
          查询
        </el-button>
      </div>
      <div class="content">
        <div>
          <el-form label-width="80px">
            <el-form-item label="用户" class="rr_BillItem">
              <el-select size="small" v-model="msg.UserId" :filter-method="ChangeListName"
                @change="msg.pageIndex=1,getList()" filterable placeholder="请选择">
                <el-option label="不限" :value="0"></el-option>
                <el-option v-for="item in tableData" :key="item.Id" :label="item.Name" :value="item.Id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="供应商" class="rr_BillItem">
              <el-select class="w150" style="margin-right: 10px;" v-model="msg.SupplierId"
                @change="msg.pageIndex=1,getList()" size="small" placeholder="请选择">
                <el-option label="不限" :value="0"></el-option>
                <el-option v-for="item in options" :key="item.ID" :label="item.Name" :value="item.ID">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="状态" class="rr_BillItem">
              <el-select class="w150" v-model="msg.BillState" size="small" @change="msg.pageIndex=1,getList()"
                placeholder="请选择">
                <el-option label="不限" :value="0"></el-option>
                <el-option label="待打款" :value="1"></el-option>
                <el-option label="已打款" :value="2"></el-option>
                <el-option label="取消" :value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="期数" class="rr_BillItem">
              <el-input type="text" class="w150" size="small" @keyup.enter.native="msg.pageIndex=1,getList()" v-model="msg.Periods">
              </el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-table :data="dataList" v-loading="loading" border style="width: 100%;margin:20px 0">
          <el-table-column prop="Id" label="id" width="80"></el-table-column>
          <el-table-column prop="Periods" label="期数" width="100"></el-table-column>
          <el-table-column prop="Money" label="总金额" width="100"></el-table-column>
          <el-table-column prop="SupplierName" label="供应商名称" width="130"></el-table-column>
          <el-table-column prop="UserName" label="用户名称" width="150"></el-table-column>
          <el-table-column prop="Remark" label="备注" min-width="200"></el-table-column>
          <el-table-column prop="BillState" width="100" label="状态">
            <template slot-scope="scope">
              <span style="color:#409EFF;" v-if="scope.row.BillState==1">待打款</span>
              <span style="color:green;" v-if="scope.row.BillState==2">已打款</span>
              <span style="color:red;" v-if="scope.row.BillState==3">取消</span>
            </template>
          </el-table-column>
          <el-table-column prop="UpdateDate" label="操作时间" width="120"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <el-tooltip class="item" effect="dark" content="修改账单备注" placement="top">
                <img @click="updateRemark(scope.row)" style="margin-right:9px" src="../../assets/img/userman/edit.png"
                  alt="">
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="账单明细" placement="top">
                <img @click="goOrderDetail(scope.row)" style="margin-right:9px" src="../../assets/img/userman/order.png"
                  alt="">
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="账单打款" placement="top">
                <img @click="OrderDakuan(scope.row)" v-if="scope.row.BillState==1" style="margin-right:9px" src="../../assets/img/userman/balance.png"
                  alt="">
              </el-tooltip>
              <el-tooltip class="item" effect="dark" content="取消账单" placement="top">
                <img @click="cancelOrder(scope.row)" v-if="scope.row.BillState==1" style="margin-right:9px" src="../../assets/img/userman/nopass.png"
                  alt="">
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination style="text-align:right" background @current-change="handleCurrentChange"
          :page-size="msg.pageSize" layout="prev, pager, next" :total="total">
        </el-pagination>
      </div>
    </template>
    <!-- 选择返佣订单列表 -->
    <el-dialog title="新增账单" :visible.sync="choiceOrder" width="1200px" @close='closeDialog'>
      <el-form label-width="80px">
        <el-form-item label="用户" style="display:inline-block;">
          <el-select size="small" v-model="orderMsg.UserId" :filter-method="ChangeListName" filterable
            placeholder="请选择">
            <el-option label="不限" :value="0"></el-option>
            <el-option v-for="item in searchUserTable" :key="item.Id" :label="item.Name" :value="item.Id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" style="display:inline-block;">
          <el-select class="w150" v-model="orderMsg.SupplierId"  size="small" placeholder="请选择">
            <el-option label="不限" :value="0"></el-option>
            <el-option v-for="item in options" :key="item.ID" :label="item.Name" :value="item.ID">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item style="display:inline-block;margin-left:10px;" label-width="0">
          <el-button @click="goquery()" size="small" type="primary">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table ref="multipleTable" :data="orderData" tooltip-effect="dark" height="450" style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="OrderNo" label="订单号" width="185"></el-table-column>
        <el-table-column label="商品信息">
          <template slot-scope="scope">
            <div class="clearfix">
              <div class="Re_leftImage">
                <img :src="scope.row.CoverImage" alt=""/>
              </div>
              <div class="Re_rightDiv">
                <el-tooltip class="item" effect="dark" :content="scope.row.GoodsName" placement="top">
                  <div class="ReDivList">
                      {{scope.row.GoodsName}}
                  </div>
                </el-tooltip>
                <div style="margin-top:5px;">
                  规格:&nbsp;&nbsp;<el-tag size="small" v-for="(item,index) in scope.row.SpecificationList" :key="index">{{item}}</el-tag>
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="Number" label="数量" width="60"></el-table-column>
        <el-table-column prop="Final_Price" label="价格" width="80"></el-table-column>
        <el-table-column prop="OrderIntroduction" label="返佣信息" width="200">
          <template slot-scope="scope">
            <div>姓名：{{scope.row.OrderIntroduction.Name}}</div>
            <div>昵称：{{scope.row.OrderIntroduction.UserName}}</div>
            <div>佣金：{{scope.row.OrderIntroduction.Commission}}</div>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
              <span style="color:red;" v-if="scope.row.OrderIntroduction.CommissionState==1">未结算</span>
              <span style="color:green;" v-if="scope.row.OrderIntroduction.CommissionState==2">已结算</span>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="choiceOrder = false,toggleSelection()">取消</el-button>
        <el-button type="primary" size="small" @click="saveOrderChoice">确定</el-button>
      </span>
    </el-dialog>
    <!-- 修改备注 -->
    <el-dialog title="修改备注" :visible.sync="upRemarkDialog" width="800px">
      <el-form label-width="80px">
        <el-form-item label="备注">
          <el-input type="textarea" v-model="remarkMsg.Remark" :rows="4"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="upRemarkDialog = false">取消</el-button>
        <el-button type="primary" size="small" @click="saveRemark">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
  export default {
    data() {
      return {
        loading: false,
        dataList: [],
        msg: {
          pageIndex: 1,
          pageSize: 15,
          UserId: 0,
          SupplierId: 0,
          Periods: '',
          BillState: 0
        },
        total: 0,
        //获取用户下拉msg
        UserMsg: {
          pageIndex: 1,
          pageSize: 20,
          Name: '',
          Source: 0,
          MemberGrade: 0,
          Id: 0,
          Moblie: ''
        },
        //用户下拉
        tableData: [],
        //弹窗用户下拉
        searchUserTable: [],
        //供应商
        options: [],
        //选择返佣
        choiceOrder: false,
        //返佣订单msg
        orderMsg: {
          pageIndex: 1,
          pageSize: 9999,
          UserId: 0,
          SupplierId: 0,
          RemitStatus: 2,
          CommissionState: 2
        },
        //弹窗数据源
        orderData: [],
        //新增账单保存msg
        addOrderMsg: {
          IntroductionIds: [],
          SupplierId: 0,
          UserId: 0
        },
        //公用IntroductionIds
        IntroductionIds: [],

        //修改备注弹窗
        upRemarkDialog: false,
        //修改备注msg
        remarkMsg: {
          BillId: 0,
          Remark: ''
        }
      };
    },
    components: {},
    created() {

    },
    methods: {
      handleCurrentChange(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      getList() {
        this.loading = true;
        this.apipost("/api/user/GetRecommendOrdersBillPageList", this.msg, res => {
          this.loading = false;
          if (res.data.resultCode == 1) {
            this.dataList = res.data.data.pageData;
            this.total = res.data.data.count;
          } else {
            this.Error(res.data.message);
          }
        })
      },
      //获取用户id
      getUserId() {
        this.apipost("/api/user/GetMemberUserDropDownList", this.UserMsg, res => {
          if (res.data.resultCode == 1) {
            if (this.choiceOrder) {
              this.searchUserTable = res.data.data.pageData;
            } else {
              this.tableData = res.data.data.pageData;
            }
          }
        })
      },
      //搜索用户id
      ChangeListName(val) {
        this.UserMsg.Name = val;
        this.getUserId();
      },
      //获取供应商下拉
      getSupplierList() {
        this.apipost("/api/Supplier/GetSupplierAllList", {}, res => {
          if (res.data.resultCode == 1) {
            this.options = res.data.data;
          }
        });
      },
      getOrderTable() {
        this.apipost("/api/order/GetRecommendOrdersCommissionPageList", this.orderMsg, res => {
          if (res.data.resultCode == 1) {
            this.orderData = res.data.data.pageData;
          }
        });
      },
      goquery(){
        if( this.orderMsg.UserId==0){
          this.Error('请选择用户')
          return
        }
           if( this.orderMsg.SupplierId==0){
          this.Error('请选择供应商')
          return
        }
        this.orderMsg.pageIndex =1
        this.getOrderTable()
      
      },
      handleSelectionChange(val) {
        this.IntroductionIds = val;
      },
      //保存选择
      saveOrderChoice() {
        
        this.addOrderMsg.IntroductionIds = [];
        this.IntroductionIds.forEach(x => {
          this.addOrderMsg.IntroductionIds.push(x.OrderIntroduction.IntroductionId);
        })
        this.addOrderMsg.SupplierId = this.orderMsg.SupplierId;
        this.addOrderMsg.UserId = this.orderMsg.UserId;
        if (this.addOrderMsg.SupplierId == 0) {
          this.Error('请选择供应商');
          return;
        }
        if (this.addOrderMsg.IntroductionIds.length == 0) {
          this.Error('请选择返佣订单');
          return;
        }
        
         const loading = this.$loading({
              lock: true,
              text: '正在生成账单中...',
              spinner: 'el-icon-loading',
            });
        this.apipost("/api/user/SetRecommendOrdersBillInfo", this.addOrderMsg, res => {
          loading.close();
          if (res.data.resultCode == 1) {
            this.Success(res.data.message);
            this.choiceOrder = false;
            this.getList();
            //清空勾选
            this.toggleSelection();
          } else {
            this.Error(res.data.message);
          }
        });

      },
      //修改备注
      updateRemark(item) {
        this.upRemarkDialog = true;
        this.remarkMsg.BillId = item.Id;
        this.remarkMsg.Remark = item.Remark;
      },
      //保存修改备注
      saveRemark() {
        this.apipost("/api/user/SetRecommendOrdersBillRemark", this.remarkMsg, res => {
          if (res.data.resultCode == 1) {
            this.Success(res.data.message);
            this.upRemarkDialog = false;
            this.getList();
          } else {
            this.Error(res.data.message)
          }
        });

      },
      //跳转至账单明细
      goOrderDetail(item) {
        this.$router.push({
          name: 'RecomBillDetail',
          query: {
            BillId: item.Id,
            blank: "y"
          }
        });
      },
      //取消账单
      cancelOrder(item) {
        let that = this;
        that.Confirm("是否确认起取消?", function () {
          that.apipost("/api/user/CancalRecommendOrdersBill", {
            BillId: item.Id,
          }, res => {
            if (res.data.resultCode == 1) {
              that.Success(res.data.message);
              that.getList();
            } else {
              that.Error(res.data.message);
            }
          })
        });
      },
      //账单打款
      OrderDakuan(item){
        let that = this;
        that.Confirm("是否确认打款?", function () {
          that.apipost("/api/user/SetRecommendOrdersBillRemit", {
            BillId: item.Id,
          }, res => {
            if (res.data.resultCode == 1) {
              that.Success(res.data.message);
              that.getList();
            } else {
              that.Error(res.data.message);
            }
          })
        });
      },
      //清空多选方法
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      //关闭弹窗事件
      closeDialog(){
        this.toggleSelection();
      }
    },
    mounted() {
      this.getSupplierList();
      this.getUserId();
      this.getList();
    }
  };

</script>
<style>
  .RecommendBill .content {
    background: #fff;
    margin-top: 10px;
    padding: 20px;
    box-sizing: border-box;
  }
  .RecommendBill .rr_BillItem{
    display:inline-block;
    margin-bottom: 0;
  }
  .RecommendBill .Re_leftImage{
    float:left;
    width:90px;
    height:90px;
  }
  .RecommendBill .Re_leftImage img{
    width:100%;
    height:100%;
  }
  .RecommendBill .Re_rightDiv{
    float:left;
    width:76%;
    margin:0 10px;
  }
  .RecommendBill .ReDivList{
    width:100%;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;/*超出3行部分显示省略号，去掉该属性 显示全部*/
    -webkit-box-orient: vertical;
  }
</style>
