<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支付状态" prop="payStatus">
        <el-select v-model="queryParams.payStatus" placeholder="请选择支付状态" clearable size="small">
          <el-option label="待支付" value="0" />
          <el-option label="已支付" value="1" />
          <el-option label="已取消" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="资源状态" prop="resourceStatus">
        <el-select v-model="queryParams.resourceStatus" placeholder="请选择资源状态" clearable size="small">
          <el-option label="未发放" value="0" />
          <el-option label="已发放" value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['generator:sourcePurchase:edit']">修改状态</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['generator:sourcePurchase:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sourcePurchaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单号" align="center" prop="orderNo" min-width="190" />
      <el-table-column label="项目名称" align="center" prop="projectName" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="用户ID" align="center" prop="userId" width="90" />
      <el-table-column label="金额" align="center" prop="amount" width="100">
        <template slot-scope="scope">{{ formatPrice(scope.row.amount) }}</template>
      </el-table-column>
      <el-table-column label="支付状态" align="center" prop="payStatus" width="100">
        <template slot-scope="scope">
          <el-tag :type="payStatusType(scope.row.payStatus)">{{ payStatusLabel(scope.row.payStatus) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="资源状态" align="center" prop="resourceStatus" width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.resourceStatus === '1' ? 'success' : 'info'">{{ scope.row.resourceStatus === '1' ? '已发放' : '未发放' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="购买时间" align="center" prop="purchaseTime" width="160" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['generator:sourcePurchase:edit']">修改</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <el-dialog :title="title" :visible.sync="open" width="460px" append-to-body>
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="订单号">
          <el-input v-model="form.orderNo" disabled />
        </el-form-item>
        <el-form-item label="支付状态">
          <el-radio-group v-model="form.payStatus">
            <el-radio label="0">待支付</el-radio>
            <el-radio label="1">已支付</el-radio>
            <el-radio label="2">已取消</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="资源状态">
          <el-radio-group v-model="form.resourceStatus">
            <el-radio label="0">未发放</el-radio>
            <el-radio label="1">已发放</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getSourcePurchase, listSourcePurchase, updateSourcePurchase } from '@/api/generator/sourcePurchase'

export default {
  name: 'SourcePurchase',
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      showSearch: true,
      total: 0,
      sourcePurchaseList: [],
      open: false,
      title: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: null,
        projectName: null,
        payStatus: null,
        resourceStatus: null
      },
      form: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listSourcePurchase(this.queryParams).then(response => {
        this.sourcePurchaseList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        purchaseId: null,
        orderNo: '',
        payStatus: '0',
        resourceStatus: '0',
        remark: ''
      }
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseId)
      this.single = selection.length !== 1
    },
    handleUpdate(row) {
      this.reset()
      const purchaseId = row.purchaseId || this.ids
      getSourcePurchase(purchaseId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改购买记录'
      })
    },
    submitForm() {
      updateSourcePurchase(this.form).then(() => {
        this.$modal.msgSuccess('修改成功')
        this.open = false
        this.getList()
      })
    },
    handleExport() {
      this.download('generator/sourcePurchase/export', {
        ...this.queryParams
      }, `source_purchase_${new Date().getTime()}.xlsx`)
    },
    formatPrice(value) {
      const amount = Number(value || 0) / 100
      return `¥${Number.isInteger(amount) ? amount : amount.toFixed(2)}`
    },
    payStatusLabel(value) {
      return { '0': '待支付', '1': '已支付', '2': '已取消' }[value] || value
    },
    payStatusType(value) {
      return { '0': 'warning', '1': 'success', '2': 'info' }[value] || 'info'
    }
  }
}
</script>
