<template>
  <div class="page-header-index-wide">
  <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="8" :sm="8">
            <a-form-item label="时间">
              <a-range-picker
                style="width: 100%"
                v-model="queryParam.generateTimeRange"
                format="YYYY-MM-DD"
                :placeholder="['开始时间', '结束时间']"
                @change="onDateChange"
                @ok="onDateOk"
                :allowClear="allowClear"
              />
            </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>


          <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="ant-alert ant-alert-info" style="margin-bottom: 16px;display: flex;flex-direction: row">
     <div style="display: flex;flex-direction: column"><i class="anticon anticon-info-circle ant-alert-icon"></i> 项目金额合计 <a style="font-weight: 600">{{ this.sumAccount}}元</a></div>

      <div style="display: flex;flex-direction: column;margin-left: 30%;"><i class="anticon anticon-info-circle ant-alert-icon"></i> 服务费合计 <a style="font-weight: 600">{{ this.sumServiceFee}}元</a></div>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <!--<a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>-->
      <!--<i style="float: left;font-family:'STHeiti';font-size: 18px;color: rgba(0, 0, 0, 0.85);font-weight:bold">入账明细</i>-->
      <!--<a-button type="primary" style="float: right;margin-right: 10px;margin-bottom: 5px;z-index: 1" icon="download" @click="handleExportXls1('入账统计')" v-has="'require:export'">导出</a-button>-->
      <!--<a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">-->
        <!--<a-button type="primary" icon="import">导入</a-button>-->
      <!--</a-upload>-->
      <!--<a-dropdown v-if="selectedRowKeys.length > 0">-->
        <!--<a-menu slot="overlay">-->
          <!--<a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>-->
        <!--</a-menu>-->
        <!--<a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>-->
      <!--</a-dropdown>-->
    </div>

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

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange">

       <span slot="action" slot-scope="text, record">
         <template v-if="record.isThroughPlatform==0">
          <a @click="viewServiceContract(record)" v-has="'require:viewServiceContract'">企业合同</a>
         </template>
          <template v-else>
            <a @click="viewSubcontract(record)" v-has="'require:subcontractorInfo'">外包合同</a>
          </template>
          <a-divider type="vertical"/>
            <a @click="subcontractorInfo(record)" v-has="'require:subcontractorInfo'">出账信息</a>
        </span>

      </a-table>
    </div>
     <HwPaymentOutModal ref="hwPaymentOutModal" @ok="modalFormOk"></HwPaymentOutModal>

  </a-card>
  </div>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import qs from 'qs';
  import JDate from '@/components/jeecg/JDate'
  import { filterObj } from '@/utils/util';
  import moment from "moment"
  import { getAction,postAction,deleteAction,downFile } from '@/api/manage'
  import { filterMultiDictText } from '@/components/dict/JDictSelectUtil'
  import HwPaymentOutModal from '../subcontratormanager/modules/HwPaymentOutModal'
  export default {
    name: "HwPlatformCompanyAccountDetailZSList",
    mixins:[JeecgListMixin],
    components: {
      HwPaymentOutModal,
      JDate,

    },
    data () {
      return {
        queryParam: {
          generateTimeRange:[moment(new Date(new Date().getTime()-3600*24*1000*30), 'YYYY/MM/DD'), moment(new Date(), 'YYYY/MM/DD')],
          generateTimeRange_begin:new Date(new Date().getTime()-3600*24*1000*30).toLocaleDateString()+" 00:00:00",
          generateTimeRange_end:new Date().toLocaleDateString() +" 23:59:59"
        },
        sumAccount:0,
        sumServiceFee:0,
        description: '入账统计',
        allowClear:false,
        // 表头
        columns: [
          {
            title:'业务单号',
            align:"center",
            dataIndex: 'hwRequirement.code'
          },
          {
            title:'需求方/平台',
            align:"center",
            dataIndex: 'hwRequirement.companyName',
            customRender:function (text,record) {
              if(record.isThroughPlatform==1){
                return record.enterpriseName
              }else{
                return record.hwRequirement.companyName
              }
            }
          },
          {
            title:'服务商',
            align:"center",
            dataIndex: 'paymentId',
            customRender:function (text,record) {
                return record.subName
            }
          },
          {
            title:'项目金额',
            align:"center",
            dataIndex: 'amount',
            customRender:function (text) {
             if(text==null){
               return "补发"
             }else{
               return text;
             }
            }
          },
          {
            title:'服务费',
            align:"center",
            dataIndex: 'serviceFee',
            customRender:function (text,record) {
              if(record.isThroughPlatform==1){
                return record.subServiceFee
              }else{
                return record.serviceFee
              }
            }

          },
          {
            title:'时间',
            align:"center",
            dataIndex: 'businessTime',
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/saasmanager/hwPlatformCompanyAccountDetail/countlist",
          delete: "/saasmanager/hwPlatformCompanyAccountDetail/delete",
          deleteBatch: "/saasmanager/hwPlatformCompanyAccountDetail/deleteBatch",
          exportXlsUrl: "/saasmanager/hwPlatformCompanyAccountDetail/exportXls",
          importExcelUrl: "saasmanager/hwPlatformCompanyAccountDetail/importExcel",
          exportXls:'/online/cgform/api/exportXls/',
        },
        dictOptions:{
        } 
      }
    },
    created(){
      console.log(this.queryParam.generateTimeRange_end)
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      moment,
      initDictConfig(){
      },

      getQueryParams(){
        console.log(this.queryParam.generateTimeRange)
        var param = Object.assign({}, this.queryParam,this.isorter);
        param.field = this.getQueryField();
        param.pageNo = this.ipagination.current;
        param.pageSize = this.ipagination.pageSize;
        delete param.generateTimeRange; // 时间参数不传递后台
        return filterObj(param);
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0],dateString[1]);
        this.queryParam.generateTimeRange_begin=dateString[0]+" 00:00:00";
        this.queryParam.generateTimeRange_end=dateString[1] +" 23:59:59";
      },
      onDateOk(value) {
        console.log(value);
      },
      handleExportXls1(){
        let param = {};
        if(this.selectedRowKeys && this.selectedRowKeys.length>0){
          param['selections'] = this.selectedRowKeys.join(",")
        }
        console.log("导出参数",param)
        let paramsStr = JSON.stringify(filterObj(param));
        downFile(this.url.exportXlsUrl,{paramsStr:paramsStr}).then((data)=>{
          if (!data) {
            this.$message.warning("文件下载失败")
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(new Blob([data]), this.description+'.xls')
          }else{
            let url = window.URL.createObjectURL(new Blob([data]))
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', this.description+'.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
        })
      },

      loadData(arg) {
        if (!this.url.list) {
          this.$message.error('请设置url.list属性!')
          return
        }
        if(arg){
           this.ipagination.current=arg
        }
        var params = this.getQueryParams()//查询条件
        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.result.records.length>0){
              this.sumAccount=this.returnFloat(res.result.records[0].sumAccount)
              this.sumServiceFee=this.returnFloat(res.result.records[0].sumServiceFee)
            }else{
              this.sumServiceFee=this.returnFloat(0)
              this.sumAccount=this.returnFloat(0)
            }
          }
          if (res.code === 510) {
            this.$message.warning(res.message)
          }
          this.loading = false
        })
      },
    returnFloat(value){
      var value=Math.round(parseFloat(value)*100)/100;
      var s=value.toString().split(".");
      if(s.length==1){
        value=value.toString()+".00";
        return value;
      }
      if(s.length>1){
        if(s[1].length<2){
          value=value.toString()+"0";
        }
        return value;
      }
    },
      viewSubcontract(record){
        if(record.hwRequirement.subcontract==null){
          this.$message.warning('该合同为线下合同,不支持预览')
          return
        }
        window.open(window._CONFIG['imgDomainOssURL']+record.hwRequirement.subcontract, '_blank', 'toolbar=yes')
      },
      viewServiceContract(record){
        if(record.hwRequirement.serviceContract==null){
          this.$message.warning('该合同为线下合同,不支持预览')
          return
        }
        window.open(window._CONFIG['imgDomainOssURL']+record.hwRequirement.serviceContract, '_blank', 'toolbar=yes')
      },
      subcontractorInfo(record){
        this.$refs.hwPaymentOutModal.edit(record)
        this.$refs.hwPaymentOutModal.title = '出账信息'
        this.$refs.hwPaymentOutModal.disableSubmit = false
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>