<template>
  <div class="center">
    <div class="center_css">
      <div class="top_css">
        <el-row>
          <el-col :span="14">
          <el-button type="primary" @click="deferPay">暂缓支付</el-button>
          <!-- <el-button type="primary" @click="batchpayment">批量付款</el-button> -->
          <el-date-picker class='date' v-model="value1" value-format="yyyy-MM-dd" @change="dateChange" type="daterange"
              range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
          </el-col>
          <el-col :span="10">
          <div class="screen">
            <el-input class='find' v-model="searchkeyWord" @keyup.enter.native="find" placeholder="可按司机姓名和订单编号查找" clearable />
            <el-button class="search" @click="find"><img width="16" height="16" style="margin-left: -8px"
                  src="../../../public/img/sousuo.png"></el-button>
          </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <div class="search_btn">
              <div :class="searchType == '' ? 'search' : 'searchNo'" class="search_item search_block"
                @click="searchBtn('')">
                全部
              </div>
              <div :class="searchType == 1 ? 'search' : 'searchNo'" class="search_item" @click="searchBtn(1)">
                待支付
              </div>
              <div :class="searchType == 2 ? 'search' : 'searchNo'" class="search_item" @click="searchBtn(2)">
                已支付
              </div>
              <div :class="searchType == 4 ? 'search' : 'searchNo'" class="search_item" @click="searchBtn(4)">
                已到账
              </div>
              <div :class="searchType == 3 ? 'search' : 'searchNo'" class="search_item" @click="searchBtn(3)">
                暂缓中
              </div>
            </div>
          </el-col>
        </el-row>
      </div>
      <el-table @selection-change="handleSelectionChange" :data="tableData" style="width: 98%; margin: 0 auto; border-radius: 10px" height="68vh" border highlight-current-row>
        <el-table-column type="selection" :selectable="checkSelectable" width="55"></el-table-column>
        <!-- <el-table-column prop="index" label="序号" >
          <template slot-scope="scope">
            {{scope.row.$index}}
          </el-table-column>
        </el-table-column> -->
        <el-table-column type="index" width="60"  label="序号" align="center">
            <template scope="scope">
                <span>{{scope.$index + 1}}</span>
                <!-- <span v-else>{{scope.$index}}</span> -->
            </template>
        </el-table-column>
        <el-table-column min-width="100" prop="orderNo" label="订单编号" >
          <template scope="scope">
            <span>{{scope.row.orderNo}}</span>
            <span class="pad_css" v-if="scope.row.freightAdvance == '1'">垫</span>
          </template>
        </el-table-column>
        <el-table-column prop="payeeName" label="收款人姓名" />
        <el-table-column min-width="160" prop="issuingDate" label="开户行" >
          <template scope="scope">
            <span>{{scope.row.bankDeposit}}-{{scope.row.bankDepositBranch}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="bankCard" min-width="120" label="银行卡号" />
        <el-table-column prop="paymentType" label="付款类型" />
        <el-table-column prop="amountMoney" label="金额（元）" />
        <el-table-column prop="paymentDate" min-width="120" label="付款时间" />
        <el-table-column prop="paymentDate" label="支付凭证" >
          <template scope="scope">
            <el-link v-if='scope.row.paymentVoucher' target="_blank" type="primary" :underline="false" @click="paymentVoucher(scope.row)">查看</el-link>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" >
          <template scope="scope">
            <span>{{scope.row.status}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="50">
          <template slot-scope="scope">
            <el-link :disabled="scope.row.status!='待支付'" target="_blank" type="primary" :underline="false" @click="payment(scope.row)">付款</el-link>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-pagination :current-page="currentPage" style="text-align: center; margin-top: 10px"
      :page-size="deptCircularPage.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="deptBudgetTotal"
      @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    <el-dialog :close-on-click-modal='false' title="支付凭证" :visible.sync="fujianInfo" width="830px" :before-close="fujianClose">
      <div class="file">
        <img :src="file.paymentVoucher" class="img_css" @click="enlarge(file.paymentVoucher)" />
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal='false' :title="title" :visible.sync="paymentshow" width="830px" :before-close="paymentshowClose">
      <div >
        <el-form ref="form" auto-complete="on" :model="currectdata" :rules="Rules" label-width="100px">
          <el-form-item label="收款人">
            {{currectdata.payeeName}}
          </el-form-item>
          <el-form-item label="卡号">
           {{currectdata.bankCard}}
          </el-form-item>
          <el-form-item label="金额">
           {{currectdata.amountMoney}}
          </el-form-item>

          <el-form-item label="流水号" prop='serialNumber'>
            <el-input placeholder="输入流水号" v-model="currectdata.serialNumber"></el-input>
            <!-- <el-input type="textarea" v-model="currectdata.serialNumber"></el-input> -->
          </el-form-item>
          <el-form-item label="支付凭证" prop='paymentVoucher'>
            <el-upload ref='upload' class="avatar-uploader" :action="uploadPath" :show-file-list="false"
              :on-success="uploadSuccess">
              <img v-if="currectdata.paymentVoucher" :src="currectdata.paymentVoucher" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <!-- <el-input type="textarea" v-model="currectdata.serialNumber"></el-input> -->
          </el-form-item>
          <div class="btn">
            <el-button @click="paymentsubmit">提交</el-button>
          </div>
        </el-form>
      </div>
      
    </el-dialog>
    <el-image-viewer v-if="imgsVisible" :on-close="closeImgViewer" :url-list="srcList" style="z-index:9999" />
  </div>
</template>
<script>
  import {
    uploadPath
  } from '@/api/officialWebsiteManagement'
import {
  getListdriverfreight,
  deferpay,
  batchpay
} from '@/api/settlementManagement'
  export default {
    components: {
      "el-image-viewer": () =>
        import("element-ui/packages/image/src/image-viewer"),
    },
    data() {
      return {
        uploadPath,
        startDate: '',
        endDate: '',
         value1: '',
        tableData: [],
        // 分页
        searchkeyWord: '',
        currentPage: 1,
        pageSize: 10,
        deptBudgetTotal: 0,
        deptCircularPage: {},
        imgsVisible:false,
        Rules: {
          serialNumber: [{
              required: true,
              message: "请输入流水号",
              trigger: "blur",
            },
            {
              min: 6,
              max: 30,
              message: "流水号输入错误",
              trigger: "blur",
            },
          ],
          paymentVoucher: [{
              required: true,
              type:'string',
              message: "支付凭证不能为空",
              trigger: "change",
            }
          ],
        },
        multipleSelection:[],
        search: 1,
        searchType:'1',
        file:{},
        title:'',
        disabled: false,
        fujianInfo:false,
        paymentshow:false,
        currectdata:{},
        srcList:[]
      }
    },
    mounted() {
      this.getPassYearFormatDate()
      this.getList()
    },
    methods: {
      enlarge(url) {
        this.imgsVisible = true;
        this.srcList.push(url)
      },
      closeImgViewer() {
        this.srcList = []
        this.imgsVisible = false;
      },
      fujianClose(){
        this.fujianInfo=false
      },
      uploadSuccess(res) {
        console.log(res.data)
        this.currectdata.paymentVoucher=res.data.url
      },
      paymentshowClose(){
        this.paymentshow=false
      },
      checkSelectable(row) {
        if(row.status!='已付款'){
          return true
        }else{
          return false
        }
      },
      searchBtn(status){
        this.searchType=status
        this.getList()
      },
      dateChange(e) {
        this.startDate = e[0]
        this.endDate = e[1]
        this.getList()
      },
      // 默认日期
      getPassYearFormatDate() {
        var nowDate = new Date()
        var _date = new Date()
        var year = nowDate.getFullYear()-1
        var year1 = _date.getFullYear()
        var month = nowDate.getMonth() + 1
        var month1 = _date.getMonth() + 1
        var strDate = nowDate.getDate()
        var strDate1 = _date.getDate()
        if (month >= 1 && month <= 9) {
          month = '0' + month
        }
        if (month1 >= 1 && month1 <= 9) {
          month1 = '0' + month1
        }

        if (strDate >= 0 && strDate <= 9) {
          strDate = '0' + strDate
        }
        if (strDate1 >= 0 && strDate1 <= 9) {
          strDate1 = '0' + strDate1
        }
        this.startDate = year + '-' + month + '-' + strDate
        this.endDate = year1 + '-' + month1 + '-' + strDate1
        this.value1 = [this.startDate, this.endDate]
      },
      deferPay() {
        if(this.multipleSelection.length>0){
          this.$confirm('确定暂缓支付选中的条目？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.listLoading = true
            var _obj = {}
            _obj.hyFreightSettlementInfos=this.multipleSelection
            deferpay(_obj).then(response => {
                this.$notify({
                  title: '成功',
                  message: '操作成功！',
                  type: 'success'
                })
                this.getList()
                this.listLoading = false
              })
              .catch(() => {
                this.loading = false
              })
          })
        }else{
          this.$notify({
            title: '失败',
            message: '至少选中一条待支付的条目！',
            type: 'error'
          })
        }
       
      },
      paymentVoucher(row){
        this.file=row
        this.fujianInfo=true
      },
      batchpayment(){
        if(this.multipleSelection.length>0){
          var money=0
          var name=[]
          for (let i = 0; i < this.multipleSelection.length; i++) {
            money+=this.multipleSelection[i].amountMoney
            if(name.indexOf(this.multipleSelection[i].driverName)==-1){
              name.push(this.multipleSelection[i].driverName)
            }
          }
          this.$confirm(`确定支付${this.multipleSelection[0].driverName}等${name.length}人运费合计${money}元？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.listLoading = true
            var _obj = {}
            _obj.hyFreightSettlementInfos = this.multipleSelection
            batchpay(_obj).then(response => {
                this.$notify({
                  title: '成功',
                  message: '支付成功！',
                  type: 'success'
                })
                this.getList()
                this.listLoading = false
              })
              .catch(() => {
                this.loading = false
              })
          })
        }else{
          this.$notify({
            title: '失败',
            message: '至少选中一条待支付的条目！',
            type: 'error'
          })
        }
      },
      // payment(row){
      //   this.$confirm(`确定支付${row.driverName}运费${row.amountMoney}元？`, '提示', {
      //       confirmButtonText: '确定',
      //       cancelButtonText: '取消',
      //       type: 'warning'
      //     })
      //     .then(() => {
      //       this.listLoading = true
      //       var _obj = {}
      //       _obj.hyFreightSettlementInfos = [row]
      //       batchpay(_obj).then(response => {
      //           this.$notify({
      //             title: '成功',
      //             message: '支付成功！',
      //             type: 'success'
      //           })
      //           this.getList()
      //           this.listLoading = false
      //         })
      //         .catch(() => {
      //           this.loading = false
      //         })
      //     })
      // },
      paymentsubmit(){
        this.$refs.form.validate(async (valid) => {
          if (valid) {
          this.$confirm(`确定提交付款信息？`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          })
          .then(() => {
            this.paymentshow=false
            this.listLoading = true
            var _obj = {}
            _obj.hyFreightSettlementInfos = [this.currectdata]
            batchpay(_obj).then(response => {
                this.$notify({
                  title: '成功',
                  message: '提交成功！',
                  type: 'success'
                })
                
                this.$refs.upload.clearFiles();
                this.getList()
                this.listLoading = false
              })
              .catch(() => {
                // this.$notify({
                //   title: '失败',
                //   message: '提交失败！',
                //   type: 'error'
                // })
                this.currectdata.paymentVoucher=''
                this.$refs.upload.clearFiles();
                this.loading = false
              })
          })
          }else {
            console.log('error submit!!');
            return false;
          }
        })
      },
      payment(row){
        this.title='支付'+row.paymentType
        this.currectdata=row
        this.paymentshow=true
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      getList() {
        this.loading = true
        const _obj = {}
        _obj.currentPage = this.currentPage
        _obj.pageSize = this.pageSize
        _obj.searchKeyWord = this.searchkeyWord
        _obj.startDate =  this.startDate
        _obj.endDate= this.endDate
        _obj.searchType = this.searchType
        getListdriverfreight(_obj).then(response => {
            this.tableData = response.data.records
            this.deptBudgetTotal = response.data.total
            this.listLoading = false
          })
          .catch(() => {
            this.loading = false
          })
      },
      find() {
        this.currectdata = 1
        this.getList()
      },
      look(row) {
        this.$router.push({
          path: 'newsLook',
          query: {
            id: row.id,
          }
        })
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        this.getList()
      },
      handleCurrentChange(val) {
        this.currentPage = val
        console.log(`当前页: ${val}`)
        this.getList()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .center {
    padding: 10px 20px;
    background: #f5f6f7;
    min-height: calc(100vh - 50px);

    .top_css {
      padding: 10px;

      .search_btn {
        display: flex;
        margin-top: 20px;

        .search_item {
          text-align: center;
          font-size: 14px;
          font-weight: 600;
          line-height: 40px;
          width: 112px;
          height: 40px;
          background: #f7f8f9;
          cursor: pointer
        }

        .searchNo {
          color: #323233;
        }

        .search {
          color: #2f53eb;
          background: #ffffff;
        }
      }
    }

    .ask_css {
      position: absolute;
      margin: 3px 0 0 10px;
    }

    .center_css {
      background: #ffffff;
      border-radius: 1px;
      margin-top: 10px;
      padding-bottom: 10px;
    }

    .screen {
      display: flex;

      .search {
        width: 40px;
        height: 40px;
        background: #2f53eb;
        border-radius: 0px 2px 2px 0px;
        border: 1px solid #DCDFE6;
        margin-left:-1px;
      }

      .count_css {
        width: 80px;
        text-align: center;
        line-height: 40px;
        color: #666666;
      }
    }

    .el-button {
      padding: 10px 20px !important;
    }

    .center_css {

      ::v-deep .el-table th,
      ::v-deep .el-table td {
        text-align: center;
      }

      .fujian {
        font-size: 24px;
        color: #409eff;
      }

      .warning {
        font-size: 14px;
        color: #ed1d1d;
      }
    }
  }

  .car_css {
    width: 50%;
    display: inline-block;
    text-align: center;
    margin-top: 30px;
    position: relative;

    .status_css {
      background: #3AC602;
      width: 50px;
      height: 17px;
      position: absolute;
      top: 0px;
      right: 15px;
      border-radius: 3px;
    }

    .carno_css {
      padding: 1px 5px;
      background-color: #F5F6F7;
      color: #666666;
      border-radius: 12px;
      text-align: center;
      width: 100px;
      margin: 10px auto 0;
    }

    .carborder_css {
      border: 1px solid #F0F1F2;
      margin: 15px auto 0;
      width: 90%;
    }
  }

  .tips {
    color: #999999;
    font-size: 14px;
  }

  .car_item {
    width: 200px;
    height: 160px;
    border-radius: 5px;
  }

  .user {
    margin-bottom: 20px;

    .id_css {
      font-size: 14px;
    }

    .id_css,
    .name_css {
      margin: 5px 0;
      color: #0D0D0D;
    }

    .pay_name {
      color: #9D9D9D;
      font-size: 14px;
    }

    .name_css {
      font-size: 16px;
    }

    .user_item {
      margin: 25px 0;
      width: 450px;
      height: 300px;
    }

    .border_css {
      width: 100%;
      border: 1px solid #F0F1F2;
    }
  }

  ::v-deep .el-table--border .el-table__header th {
    background: #f7f8f9;
  }

  .btn_css {
    color: #409EFF;
    cursor: pointer
  }

  .btn_css1 {
    margin-left: -20px;
  }

  .sign {
    font-size: 14px;
    color: red;
  }

  .form_css {
    width: 100%;
    margin: 20px auto 20px;

    ::v-deep .el-checkbox {
      width: 40%;
      height: 30px;
    }

    // ::v-deep .el-dialog__body{
    // 	padding: 10px 20px;
    // }
    ::v-deep .el-dialog__title {
      font-size: 16px;
    }

    ::v-deep .el-textarea__inner {
      background: #F0F1F2;
    }

    .form_btn {
      text-align: right;
      margin-top: 10px;
    }
  }

  //发送信息
  .Info_css {
    .Info_title {
      color: #323233;
      font-size: 16px;
    }

    .Info_item {
      margin: 20px 0;
    }

    .Info_btn {
      text-align: right;
      margin-top: 10px;
    }
  }

  //附件
  .file {
    .fujian_css {
      width: 631px;
      display: flex;
      margin: 0 auto;
      text-align: center;
      line-height: 32px;
      border: 1px solid #F0F1F2;
      border-right: 0px;
      border-radius: 2px 0px 0px 2px;

      .fujian_item {
        cursor: pointer;
        width: 90px;
        height: 32px;
        border-right: 1px solid #F0F1F2;
      }

      .file_btn {
        color: #2F53EB;
        background-color: #CFDBFE;
      }
    }

    .file_img {
      width: 525px;
      height: 332px;
      margin: 20px auto;
    }

    .img_css {
      width: 525px;
      height: 332px;
    }

    .btn {
      width: 400px;
      margin: 0 auto;
    }
  }
  .date{
    margin:0 10px;
  }
  .find::v-deep input.el-input__inner{
    border-radius:0;
  }
  .pad_css {
    background: #F0F4FF;
    color: #2F53EB;
    padding: 2px;
    margin-left: 10px;
  }
  .avatar-uploader::v-deep .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
