<template>
  <div class="app-container">

    <div class="filter-container">
      <div class="filter-lable">
        用户名称：
      </div>
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
      <div class="filter-group mr-10">
        <div class="filter-lable" style="width:72px">
          交付时间：
        </div>
        <div class="filter-date">
          <el-date-picker
            :clearable="false"
            v-model="YEAR"
            value-format="yyyy"
            type="year"
            placeholder="选择年"
            style="width: 100%;" />
        </div>
      </div>
      <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="USERNAME" label="用户名称" />
      <el-table-column prop="l1_cnum" label="一级开户数" />
      <el-table-column prop="l2_cnum" label="二级开户数" />
      <el-table-column prop="l3_cnum" label="三级开户数" />
      <el-table-column prop="l1_jnum" label="一级交付数" />
      <el-table-column prop="l2_jnum" label="二级交付数" />
      <el-table-column prop="l3_jnum" label="三级交付数" />
      <el-table-column prop="l1_dnum" label="一级到期数" />
      <el-table-column prop="l2_dnum" label="二级到期数" />
      <el-table-column prop="l3_dnum" label="三级到期数" />
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

  </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: '',
      YEAR: '2022',
      varList: [],
      pd: [],
      dates: ['', ''], // 区间条件
      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(
        '/statistics/selectlist?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          YEAR: this.YEAR
        }
      ).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>
