<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="支付状态">
              <j-dict-select-tag v-model="queryParam.status" placeholder="请选择"
                                 dictCode="payment_result_status"></j-dict-select-tag>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="姓名">
              <a-input placeholder="请输入姓名" v-model="queryParam.userName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8" >
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleChargeBack" type="primary" icon="rollback">退单处理</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="status" slot-scope="text, record">
            {{text}}
           <a type="link" v-if="record.status === 4" style="margin-left: 10px" @click="checkCause(record)">原因</a>
           <a type="link" v-if="record.status === 4" style="margin-left: 10px" @click="handleBackPay(record)" v-has="'payment:backPay'">补发</a>
        </span>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.status === 3">
            <a @click="handleViewStatement(record)" v-has="'payment:viewDetailStatement'">查看对账单</a>
          </template>
          <template v-else>
            --
          </template>
        </span>
      </a-table>
    </div>
    <HlwPaymentReissueModal ref="modalPayBackForm" @ok="modalFormOk"></HlwPaymentReissueModal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { httpAction, getAction, putAction } from '@/api/manage'
  import HlwPaymentReissueModal from './modules/HlwPaymentReissueModal'

  export default {
    name: "HlwPaymentDetailList",
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      HlwPaymentReissueModal
    },
    data () {
      return {
        description: '付款明细表管理页面',
        // 表头
        columns: [
          {
            title:'姓名',
            align:"left",
            dataIndex: 'userName'
          },
          {
            title:'手机号',
            align:"left",
            dataIndex: 'phone'
          },
          {
            title:'身份证号',
            align:"left",
            dataIndex: 'idcardNumber'
          },
          {
            title:'银行卡号',
            align:"left",
            dataIndex: 'cardNumber'
          },
          {
            title:'付款金额',
            align:"left",
            dataIndex: 'payment'
          },
          {
            title:'到账金额',
            align:"left",
            dataIndex: 'netPayment'
          },
          {
            title:'支付状态',
            align:"left",
            dataIndex: 'status_dictText',
            scopedSlots: { customRender: 'status' }
          },
          {
            title:'银行名称',
            align:"left",
            dataIndex: 'eacbnk',
            customRender: function(text, record) {
              return record.bnkflg === 'Y' ? '招商银行' : text
            }
          },
          {
            title:'开户地',
            align:"left",
            dataIndex: 'rcveaa',
            customRender: function(text, record) {
              return text ? text : '--'
            }
          },
          {
            title:'返回结果时间',
            align:"left",
            dataIndex: 'paymentResultTime',
            customRender: function(text, record) {
              return text ? text : '--'
            }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'left',
            width: 200,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/wisdom/hlwPaymentDetail/list",
          delete: "/wisdom/hlwPaymentDetail/delete",
          deleteBatch: "/wisdom/hlwPaymentDetail/deleteBatch",
          exportXlsUrl: "/wisdom/hlwPaymentDetail/exportXls",
          importExcelUrl: "wisdom/hlwPaymentDetail/importExcel",
          getStatement: '/wisdom/hlwPaymentDetail/getStatement',
          chargeBack: '/wisdom/hlwPaymentDetail/chargeBack',
          imgerver: window._CONFIG['imgDomainOssURL']
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
        }
        this.paymentId = this.$route.query.paymentId;
        var params = this.getQueryParams()//查询条件
        params.paymentId = this.paymentId;
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = res.result.records
            this.ipagination.total = res.result.total;
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
      checkCause(record) {
        this.$error({
          title: '失败原因',
          content: record.failureReason,
          okText: '确定',
          centered: true
        })
      },
      //补发
      handleBackPay(record){
        let that = this
        that.$refs.modalPayBackForm.edit(record)
        that.$refs.modalPayBackForm.title = '补发'
        that.$refs.modalPayBackForm.disableSubmit = false
      },
      modalFormOk(){
        this.loadData(1);
      },
      //查看对账单
      handleViewStatement(record){
        this.loading = true
        getAction(this.url.getStatement, {id: record.id}).then((res) => {
          if (res.success) {
            if(res.result){
              if( res.result.statementUrl){
                var statementUrl = res.result.statementUrl
                statementUrl = this.url.imgerver + statementUrl;
                //预览pdf
                window.open(statementUrl,'_blank','location=no');
              }else{
                this.$message.warning('暂无对账单')
              }
            }else{
              this.$warning({
                title: '提示',
                content: res.message,
                okText: '确定'
              })
            }
          }else{
            this.$warning({
              title: '提示',
              content: res.message,
              okText: '确定'
            })
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //退单处理
      handleChargeBack(){
        const that = this;
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return false;
        }
        const modal = that.$confirm({
          title: '退单处理',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '本次退单选中'+ that.selectedRowKeys.length +'条',
          okText: '确定退单',
          cancelText: '取消',
          onOk: function() {
            that.confirmLoading = true;
            var ids = that.selectedRowKeys.join(",");
            console.log("ddf",that.selectedRowKeys,ids)
            putAction(that.url.chargeBack, { ids:ids }).then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData(1)
              } else {
                that.$error({
                  title: '退单失败',
                  content: res.message,
                  okText: '确定'
                })
              }
            })
          },
          onCancel(e) {
            modal.destroy()
          }
        })
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>