<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜 索
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="PAYMENTTIME" label="支付时间" />
      <el-table-column prop="MONEY" label="金额" />
      <el-table-column prop="BILLINGSTATUS" label="开票状态">
        <template slot-scope="{row}">
          <div v-if="row.BILLINGSTATUS==0">待开票</div>
          <div v-else-if="row.BILLINGSTATUS==1">开票中</div>
          <div v-else-if="row.BILLINGSTATUS==2">已开票</div>
          <div v-else/>
        </template>
      </el-table-column>
      <el-table-column prop="ENTERPRISENAME" label="开票企业" />
      <el-table-column prop="UCODE" label="统一社会信用代码" />
      <el-table-column label="操作" align="center" width="250">
        <template slot-scope="{row}">
          <el-button v-if="row.BILLINGSTATUS==1" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.BILLINGMANAGE_ID)">开票</el-button>
          <el-button v-if="row.BILLINGSTATUS==2" type="success" icon="el-icon-view" size="mini" @click="goDetail(row)">查看开票</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" title="开票信息" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="附件">
          <el-upload
            ref="upload"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
            :on-change="handleEditChange"
            :before-upload="beforeFileUpload"
            :auto-upload="false"
            :limit="1"
            :class="{hide:hideUpload}"
            action="#"
            accept=".jpg,.jpeg,.png"
            list-type="picture-card">
            <i class="el-icon-plus" />
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img :src="dialogImageUrl" width="100%" alt="">
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormView" title="查看" width="800px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <img :src="config.fileUrl + form.FILEPATH" alt="" width="600px">
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
import { upload } from '@/utils/upload'
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      config: config,
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        BILLINGMANAGE_ID: '', // 备注1
        PAYMENTTIME: '', // 支付时间
        MONEY: '', // 金额
        BILLINGSTATUS: '', // 开票状态
        INVOICEMANAGER_ID: '', // 发票ID
        ISDELETE: '', // 备注6
        CORPINFO_ID: '', // 备注7
        OPDATE: '', // 备注8
        OPUSER: '', // 备注9
        FPURL: ''// 备注10
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {},
      FFILE: {},
      dialogFormView: false
    }
  },
  created() {
    this.getList(this.ROLE_ID)
  },
  methods: {
    getRowKey(row) {
      return row.BILLINGMANAGE_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/billingmanage/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      this.form.BILLINGMANAGE_ID = ID
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      requestFN(
        '/billingmanage/editkp',
        {
          BILLINGMANAGE_ID: this.form.BILLINGMANAGE_ID,
          tm: new Date().getTime()
        }
      ).then((data) => {
        if (data.result == 'success') {
          const formData = new FormData()
          formData.append('FOREIGN_KEY', this.form.BILLINGMANAGE_ID)
          formData.append('TYPE', '12')
          formData.append('FFILE', this.FFILE)
          upload(
            '/imgfiles/add',
            formData
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
          })
        }
      }).catch((e) => {
        this.listLoading = false
      })
    },
    beforeFileUpload(file) {
      const types = ['image/jpeg', 'image/jpg', 'image/png']
      const isImage = types.includes(file.type)
      if (!isImage) {
        this.$message.error('上传图片只能是 JPG、JPEG、PNG 格式!')
      } else {
        this.FFILE = file
      }
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/billingmanage/delete',
          {
            BILLINGMANAGE_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },

    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.BILLINGMANAGE_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/billingmanage/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'billingmanage:add,billingmanage:del,billingmanage:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.billingmanagefhadminadd		// 新增权限
        this.del = data.billingmanagefhadmindel		// 删除权限
        this.edit = data.billingmanagefhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict: function() {
    },
    resetForm() {
      this.form = {
        BILLINGMANAGE_ID: '', // 备注1
        PAYMENTTIME: '', // 支付时间
        MONEY: '', // 金额
        BILLINGSTATUS: '', // 开票状态
        INVOICEMANAGER_ID: '', // 发票ID
        ISDELETE: '', // 备注6
        CORPINFO_ID: '', // 备注7
        OPDATE: '', // 备注8
        OPUSER: '', // 备注9
        FPURL: ''// 备注10
      }
    },
    goDetail(row) {
      this.dialogFormView = true
      this.form.FILEPATH = row.FILEPATH
    }
  }
}

</script>
