<template>
  <div class="app-container">
    <div class="filter-container">
      <sticky style="margin-bottom: 10px;">
        <div class="sub-navbar">
          <el-input v-model="listQuery.serialNumber" placeholder="报检单号" clearable style="width: 260px" @keyup.enter.native="handleFilter" />
          <el-button type="primary" icon="el-icon-search" @click="handleFilter">
            查询
          </el-button>
          <el-button type="success" @click="addBjdJKM">
            <svg-icon icon-class="charge" />添加缴费码
          </el-button>
        </div>
      </sticky>
    </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" type="index" />
      <el-table-column align="center" label="设备种类" prop="shebeizhonglei" width="190" sortable="custom" />
      <el-table-column align="center" label="报检单号" prop="serialNumber" width="140" sortable="custom" />
      <el-table-column align="center" label="使用单位" prop="shiyongdanwei" width="260" sortable="custom" />
      <el-table-column align="center" label="施工/维保单位" prop="shigongdanwei" width="260">
        <template slot-scope="scope">
          <span v-if="scope.row.shigongdanwei">
            {{ scope.row.shigongdanwei }}
          </span>
          <span v-else-if="scope.row.weibaodanwei">
            {{ scope.row.weibaodanwei }}
          </span>
          <span v-else>
            /
          </span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="制造单位" prop="zhizaodanwei" width="260" />
      <el-table-column align="center" label="受理日期" prop="shouliriqi" width="100" />
      <el-table-column :formatter="formatter.moneyFormat" align="center" label="检验员核定（总）" prop="sumJine" width="140" />
    </el-table>
    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-row.sync="listQuery.pageSize" @pagination="getList" />
    <el-dialog :visible.sync="chargeFormVisible" title="添加缴费码详情" @close="closeDialog">
      <el-form ref="bjd" :model="bjd" class="el-form" label-position="right" label-width="100px">
        <el-row :gutter="18">
          <el-col :span="18">
            <el-form-item label="使用单位" prop="shiyongdanwei">
              {{ bjd.shiyongdanwei }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="18">
          <el-col :span="18">
            <el-form-item label="缴款单位全称" prop="jiaokuandanwei">
              {{ bjd.jiaokuandanwei }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="18">
          <el-col :span="11">
            <el-form-item label="缴款联系人" prop="jiaokuanren">
              {{ bjd.jiaokuanren }}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="缴款联系电话" prop="jiaokuandianhua">
              {{ bjd.jiaokuandianhua }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="18">
          <el-col :span="11">
            <el-form-item label="缴款邮箱" prop="jiaokuanyouxiang">
              {{ bjd.jiaokuanyouxiang }}
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="核定金额(总)" prop="sumJine">
              {{ bjd.sumJine }}元
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="缴款码" :rules="[{ required: true, message: '请输入缴款码', trigger: 'blur' },{ min: 20, max:20, message: '请输入20位缴款码', trigger: ['blur', 'change'] }]" prop="jiaokuanma">
              <el-input v-model="bjd.jiaokuanma" type="number" />
            </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="saveBjd">
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import Sticky from '@/components/Sticky'
import Pagination from '@/components/Pagination'

export default {
  name: 'YiChangShouFeiUnitList', // 未核价单位
  components: { Sticky, Pagination },
  data() {
    return {
      total: 0,
      chargeFormVisible: false,
      listQuery: {
        pageNum: 1, // 页码
        pageSize: 20, // 每页条数
        order: undefined, // 排序字段
        sort: undefined // 排序方式
      },
      multipleSelection: [],
      list: [],
      bjd: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      // 查询事件
      this.listQuery.pageNum = 1
      this.getList()
    },
    getList() {
      this.apibjd({
        url: '/charge/listDsf',
        method: 'get',
        params: this.listQuery
      }).then(data => {
        this.list = data.list
        this.total = data.total
        this.common.switchInspection(this.list)
      })
    },
    onRowClick(row) {
      this.$refs.list.toggleRowSelection(row)
    },
    handleSelectionChange: function(val) {
      this.multipleSelection = val
    },
    sortChange(column) {
      this.listQuery.order = column.prop
      this.listQuery.sort = column.order.replace('ending', '')
      this.getList()
    },
    closeDialog() {
      this.chargeFormVisible = false
    },
    addBjdJKM() {
      if (this.multipleSelection.length !== 1) {
        this.$message.warning('请选择一家单位进行添加。')
        return false
      }
      this.bjd = this.multipleSelection[0]
      this.chargeFormVisible = true
    },
    saveBjd() {
      this.$refs['bjd'].validate((valid) => {
        if (valid) {
          if (this.bjd.id === '' || this.bjd.id === null) {
            this.$message.warning('无法获取该单位信息，请刷新后重新操作！')
            return false
          }
          this.apibjd({
            url: 'charge/addJkm',
            method: 'post',
            data: {
              id: this.bjd.id,
              jiaokuanma: this.bjd.jiaokuanma
            }
          }).then(data => {
            this.$message({ message: '添加缴款码成功！', type: 'success' })
            this.chargeFormVisible = false
            this.getList()
          })
        }
      })
    }
  }
}
</script>
