<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="付款单号">
              <a-input placeholder="请输入付款单号" v-model="queryParam.paymentCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="付款企业">
              <a-input placeholder="请输入付款企业" v-model="queryParam.companyName"></a-input>
            </a-form-item>
          </a-col>

          <template v-if="toggleSearchStatus">
            <a-col :md="6" :sm="8">
              <a-form-item label="状态">
                <j-dict-select-tag v-model="queryParam.status" placeholder="请选择"
                                   dictCode="payment_status"></j-dict-select-tag>
              </a-form-item>
            </a-col>
          </template>
          <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>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">

    </div>
    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowClassName="payFail"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="paymentCode" slot-scope="text,record">
          <a @click="toDetail(record)">{{text}}</a>
        </span>

        <span slot="action" slot-scope="text, record">
          <template v-if="record.status === 1">
             <a @click="handleReview(record)" v-has="'payment:payment'">支付</a>
             <a-divider type="vertical" v-has="'payment:editService'"/>
             <a @click="handleEditService(record)" v-has="'payment:editService'">修改服务费</a>
             <a-divider type="vertical" v-has="'payment:goBack'"/>
             <a @click="handleBack(record)" v-has="'payment:goBack'">打回</a>
          </template>
          <template v-else-if="record.status === 3">
            <a @click="handleViewStatement(record)" v-has="'payment:viewStatement'">查看对账单</a>
          </template>
          <template v-else>
            --
          </template>
        </span>

      </a-table>
    </div>
    <stepForm ref="payModalForm" @ok="modalFormOk"></stepForm>
    <HlwPaymentError ref="errorModalForm" @ok="modalFormOk"></HlwPaymentError>
    <step-form-subcontractor ref="payModalFormSubcontractor" @ok="modalFormOk"></step-form-subcontractor>
    <HlwPaymentEditServiceFeeModal ref="editServiceFeeModal" @ok="modalFormOk"></HlwPaymentEditServiceFeeModal>
  </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 stepForm from './modules/HlwPaymentStepForm'
  import HlwPaymentError from './modules/HlwPaymentError'
  import { Arithmetic } from '@/mixins/Arithmetic'
  import stepFormSubcontractor from './modules/HlwPaymentStepFormSubcontractor'
  import HlwPaymentEditServiceFeeModal from './modules/HlwPaymentEditServiceFeeModal'

  export default {
    name: "HlwPaymentList",
    mixins:[JeecgListMixin, mixinDevice,Arithmetic],
    components: {
      stepForm,
      HlwPaymentError,
      stepFormSubcontractor,
      HlwPaymentEditServiceFeeModal
    },
    data () {
      return {
        description: '付款信息表管理页面',
        // 表头
        columns: [
          {
            title:'付款应用',
            align:"left",
            dataIndex: 'applicationName'
          },
          {
            title:'付款单号',
            align:"left",
            dataIndex: 'paymentCode',
            scopedSlots: { customRender: 'paymentCode' }
          },
          {
            title:'付款企业',
            align:"left",
            dataIndex: 'companyName'
          },
          {
            title:'服务商',
            align:"left",
            dataIndex: 'subcontractorName',
            customRender: (value,record) => {
              return value ? value:'--'
            }
          },
          {
            title:'付款合计',
            align:"left",
            dataIndex: 'payment'
          },
          {
            title:'付款类型',
            align:"left",
            dataIndex: 'paymentType_dictText'
          },
          {
            title:'服务费',
            align:"left",
            dataIndex: 'serviceFee'
          },
          {
            title:'状态',
            align:"left",
            dataIndex: 'status_dictText'
          },
          {
            title:'通过平台',
            align:"left",
            dataIndex: 'isThroughPlatform_dictText'
          },
          {
            title:'付款时间',
            align:"left",
            dataIndex: 'paymentTime',
            customRender: (value,record) => {
              return value ? value:'--'
            }
          },
          {
            title:'备注',
            align:"left",
            dataIndex: 'remark'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'left',
            width: 200,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/wisdom/hlwPayment/list",
          delete: "/wisdom/hlwPayment/deleteBack",
          deleteBatch: "/wisdom/hlwPayment/deleteBatch",
          exportXlsUrl: "/wisdom/hlwPayment/exportXls",
          importExcelUrl: "/wisdom/hlwPayment/importExcel",
          balance: "/wisdom/hlwPayment/checkBalance",
          getStatement: '/wisdom/hlwPayment/getStatement',
          imgerver: window._CONFIG['imgDomainOssURL']
        },
        dictOptions:{},
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig(){
      },
      //付款明细
      toDetail(record) {
        this.$router.push({ path: '/wisdom/hlwPaymentDetail/list',query:{ paymentId:record.id } })
      },
      //支付
      handleReview(record){
        //查看余额是否足够
        this.loading = true
        var params = {id: record.id};
        getAction(this.url.balance, params).then((res) => {
          if (res.success) {
            record = res.result;
            if(record.isThroughPlatform === 1){
                this.$refs.payModalForm.edit(record)
                this.$refs.payModalForm.title = '支付'
                this.$refs.payModalForm.disableSubmit = false
            }else{
                this.$refs.payModalFormSubcontractor.edit(record)
                this.$refs.payModalFormSubcontractor.title = '支付'
                this.$refs.payModalFormSubcontractor.disableSubmit = false
            }
          }else{
            record.accountBalance = res.message;
            this.$refs.errorModalForm.edit(record)
            this.$refs.errorModalForm.title = '提示'
            this.$refs.errorModalForm.disableSubmit = false
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.loading = false
        })
      },
      //打回
      handleBack(record){
        const id = record.id
        let that = this
        const modal = that.$confirm({
          title: '打回付款',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '确定要打回该付款吗？',
          okText: '确定打回',
          cancelText: '取消',
          onOk: function() {
            putAction(that.url.delete, { id }).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()
          }
        })
      },
      handleEditService(record){
        this.$refs.editServiceFeeModal.edit(record)
        this.$refs.editServiceFeeModal.title = '修改服务费'
        this.$refs.editServiceFeeModal.disableSubmit = false
      },
      //操作table行属性
      payFail(record,index){
        if(record.isAllSuccess === 0){
          return "payFail";
        }
      },
      //查看对账单
      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
        })
      }
    }
  }
</script>
<style lang="less" scoped>
  @import '~@assets/less/common.less';
  /deep/ .payFail{
    color: #D9001B;
  }
</style>