<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-button type="success" @click="btnChargeClick">
            <svg-icon icon-class="charge" /> 收费
          </el-button>
        </div>
      </sticky>
      <el-form>
        <el-form-item>
          <el-select v-model="listQuery.searchRenwu.shebeizhongleidaima" placeholder="请选择设备种类" style="width: 165px" clearable>
            <el-option v-for="item in sbzlList" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
          <el-input v-model="listQuery.searchRenwu.zhucedaima" placeholder="注册代码" clearable style="width: 240px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.searchRenwu.serialNumber" placeholder="报检编号" clearable style="width: 240px" @keyup.enter.native="handleFilter" />
          <el-input v-model="listQuery.searchRenwu.shiyongdanwei" placeholder="使用单位" clearable style="width: 400px" @keyup.enter.native="handleFilter" />
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="info" icon="el-icon-close" @click="clearQuery">
            清空条件
          </el-button>
          <br>
          <span>收费状态：</span>
          <el-radio-group v-model="listQuery.searchRenwu.jfState" @change="handleFilter">
            <el-radio :label="null">
              全部
            </el-radio>
            <el-radio :label="0">
              待交费
            </el-radio>
            <el-radio :label="1">
              已交费
            </el-radio>
          </el-radio-group>&nbsp;&nbsp;
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="list"
      v-adaptive="{bottomOffset: 50}"
      height="0"
      :data="list"
      element-loading-text="拼命加载中"
      border
      fit
      highlight-current-row
      stripe
      width="100%"
      @row-click="onRowClick"
      @selection-change="handleSelectionChange"
      @sort-change="sortChange"
    >
      <el-table-column fixed="left" type="selection" width="40" />
      <el-table-column fixed="left" align="center" label="序号" width="60">
        <template slot-scope="scope">
          <span v-text="getIndex(scope.$index)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="报检单号" prop="serialNumber" width="140" sortable="custom" />
      <el-table-column align="center" label="使用单位" prop="shiyongdanwei" />
      <el-table-column align="center" label="制造单位" prop="zhizaodanwei" />
      <el-table-column align="center" label="产品(设备)名称" prop="shebeimingcheng" width="140" />
      <el-table-column align="center" label="产品(出厂)编号" prop="chanpinbianhao" width="140" />
      <el-table-column align="center" label="单位内编号" prop="danweineibubianhao" width="120" sortable="custom" />
      <el-table-column align="center" label="受理日期" prop="shouliriqi" width="120" />
      <el-table-column fixed="right" :formatter="formatter.moneyFormat" align="center" label="标准金额" prop="xitongheding" width="80" />
      <el-table-column fixed="right" :formatter="formatter.moneyFormat" align="center" label="核定金额" prop="datingheding" width="80" />
      <el-table-column fixed="right" :formatter="formatter.moneyFormat" align="center" label="检验员核定" prop="jianyanheding" width="100" />
      <el-table-column fixed="right" align="center" label="补交状态" prop="bjState" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.bjState===true" type="danger">
            待补交
          </el-tag>
          <el-tag v-else type="success">
            无补交
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="交费状态" prop="jfState" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.jfState===true" type="success">
            已交费
          </el-tag>
          <el-tag v-if="scope.row.jfState===false" type="danger">
            待交费
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalCount>0" :total="totalCount" :page-num.sync="listQuery.pageNum" :page-row.sync="listQuery.pageRow" @pagination="getList" />
    <el-dialog :visible.sync="chargeFormVisible" title="收费详细页" @close="closeDialog">
      <el-form ref="charge" :model="charge" class="el-form" label-position="right" label-width="100px">
        <el-row :gutter="18">
          <el-col :span="11">
            <el-form-item label="系统核定" prop="xitongheding">
              <el-input-number v-model="charge.xitongheding" :controls="false" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="大厅核定" prop="datingheding">
              <el-input-number v-model="charge.datingheding" :controls="false" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="18">
          <el-col :span="11">
            <el-form-item label="检验核定" prop="jianyanheding">
              <el-input-number v-model="charge.jianyanheding" :controls="false" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="已交金额" prop="yijiaojine">
              <el-input-number v-model="charge.yijiaojine" :controls="false" disabled style="width: 100%;" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="18">
          <el-tooltip class="item" effect="light" content="应补金额 = 检验核定 - 大厅核定（系统核定）" placement="top">
            <el-col :span="11">
              <el-form-item label="应补金额" prop="yingbujine">
                <el-input-number v-model="charge.yingbujine" :controls="false" disabled style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-tooltip>
          <el-tooltip class="item" effect="light" content="实交金额 = 初次交费：检验核定【大厅核定（系统核定）】；补交：应补金额" placement="top">
            <el-col :span="11">
              <el-form-item label="实交金额" prop="shijiaojine">
                <el-input-number v-model="charge.shijiaojine" :controls="false" :min="0" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-tooltip>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="11">
            <el-form-item label="收款人" prop="shoukuanren">
              <el-input v-model="charge.shoukuanren" type="text" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="收费日期" prop="shoufeiriqi">
              <el-date-picker v-model="charge.shoufeiriqi" type="date" placeholder="选择日期" style="width: 100%;" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="height: 130px">
          <el-col :span="22">
            <el-form-item label="备注" prop="jiaofeishuoming">
              <el-input v-model="charge.jiaofeishuoming" type="textarea" :rows="5" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="chargeFormVisible = false">
          取 消
        </el-button>
        <el-button type="primary" @click="saveCharge">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <invoice-create :fapiao-info.sync="fapiaoInfo" :fapiao-list-visible.sync="fapiaoListVisible" />
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'
import Utils from '@/utils/contact'
import InvoiceCreate from '@/views/invoice/create'

