<template>
  <div class="app-container">
    <el-form :model="tableFilter" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="系统编号" prop="fCtrlcorpid">
        <el-input
          v-model="tableFilter.fBillno"
          placeholder="请输入系统编号"
          clearable
          size="small"
          style="width:200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货权方" prop="fCtrlcorpid">
        <el-input
          v-model="tableFilter.fCtrlcorpid"
          placeholder="请输入货权方"
          clearable
          size="small"
          style="width:200px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结算单位" prop="fCorpid">
        <el-select
          v-model="tableFilter.fCorpid"
          filterable
          remote
          clearable
          size="small"
          style="width: 200px"
          @keyup.enter.native="handleQuery"
          :remote-method="corpsRemoteMethod"
          placeholder="请输入结算单位"
        >
          <el-option
            v-for="(dict, index) in fMblnoOptions"
            :key="index.fId"
            :label="dict.fName"
            :value="dict.fId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="对账日期" prop="timeInterval">
        <el-date-picker
          type="daterange"
          size="small"
          style="width: 240px"
          v-model="tableFilter.timeInterval"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          :default-time="['00:00:00', '23:59:59']"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['finance:payment:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['finance:payment:edit']"
        >修改
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['finance:charge:remove']"-->
      <!--        >删除-->
      <!--        </el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['finance:payment:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="chargeList" @selection-change="handleSelectionChange" show-summary :summary-method="listTotal">
      <el-table-column type="selection" width="60" align="center"/>
      <el-table-column label="序号" type="index" width="55" align="center"/>
      <el-table-column label="系统编号" :show-overflow-tooltip="true" align="center" prop="fBillno" width="120"/>
      <el-table-column label="货权方" :show-overflow-tooltip="true" sortable align="center" prop="fCtrlcorpid" width="220"/>
      <!--      <el-table-column label="账单日期" align="center" prop="fAccbilldate" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.fAccbilldate, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <!--      <el-table-column label="制单部门" align="center" prop="fId"/>-->
      <!--      <el-table-column label="结算单位" align="center" prop="fCorpid"/>-->
      <el-table-column label="对账日期" align="center" sortable prop="fAccbilldate" width="120"/>
      <el-table-column label="提单号" align="center" sortable prop="tMblno" width="216"/>
      <el-table-column label="应付合计" align="center" prop="fAmtdr" width="120"/>
      <el-table-column label="实付合计" align="center" prop="fAmtcr" width="120"/>
      <el-table-column label="未付金额" align="center" prop="" width="120"/>
      <el-table-column label="备注" align="center" prop="fRemarks" width="120"/>
      <el-table-column label="状态" align="center" prop="fBillstatus" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.fBillstatus == '1'">保存</span>
          <span v-else-if="scope.row.fBillstatus == '2'">暂存</span>
          <span v-else-if="scope.row.fBillstatus == '3'">审批驳回</span>
          <span v-else-if="scope.row.fBillstatus == '4'">提交审核</span>
          <span v-else-if="scope.row.fBillstatus == '5'">审核中</span>
          <span v-else-if="scope.row.fBillstatus == '6'">审核完成</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="check(scope.row,0)"
            v-hasPermi="['finance:payment:edit']"
          >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="check(scope.row,1)"
            v-hasPermi="['finance:payment:edit']"
            v-if="scope.row.fBillstatus == 4 || scope.row.fBillstatus == 5"
          >审批进度</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['finance:payment:edit']"
            v-if="scope.row.fBillstatus <= 3"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['finance:payment:remove']"
            v-if="scope.row.fBillstatus <= 3"
          >删除
          </el-button>
          <!--            <el-button-->
          <!--              size="mini"-->
          <!--              type="text"-->
          <!--              icon="el-icon-delete"-->
          <!--              v-if="scope.row.fBillstatus === 6"-->
          <!--            >查看-->
          <!--            </el-button>-->
          <!--            <el-button-->
          <!--              size="mini"-->
          <!--              type="text"-->
          <!--              icon="el-icon-delete"-->
          <!--              v-if="scope.row.fBillstatus > 3 && scope.row.fBillstatus < 6"-->
          <!--            >查看审批流-->
          <!--            </el-button>-->
        </template>
      </el-table-column>
      <!--      <el-table-column label="单据类型" align="center" prop="fBilltype"/>-->
      <!--      <el-table-column label="制单部门" align="center" prop="fDeptid"/>-->

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改财务数据主对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :show-close="Xbutton" :visible.sync="open" width="70%" append-to-body>
      <el-form :model="queryParams" :rules="ruless" ref="ruless" :inline="true" v-show="showSearch"
               label-width="68px"
      >
        <el-form-item label="货权方" prop="fCtrlcorpid">
          <el-input
            v-model="queryParams.fCtrlcorpid"
            placeholder="请输入货权方"
            clearable
            size="small"
            :disabled="notChange"
            style="width:200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="结算单位" label-width="78px" prop="fCorpid">
          <el-select
            v-model="queryParams.fCorpid"
            filterable
            remote
            clearable
            size="small"
            style="width: 200px"
            @keyup.enter.native="handleQuery"
            :remote-method="corpsRemoteMethod"
            :disabled="doNot"
            placeholder="请输入结算单位"
          >
            <el-option
              v-for="(dict, index) in fMblnoOptions"
              :key="index.fId"
              :label="dict.fName"
              :value="dict.fId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="结算日期" label-width="78px" prop="fAccbilldate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="queryParams.fAccbilldate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择账单日期"
                          :disabled="notChange"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="系统编号" prop="fBillno">
          <el-input
            v-model="queryParams.fBillno"
            placeholder="请输入系统编号"
            clearable
            disabled
            style="width: 200px"
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="备注" prop="fRemarks">
          <el-input
            v-model="queryParams.fRemarks"
            placeholder="请输入备注"
            clearable
            size="small"
            style="width:200px"
            :disabled="notChange"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </el-form>
      <div style="width: 100%;">
        <el-button type="warning" size="small" @click="charGe" :disabled="notChange" v-if="queryParams.fBillstatus < '4'">付费</el-button>
        <el-button type="primary" size="small" @click="confirmCharge" :disabled="notChange" v-if="queryParams.fBillstatus < '4'">确认付费</el-button>
        <el-button type="success" size="small" @click="revokeCharge" v-if="queryParams.fBillstatus === '6'">撤销付费</el-button>
<!--        <el-button type="info" size="small" @click="printing">打印</el-button>-->
        <!--        <el-button type="danger" size="small" :disabled="notChange" v-show="Lander == Operator">撤销审批</el-button>-->
        <el-button type="danger" size="small" :disabled="tablefilter" @click="approvalRevocation" v-show="queryParams.fBillstatus === '4'">撤销审批</el-button>
      </div>
      <el-table v-loading="loading" :data="increase_s" @selection-change="handleSelectionChange_s">
        <!-- <el-table-column type="selection" width="55" align="center"/> -->
        <el-table-column label="序号" type="index" align="center"/>
        <el-table-column label="提单号" align="center" prop="fMblno"/>
        <el-table-column label="存货单号" align="center" prop="fBscorpno"/>
        <el-table-column label="品名" align="center" prop="fProductName"/>
        <el-table-column label="业务日期" align="center" prop="fBsdate">
          <template slot-scope="scope">
            <span>{{ scope.row.fBsdate.slice(0, 10) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="费用名称" align="center" prop="fFeeName"/>
        <el-table-column label="收/付" align="center" prop="fSrcdc">
          <template slot-scope="scope">
            <span v-if="scope.row.fSrcdc =='D'">收</span>
            <span v-else-if="scope.row.fSrcdc =='C'">付</span>
          </template>
        </el-table-column>
        <el-table-column label="业务类型" align="center" prop="fBilltype">
          <template slot-scope="scope">
            <span v-if="scope.row.fBilltype =='SJRK'">入库</span>

            <span v-else-if="scope.row.fBilltype == 'SJCK'">出库</span>
            <span v-else>{{ scope.row.fBilltype }}</span>
          </template>
        </el-table-column>
        <el-table-column label="作业类型" align="center" prop="fBusinessType" />
        <el-table-column label="金额" align="center" prop="fAmtdr"/>
        <el-table-column label="本次金额" align="center" prop="fAmt"/>
        <el-table-column label="备注" align="center" prop="fRemarks"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click.native.prevent="deleteRow(scope.$index, increase_s)"
              :disabled="notChange"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
        <approval-comments v-if="addOrUpdateVisib" ref="ApprovalComments" @refreshDataList="returnData"></approval-comments>
        <el-button v-if="approve === true" @click="immediateApproval">审批</el-button>
        <el-button type="primary" v-if="queryParams.fBillstatus >= '3'" @click="addOrUpdateHandle">查看审批流</el-button>
        <el-button type="primary" @click="submitForm" :disabled="notChange">保 存</el-button>
        <el-button v-if="cancelButton === true" @click="cancel">取 消</el-button>
        <el-button v-if="cancelButton === false" @click="homePage">取 消</el-button>
      </div>
      <el-dialog :close-on-click-modal="false" width="70%" :visible.sync="innerVisible" title="导入数据" append-to-body>
        <el-form ref="form" :model="queryParameter" :rules="rules" label-width="80px"
                 style="display: flex;flex-wrap: wrap;"
        >
          <el-form-item label="货权方" prop="fCtrlcorpid">
            <!--            <el-input v-model="queryParameter.fCorpid" placeholder="请输入货权方" style="width: 200px;"/>-->
            <el-select
              v-model="queryParams.fCorpid"
              filterable
              remote
              clearable
              size="small"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
              :remote-method="corpsRemoteMethod"
              placeholder="请选择货权方"
            >
              <el-option
                v-for="(dict, index) in fMblnoOptions"
                :key="index.fId"
                :label="dict.fName"
                :value="dict.fId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="结算单位" prop="fToCorpid">
            <el-select
              v-model="queryParameter.fToCorpid"
              filterable
              remote
              clearable
              size="small"
              :disabled="doNot"
              style="width: 200px"
              @keyup.enter.native="handleQuery"
              :remote-method="corpsRemoteMethod"
              placeholder="请输入结算单位"
            >
              <el-option
                v-for="(dict, index) in fMblnoOptions"
                :key="index.fId"
                :label="dict.fName"
                :value="dict.fId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提单号" prop="tMblno">
            <el-input v-model="queryParameter.fMblno" placeholder="请输入提单号" style="width: 200px;" size="small"/>
          </el-form-item>
          <el-form-item label="对账单号" prop="fStatementNo">
            <el-input v-model="queryParameter.fStatementNo" placeholder="请输入提单号" size="small" style="width: 200px;"/>
          </el-form-item>
          <el-form-item label="费用名称" prop="fFeeid">
            <template>
              <el-select
                v-model="queryParameter.fFeeid"
                filterable
                remote
                size="small"
                style="width:200px"
                :remote-method="fWRemoteMethod"
                placeholder="费用名称"
                multiple
              >
                <el-option v-for="item in fWbuOptions" :key="item.fId" :label="item.fName" :value="item.fId">
                </el-option>
              </el-select>
            </template>
          </el-form-item>
          <el-form-item label="仓储业务" prop="fFeeid">
            <el-select
              v-model="queryParameter.fBilltype"
              size="small"
              style="width:200px"
              @change="changefBilltype"
              placeholder="请选择仓储业务"
            >
              <el-option label="入库" value="SJRK"></el-option>
              <el-option label="出库" value="SJCK"></el-option>
              <el-option label="货权转移" value="HQZY"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作业类型" prop="fBusinessType">
            <el-select
              v-model="queryParameter.fBusinessType"
              filterable
              remote
              size="small"
              style="width:200px"
              placeholder="作业类型"
              multiple
            >
              <el-option
                v-for="dict in businessTypeOption"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="审核日期" prop="timeExamine">
            <el-date-picker
              size="small"
              style="width: 240px"
              v-model="queryParameter.timeExamine"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="出入库日期" label-width="85px" prop="fAccbilldate">
            <el-date-picker
              size="small"
              style="width: 240px"
              v-model="queryParameter.timeInterval"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
              :default-time="['00:00:00', '23:59:59']"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="importSearch">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery_s">重置</el-button>
          </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="chargeList_s" ref="chargeList" show-summary :summary-method="getSum"
                  @selection-change="handleSelectionChange_s"
        >
          <el-table-column type="selection" width="55" align="center"/>
          <el-table-column label="序号" type="index" width="55" align="center"/>
          <el-table-column label="货权方" align="center" prop="fName"/>
          <el-table-column label="提单号" align="center" prop="fMblno"/>
          <el-table-column label="品名" align="center" prop="fProductName"/>
          <el-table-column label="业务日期" align="center" prop="fBsdate"/>
          <el-table-column label="业务类型" align="center" prop="fBilltype">
            <template slot-scope="scope">
              <span v-if="scope.row.fBilltype =='SJRK'">入库</span>
              <span v-else-if="scope.row.fBilltype == 'SJCK'">出库</span>
              <span v-else>{{ scope.row.fBilltype }}</span>
            </template>
          </el-table-column>
          <el-table-column label="作业类型" align="center" prop="fBusinessType" />
          <el-table-column label="审核日期" align="center" prop="fReviewDate"/>
          <el-table-column label="费用名称" align="center" prop="fFeeName"/>
          <el-table-column label="金额" align="center" prop="fAmtdr"/>
          <el-table-column label="本次金额" align="center" prop="fAmt" width="100">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.fAmt"
                placeholder="请输入本次金额"
                clearable
                size="small"
                oninput='this.value=this.value.replace(/[^\-?\d.]/g,"").replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3")'
                @change="imgChangeI(scope.row)"
              />
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="fRemarks">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.fRemarks"
                placeholder="请输入备注"
                clearable
                size="small"
                width="100"
              />
            </template>
          </el-table-column>
        </el-table>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="confirmImport">确 定</el-button>
          <el-button @click="signOut">取 消</el-button>
        </div>
      </el-dialog>
    </el-dialog>

    <!--  打印页面-->
    <el-dialog
      :visible.sync="printStatus"
      width="80%"
      :before-close="closePrinting"
    >
      <el-table id="print_area2" v-loading="loading" ref="table" :data="printObject" @selection-change="handleSelectionChange_s">
        <!-- <el-table-column type="selection" width="55" align="center"/> -->
        <el-table-column label="序号" type="index" width="70" align="center"/>
        <el-table-column label="提单号" align="center" prop="fMblno" width="120"/>
        <el-table-column label="存货单号" align="center" prop="fBscorpno" width="120"/>
        <el-table-column label="品名" align="center" prop="fProductName" width="120"/>
        <el-table-column label="业务日期" align="center" prop="fBsdate" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.fBsdate.slice(0, 10)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="费用名称" align="center" prop="fFeeName" width="120"/>
        <el-table-column label="收/付" align="center" prop="fSrcdc" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.fSrcdc =='D'">收</span>
            <span v-else-if="scope.row.fSrcdc =='C'">付</span>
          </template>
        </el-table-column>
        <el-table-column label="业务类型" align="center" prop="fBilltype" width="120">
          <template slot-scope="scope">
            <span v-if="scope.row.fBilltype =='SJRK'">入库</span>

            <span v-else-if="scope.row.fBilltype == 'SJCK'">出库</span>
            <span v-else>{{ scope.row.fBilltype }}</span>
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="fAmtdr" width="120"/>
        <el-table-column label="本次金额" align="center" prop="fAmt" width="120"/>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" style="marginTop:10px" @click="printSomething">打印</el-button>
        <el-button @click="printStatus = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  collectFee,
  listCharge,
  getCharge,
  delCharge,
  addCharge,
  exportCharge,
  search,
  listCorps,
  delCharge_s,
  backCharge,
  revocation
} from '@/api/finance/payment'
import { listFees } from '@/api/basicdata/fees'
import print from 'print-js'
import AddOrUpdate from '@/views/viewApproval'
import ApprovalComments from '@/views/startApproval'
import { queryUserVal } from '@/api/warehouseBusiness/agreement'
import Global from '@/layout/components/global'

export default {
  name: 'Charge',
  data() {
    return {
      doNot:false,
      Lander:'',
      Operator:'',
      approve:false,
      addOrUpdateVisible: false,
      contrastId:230,
      tablefilter:false,
      hide:false,
      openPrint:false,
      notChange:false,
      browseStatus: true,
      nothing: [],
      //打印表
      printObject: [],
      printStatus: false,
      //导入状态
      state_s: true,
      fWbuOptions: [],
      options: '',
      // 遮罩层
      loading: true,
      //打开内部弹窗
      innerVisible: false,
      chargeList_s: [],
      selection: '',
      increase_s: [],
      cancelButton:true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totAL:0,
      Ttime:0,
      //提单号暂存
      fMblno: '',
      // 财务数据主表格数据
      chargeList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      businessTypeOption:[],
      fMblnoOptions: '',
      //导入查询参数
      queryParameter: {
        fCorpid: '',
        fToCorpid: '',
        fMblno: '',
        fStatementNo: '',
        fFeeid: '',
        timeExamine: '',
        timeInterval: ''
      },
      empty: [],
      //导入从表传主表
      pass: {
        fAmtdr: 0,    //应收合计
        fAmtcr: 0,    //应付合计
        fMblno: '',    //提单号
        fName: ''      //货权方
      },
      Xbutton:true,
      // 查询参数
      queryParams: {
        fId: null,
        pageNum: 1,
        pageSize: 10,
        fBillno: null,
        fCtrlcorpid: null,
        fCorpid: null,
        tMblno: null,
        fAmtdr: null,
        fAmtcr: null,
        fBilltype: null,
        fBillstatus: null,
        fRemarks: null,
        fAccbilldate: null,
        fDeptid: null
      },
      addOrUpdateVisib: false,
      // 主表查询参数
      tableFilter: {
        pageNum: 1,
        pageSize: 10,
        fBillno: null,
        fCtrlcorpid: null,
        fCorpid: null,
        tMblno: null,
        fAmtdr: null,
        fAmtcr: null,
        fBilltype: null,
        fBillstatus: null,
        fRemarks: null,
        fAccbilldate: null,
        fDeptid: null
      },
      approval:[],
      // 表单参数
      form: {},
      // 表单校验
      ruless: {
        fCtrlcorpid: [
          { required: true, message: ' ', trigger: 'blur' }
        ],
        fCorpid: [
          { required: true, message: ' ', trigger: 'blur' }
        ],
        fAccbilldate: [
          { required: true, message: ' ', trigger: 'blur' }
        ]
      },
      // 表单校验
      rules: {
        fBilltype: [
          { required: true, message: ' ', trigger: 'change' }
        ],
        fBillstatus: [
          { required: true, message: ' ', trigger: 'blur' }
        ],
        fDeptid: [
          { required: true, message: ' ', trigger: 'blur' }
        ],
        fToCorpid: [
          { required: true, message: ' ', trigger: 'blur' }
        ],
        timeExamine: [
          { required: true, message: ' ', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    AddOrUpdate,
    ApprovalComments
  },
  created() {
    this.getList()
    this.register()
    this.getDicts("st_in_type").then((response) => {
      this.businessTypeOption = response.data;
    });
  },
  activated(){
    this.adoPt()
  },
  methods: {
    //财务主取消
    signOut(){
      if(this.increase_s == 0){
        this.doNot = false
      }else{
        this.doNot = true
      }
      this.innerVisible = false
      this.feeList = []
    },
    //付费页面合计
    listTotal(param){
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        } else if (index === 6  || index === 7 || index === 8) {
          const values = data.map(item => Number(item[column.property]))
          if (!values.every(value => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
          }
        }
      })
      return sums
    },
    approvalRevocation(){
      let  data = {
        id:this.queryParams.fId,
        actId:this.contrastId,
        billId:this.queryParams.fId
      }
      revocation(data).then(data =>{
        if (data.code === 200){
          this.$message.success('撤销成功');
          this.open = false
          this.getList()
        }
      })
    },
    changefBilltype(){
      this.businessTypeOption = []
      if (this.queryParameter.fBilltype == 'SJRK'){
        this.getDicts("st_in_type").then((response) => {
          this.businessTypeOption = response.data;
        });
      }else if (this.queryParameter.fBilltype == 'SJCK'){
        this.getDicts("st_out_type").then((response) => {
          this.businessTypeOption = response.data;
        });
      }else if(this.queryParameter.fBilltype == 'HQZY'){
        this.getDicts("st_trans_type").then((response) => {
          this.businessTypeOption = response.data;
        });
      }
    },
    returnData(){
      this.addOrUpdateVisib = false
      this.open = false
      this.homepaGe()
    },
    getDataList(){
      this.addOrUpdateVisible = false
    },
    homepaGe(){
      let view = {
        fullPath: "/finance/charge",
        hash: "",
        matched: Array(2),
        meta: Object,
        name: "Charge",
        params: Object,
        path: "/finance/charge",
        query: Object,
        title: "付费"
      }
      this.$router.push({ path: '/index'})
      this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
        if (this.isActive(view)) {
          this.toLastView(visitedViews, view)
        }
      })
      Global.$emit("removeCache", "closeSelectedTag", view);
    },
    adoPt(){
      this.approval = this.$route.query.data
      if (this.approval){
        this.Xbutton = false
        this.approval = JSON.parse(this.approval)
        this.hide = false
        this.tablefilter = true
        this.notChange = true
        this.approve = true
        this.cancelButton = false
        this.reset()
        this.pass = {
          fAmtdr: 0,    //应收合计
          fAmtcr: 0,    //应付合计
          fMblno: '',    //提单号
          fName: '',      //货权方
          fFeesName: '',   //结算单位
          fCorpid: ''     //结算单位ID
        }
        getCharge(this.approval.billId).then(response => {
          this.Operator = response.data.tFee.createBy
          this.increase_s = response.data.feeDoList
          this.fWbuOptions = response.data.feesList
          this.queryParams = response.data.tFee
          this.fWbuOptions = response.data.feesList
          this.fMblnoOptions = response.data.corps
          this.open = true
          this.title = '修改付费列表'
        })
      }
    },
    homePage(){
      this.open = false
      let view = {
        fullPath: "/finance/charge",
        hash: "",
        matched: Array(2),
        meta: Object,
        name: "Charge",
        params: Object,
        path: "/finance/charge",
        query: Object,
        title: "付费"
      }
      this.$router.push({ path: '/index'})
      this.$store.dispatch('tagsView/delView', view).then(({ visitedViews }) => {
        if (this.isActive(view)) {
          this.toLastView(visitedViews, view)
        }
      })
      Global.$emit("removeCache", "closeSelectedTag", view);
    },
    register(){
      queryUserVal().then((response)=>{
        this.Lander = response.user.userName
      })
    },
    immediateApproval(){
      this.addOrUpdateVisib = true
      this.$nextTick(() => {
        this.$refs.ApprovalComments.init(this.approval.billId,this.approval.actId)
      })
    },
    // 查看审批流
    addOrUpdateHandle(){
      this.addOrUpdateVisible = true
      this.addOrUpdateVisib = false
      let id = '448'
      let actId = '110'
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(this.queryParams.fId,this.contrastId)
      })
    },

    // 撤销付费
    revokeCharge(){
      this.queryParams.fBillstatus = '1'
      let formDate = new window.FormData()
      formDate.append('tFee', JSON.stringify(this.queryParams))
      formDate.append('tFeeDo', JSON.stringify(this.increase_s))
      backCharge(formDate).then(response => {
        this.open = false
        this.msgSuccess('操作成功')
        this.getList()
      })
    },

    // 查看按钮
    check(row,res){
      this.notChange = true
      getCharge(row.fId).then(response => {
        this.Operator = response.data.tFee.createBy
        this.increase_s = response.data.feeDoList
        this.fWbuOptions = response.data.feesList
        this.queryParams = response.data.tFee
        this.fWbuOptions = response.data.feesList
        this.fMblnoOptions = response.data.corps
        this.open = true
        this.title = '付费列表'
        this.tablefilter = true
        if (res == 1){
          this.notChange = true
          if (this.Operator == this.Lander){
            this.tablefilter = false
            this.reset()
            this.pass = {
              fAmtdr: 0,    //应收合计
              fAmtcr: 0,    //应付合计
              fMblno: '',    //提单号
              fName: '',      //货权方
              fFeesName: '',   //结算单位
              fCorpid: ''     //结算单位ID
            }
            const fId = row.fId || this.ids
            getCharge(fId).then(response => {
              this.increase_s = response.data.feeDoList
              this.fWbuOptions = response.data.feesList
              this.queryParams = response.data.tFee
              this.fWbuOptions = response.data.feesList
              this.fMblnoOptions = response.data.corps
              this.open = true
            })
          }else {
            this.notChange = true
            // this.$message.error('未知异常，请联系管理员')
          }
        }else {
          this.notChange = true
          this.reset()
          this.pass = {
            fAmtdr: 0,    //应收合计
            fAmtcr: 0,    //应付合计
            fMblno: '',    //提单号
            fName: '',      //货权方
            fFeesName: '',   //结算单位
            fCorpid: ''     //结算单位ID
          }
          const fId = row.fId || this.ids
          getCharge(fId).then(response => {
            this.increase_s = response.data.feeDoList
            this.fWbuOptions = response.data.feesList
            this.queryParams = response.data.tFee
            this.fWbuOptions = response.data.feesList
            this.fMblnoOptions = response.data.corps
            this.open = true
          })
        }
      })
    },
    //打印功能
    printing() {
      if (this.increase_s.length !== 0) {
        this.printStatus = true
        this.printObject = this.increase_s
      } else {
        this.$message.error('无数据，请检查是否有数据')
      }
    },
    //确认打印
    printSomething() {
      // 此处的style即为打印时的样式
      const style ='table tr td,th { border-collapse: collapse;padding:0px;border:.5px #000 solid;text-align:center;}'
      print({
        printable: 'print_area2',
        type: 'html',
        header:"付费表",
        style: style, // 亦可使用引入的外部css;
        scanStyles: false
      })
    },
    //关闭打印弹窗
    closePrinting() {
      this.$confirm('确认关闭？')
        .then(_ => {
          this.printStatus = false
        })
        .catch(_ => {

        })
    },
    // 确认付费
    confirmCharge() {
      this.$refs['ruless'].validate(valid => {
        if (valid) {
          if(this.increase_s.length){
            // this.queryParams.fBillstatus = '4'
            let formDate = new window.FormData()
            formDate.append('tFee', JSON.stringify(this.queryParams))
            formDate.append('tFeeDo', JSON.stringify(this.increase_s))
            collectFee(formDate).then(response => {
              this.msgSuccess('操作成功')
              this.open = false
              this.getList()
            })
          }else {
            this.$message.error('表单为空不允许操作');
          }
        }
      })
    },
    charGe() {
      this.queryParameter = {
        fToCorpid: this.queryParams.fCorpid,
      }
      this.chargeList_s = []
      // this.queryParameter.fToCorpid = this.queryParams.fCorpid
      this.innerVisible = true
      if(this.queryParams.fCorpid){
        this.doNot = true
      }
    },
    getSum(param){
      const {columns,data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        sums[0] = '合计'
        sums[10] = this.totAL.toFixed(2)
        sums[9] = this.Ttime.toFixed(2)
      });
      return sums;
    },
    // 导入多选框
    handleSelectionChange_s(selection) {
      this.totAL = 0
      this.Ttime = 0
      this.selection = selection
      if(this.selection.length == 0){
        for (let item in this.chargeList_s){
          this.totAL += Number(this.chargeList_s[item].fAmt)
          this.Ttime += Number(this.chargeList_s[item].fAmtdr)
        }
      }else {
        for (let index in selection){
          this.totAL += Number(selection[index].fAmt)
          this.Ttime += Number(selection[index].fAmtdr)
        }
      }
      // this.getSummaries()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.totAL = 0
      this.ids = selection.map(item => item.fId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    imgChangeI(row) {
      if (row.fAmt && Number(row.fAmt) > Number(row.fAmtdr)) {
        this.$set(row, 'fAmt', row.fAmtdr)
        this.state_s = true
      }
      if (this.selection.length !== 0){
        this.totAL = 0
        this.Ttime = 0
        for (let item in this.selection){
          this.totAL += Number(this.selection[item].fAmt)
          this.Ttime += Number(this.selection[item].fAmtdr)
        }
      }else {
        this.totAL = 0
        this.Ttime = 0
        for (let item in this.chargeList_s){
          this.totAL += Number(this.chargeList_s[item].fAmt)
          this.Ttime += Number(this.chargeList_s[item].fAmtdr)
        }
      }
    },
    //确认导入
    confirmImport() {
      this.doNot = true
      this.hide = true
      for (let item in this.selection) {
        this.pass.fAmtcr += Number(this.selection[item].fAmt.toFixed(2))
        this.pass.fAmtdr += Number(this.selection[item].fAmtdr.toFixed(2))
      }
      if (this.state_s == true) {
        if (this.selection.length == '0') {
          this.$message.error('未选择导入行')
        } else {
          for (let item in this.selection) {
            this.empty.push(this.selection[item].fMblno)
            this.nothing.push(this.selection[item].fName)
          }
          //去重提单号
          this.empty = new Set(this.empty)
          this.empty = Array.from(this.empty)
          //去重货权方
          this.nothing = new Set(this.nothing)
          this.nothing = Array.from(this.nothing)

          if (this.empty.length <= 1) {
            this.pass.fMblno = this.empty[0]
          } else {
            this.pass.fMblno = this.empty[0] + '...'
          }
          if (this.nothing.length <= 1) {
            this.pass.fName = this.nothing[0]
          } else {
            this.pass.fName = this.nothing[0] + '...'
          }
          this.increase_s = this.increase_s.concat(this.selection)
          this.queryParams.tMblno = this.pass.fMblno //提单号
          this.queryParams.fCtrlcorpid = this.pass.fName
          this.queryParams.fCorpid = this.queryParameter.fToCorpid
          this.queryParams.fAmtcr = this.pass.fAmtcr
          this.queryParams.fAmtdr = this.pass.fAmtdr
          this.innerVisible = false
          this.chargeList_s = []
          this.queryParameter = {
            fCorpid: '',
            fToCorpid: '',
            fMblno: '',
            fStatementNo: '',
            fFeeid: '',
            timeExamine: '',
            timeInterval: ''
          }
        }
      } else if (this.state_s == false) {
        this.$message.error('本次金额不能大于原定金额')
      }
    },
    /** 查询财务数据主列表 */
    getList() {
      this.loading = true
      this.getDicts('approval_process').then((response) => {
        this.options = response.data
      })
      listCharge(this.tableFilter).then(response => {
        this.chargeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.approve = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        fId: null,
        fBillno: null,
        fCtrlcorpid: null,
        fCorpid: null,
        tMblno: null,
        fAmtdr: null,
        fAmtcr: null,
        fBilltype: null,
        fBillstatus: '0',
        fRemarks: null,
        fAccbilldate: null,
        delFlag: null,
        createBy: null,
        fDeptid: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 导入搜索 */
    importSearch() {
      // .join(',')
      this.chargeList_s = []
      if(this.queryParameter.fStatementNo){
        this.rules = {}
        search(this.queryParameter).then(response => {
          this.chargeList_s = response.rows
          this.$message.success('查询成功');
          for (let item in this.chargeList_s){
            this.totAL += Number(this.chargeList_s[item].fAmt)
            this.Ttime += Number(this.chargeList_s[item].fAmtdr)
          }
        })
      }else {
        this.rules = {
          fToCorpid: [
            { required: true, message: ' ', trigger: 'blur' }
          ],
          timeExamine: [
            { required: true, message: ' ', trigger: 'blur' }
          ]
        }
        if (this.rules){
          this.$refs['form'].validate(valid => {
            if (valid) {
              search(this.queryParameter).then(response => {
                this.chargeList_s = response.rows
                if(this.chargeList_s.length !== 0){
                  this.$message.success('查询成功');
                  this.totAL = 0
                  this.Ttime = 0
                  for (let item in this.chargeList_s){
                    this.totAL += Number(this.chargeList_s[item].fAmt)
                    this.Ttime += Number(this.chargeList_s[item].fAmtdr)
                  }
                }else{
                  this.$message.error("暂无数据")
                }
              })
            }
          })
        }else {
          this.$message.error('操作频繁');
        }
      }
    },
    /** 远程模糊查询用户 */
    corpsRemoteMethod(name) {
      if (name == null || name === '') {
        return false
      }
      let queryParams = { pageNum: 1, pageSize: 10, fName: name, type: 1 }
      listCorps(queryParams).then((response) => {
        this.fMblnoOptions = response.rows
        this.KHblnoOptions = response.rows
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.tableFilter = {
        pageNum: 1,
        pageSize: 10,
        timeInterval:null,
        fBillno:null,
        fCtrlcorpid:null,
        fCorpid:null
      }
      this.handleQuery()
    },
    //导入重置按钮
    resetQuery_s() {
      this.queryParameter = {
        fCorpid: '',
        fToCorpid: '',
        fMblno: '',
        fStatementNo: '',
        fFeeid: '',
        timeExamine: '',
        timeInterval: ''
      }
    },
    // 远程模糊查询费用名称
    fWRemoteMethod(name) {
      this.fWbuOptions = []
      if (name == null || name === '') {
        return false
      }
      let queryParams = { pageNum: 1, pageSize: 10, fName: name }
      listFees(queryParams).then((response) => {
        this.fWbuOptions = response.rows
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.notChange = false
      this.hide = true
      this.reset()
      this.pass = {
        fAmtdr: 0,    //应收合计
        fAmtcr: 0,    //应付合计
        fMblno: '',    //提单号
        fName: '',      //货权方
        fFeesName: '',   //结算单位
        fCorpid: ''     //结算单位ID
      },
        // this.queryParams = []
        this.increase_s = []
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        fBillno: null,
        fCtrlcorpid: null,
        fCorpid: null,
        tMblno: null,
        fAmtdr: null,
        fAmtcr: null,
        fBilltype: null,
        fBillstatus: null,
        fRemarks: null,
        fAccbilldate: null,
        fDeptid: null
      }
      this.resetForm('queryParams')
      this.open = true
      this.title = '添加财务数据主'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.approve = false
      this.hide = false
      this.notChange = false
      this.reset()
      this.pass = {
        fAmtdr: 0,    //应收合计
        fAmtcr: 0,    //应付合计
        fMblno: '',    //提单号
        fName: '',      //货权方
        fFeesName: '',   //结算单位
        fCorpid: ''     //结算单位ID
      }
      const fId = row.fId || this.ids
      getCharge(fId).then(response => {
        this.Operator = response.data.tFee.createBy
        this.increase_s = response.data.feeDoList
        this.fWbuOptions = response.data.feesList
        this.queryParams = response.data.tFee
        this.fWbuOptions = response.data.feesList
        this.fMblnoOptions = response.data.corps
        this.open = true
        this.title = '修改付费列表'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['ruless'].validate(valid => {
        if (valid) {
          if (this.queryParams.fId == null) {
            // this.chargeList.fBillstatus = '1'
            this.queryParams.fBillstatus = '1'
            let formData = new window.FormData()
            formData.append('tFee', JSON.stringify(this.queryParams))
            formData.append('tFeeDo', JSON.stringify(this.increase_s))
            addCharge(formData).then(response => {
              this.msgSuccess('新增成功')
              // this.increase_s = []
              // this.open = false
              this.getList()
            })
          } else {
            this.pass.fAmtcr = 0
            this.pass.fAmtdr = 0
            for (let item in this.increase_s) {
              this.pass.fAmtcr += Number(this.increase_s[item].fAmt)
              this.pass.fAmtdr += Number(this.increase_s[item].fAmtdr)
            }
            this.queryParams.fAmtcr = Number(this.pass.fAmtcr.toFixed(2))
            this.queryParams.fAmtdr = Number(this.pass.fAmtdr.toFixed(2))
            this.pass.fAmtcr.toFixed(2)

            this.queryParams.fBillstatus = '1'
            let formData = new window.FormData()
            formData.append('tFee', JSON.stringify(this.queryParams))
            formData.append('tFeeDo', JSON.stringify(this.increase_s))
            addCharge(formData).then(response => {
              this.msgSuccess('修改成功')
              // this.increase_s = []
              // this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fIds = row.fId || this.ids
      let tips = ''
      delCharge_s(fIds).then(data => {
        switch (data.msg) {
          case '0': {
            this.$message.error('当前数据已被其他操作员操作请刷新页面')
            break
          }
          case '1': {
            tips = '当前主表有数据从表无数据是否删除'
            this.delete_S(fIds, tips)
            break
          }
          case '2': {
            tips = '当前主表有数据从表有数据是否删除'
            this.delete_S(fIds, tips)
            break
          }
          default:{
            return this.$message.error('未知错误，无状态')
          }
        }
      })
    },
    delete_S(fIds, tips) {
      this.$confirm(tips, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delCharge(fIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有财务数据主数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportCharge(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    },
    //清空一行
    deleteRow(index, rows) {
      this.queryParams.fAmtcr = 0
      this.queryParams.fAmtdr = 0
      rows.splice(index, 1)
      for (let item in this.increase_s) {
        this.queryParams.fAmtcr += this.increase_s[item].fAmt
        this.queryParams.fAmtdr += this.increase_s[item].fAmtdr
      }
      if(this.increase_s == 0){
        this.doNot = false
      }else{
        this.doNot = true
      }
    }
  }
}
</script>