export default {
  name: 'ZzFinanceList',
  components: { Sticky, Pagination, InvoiceCreate },
  data() {
    return {
      totalCount: 0,
      chargeFormVisible: false,
      listQuery: {
        pageNum: 1, // 页码
        pageRow: 20, // 每页条数
        order: undefined, // 排序字段
        sort: undefined, // 排序方式
        searchRenwu: {
          jianyanleibie: 'ZJ',
          neibuleibie: 'ZZ',
          jfState: 0,
          bjState: null
        }
      },
      multipleSelection: [],
      list: [],
      sbzlList: [],
      charge: {
        shoukuanren: this.$store.getters.nickname,
        shoufeiriqi: new Date()
      },
      renwuIds: '',
      fapiaoListVisible: false,
      fapiaoInfo: {}
    }
  },
  created() {
    this.getList()
    this.getSbzlList()
    this.$nextTick(() => {
      this.$refs.list.doLayout()
    })
  },
  methods: {
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    getSbzlList() {
      this.api({
        url: '/sedirectory/getList',
        method: 'get',
        params: {
          sbzl: undefined,
          level: '1'
        }
      }).then(data => {
        this.sbzlList = data
      })
    },
    getList() {
      const paramList = this.listQuery
      if (paramList.searchRenwu.jfState === undefined) {
        paramList.searchRenwu.jfState = null
      }
      this.apibjd({
        url: '/renwu/getToChargeList',
        method: 'get',
        params: paramList
      }).then(data => {
        this.list = data.list
        this.totalCount = data.totalCount
        this.common.switchInspection(this.list)
      })
    },
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    clearQuery() {
      this.$set(this.listQuery, 'searchRenwu', {})
      this.getList()
    },
    sortChange(column) {
      this.listQuery.order = column.prop
      this.listQuery.sort = column.order.replace('ending', '')
      this.getList()
    },
    getIndex($index) {
      // 表格序号
      return (this.listQuery.pageNum - 1) * this.listQuery.pageRow + $index + 1
    },
    closeDialog() {
      this.chargeFormVisible = false
    },
    btnChargeClick() {
      let xitongheding = 0
      let jianyanheding = 0
      let datingheding = 0
      let yijiaojine = 0
      let yingbujine = 0
      let shijiaojine = 0
      if (this.multipleSelection === null || this.multipleSelection.length === 0) {
        this.$message.warning('请选择设备进行收费！')
        return false
      }
      const sydw = this.multipleSelection[0].shiyongdanwei
      for (let i = 0; i < this.multipleSelection.length; i++) {
        if (this.multipleSelection[i].shiyongdanwei !== sydw) {
          this.$message.warning('请选择相同使用单位的设备进行收费！')
          return false
        }
        if (this.multipleSelection[i].jfState && !this.multipleSelection[i].bjState) {
          this.$message.warning('请选择待收费或待补交的设备！')
          return false
        }
        xitongheding += this.multipleSelection[i].xitongheding
        datingheding += this.multipleSelection[i].datingheding
        jianyanheding += this.multipleSelection[i].jianyanheding
        yijiaojine += this.multipleSelection[i].shijiaojine
        if (this.multipleSelection[i].bjState) {
          // 应补金额 = 检验核定 - 实交金额
          const currentYbje = this.multipleSelection[i].jianyanheding - (this.multipleSelection[i].datingheding || this.multipleSelection[i].xitongheding)
          yingbujine += currentYbje
          shijiaojine += currentYbje
        } else {
          shijiaojine += (this.multipleSelection[i].jianyanheding || (this.multipleSelection[i].datingheding || this.multipleSelection[i].xitongheding))
        }
        if (i === 0) {
          this.renwuIds = this.multipleSelection[i].id
        } else {
          this.renwuIds += ',' + this.multipleSelection[i].id
        }
      }
      this.$set(this.charge, 'bjdId', this.multipleSelection[0].bjdId)
      this.$set(this.charge, 'serialNumber', this.multipleSelection[0].serialNumber)
      this.$set(this.charge, 'xitongheding', xitongheding)
      this.$set(this.charge, 'datingheding', datingheding)
      this.$set(this.charge, 'jianyanheding', jianyanheding)
      this.$set(this.charge, 'yijiaojine', yijiaojine)
      this.$set(this.charge, 'yingbujine', yingbujine)
      this.$set(this.charge, 'shijiaojine', shijiaojine)
      this.chargeFormVisible = true
    },
    saveCharge() {
      if (this.charge.shijiaojine === 0) {
        this.$message.warning('实交金额不能为“0”！')
        return false
      }
      this.apibjd({
        url: 'charge/saveCharge',
        method: 'post',
        data: {
          charge: this.charge,
          renwuIds: this.renwuIds
        }
      }).then(data => {
        this.$message({ message: '收费成功！', type: 'success' })
        this.chargeFormVisible = false
        this.$confirm('是否开具发票?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.apibjd({
            url: 'charge/getChargeFapiao',
            method: 'get',
            params: {
              chargeId: data
            }
          }).then(fapiao => {
            this.fapiaoInfo = fapiao
            this.fapiaoListVisible = true
          })
        }).catch(() => {
          Utils.$emit('to-be-invoiced')
          this.getList()
        })
      })
    }
  }
}
</script>

<style scoped>
</style>
