<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="支持模糊搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="支持模糊搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="全部" clearable style="width: 140px">
          <el-option v-for="item in orderTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ai:groupChargeOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="groupChargeOrderList">
      <el-table-column label="订单号" align="center" prop="orderNum" width="200" show-overflow-tooltip />
      <el-table-column label="部门名称" align="center" prop="deptName" width="150" show-overflow-tooltip />
      <el-table-column label="类型" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ orderTypeLabel(scope.row.orderType) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="金额（元）" align="center" prop="money" width="120" />
      <el-table-column label="积分" align="center" prop="amount" width="120" />
      <el-table-column label="状态" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ statusLabel(scope.row.status) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </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="560px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="团队ID" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入团队ID" />
        </el-form-item>
        <el-form-item label="类型" prop="orderType">
          <el-select v-model="form.orderType" placeholder="请选择" style="width: 100%">
            <el-option v-for="item in orderTypeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="金额(元)" prop="money">
          <el-input v-model="form.money" placeholder="可选，对账用金额" />
        </el-form-item>
        <el-form-item label="积分" prop="amount">
          <el-input v-model="form.amount" placeholder="充值/退款填正数；手动修改可填负数表示扣减" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择" style="width: 100%">
            <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="orderNum" v-if="form.id != null">
          <el-input v-model="form.orderNum" disabled />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="对账说明、打款信息等" maxlength="500" show-word-limit />
        </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 {
  listGroupChargeOrder,
  getGroupChargeOrder,
  delGroupChargeOrder,
  addGroupChargeOrder,
  updateGroupChargeOrder
} from "@/api/ai/groupChargeOrder"

export default {
  name: "GroupChargeOrder",
  data() {
    return {
      loading: true,
      showSearch: true,
      dateRange: [],
      total: 0,
      groupChargeOrderList: [],
      title: "",
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNum: null,
        deptName: null,
        orderType: null
      },
      form: {},
      rules: {
        deptId: [{ required: true, message: "请输入团队ID", trigger: "blur" }],
        orderType: [{ required: true, message: "请选择类型", trigger: "change" }],
        amount: [{ required: true, message: "请填写积分", trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }]
      },
      orderTypeOptions: [
        { label: "充值", value: 0 },
        { label: "退款", value: 1 },
        { label: "手动修改", value: 2 }
      ],
      statusOptions: [
        { label: "进行中", value: 0 },
        { label: "已完成", value: 1 },
        { label: "失败", value: 2 }
      ]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    orderTypeLabel(orderType) {
      const n = orderType !== undefined && orderType !== null ? Number(orderType) : null
      const hit = this.orderTypeOptions.find(o => o.value === n)
      return hit ? hit.label : "—"
    },
    statusLabel(status) {
      const n = status !== undefined && status !== null ? Number(status) : null
      const hit = this.statusOptions.find(o => o.value === n)
      return hit ? hit.label : "—"
    },
    getList() {
      this.loading = true
      listGroupChargeOrder(this.addDateRange(this.queryParams, this.dateRange, "CreateTime")).then(response => {
        this.groupChargeOrderList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        deptId: undefined,
        orderType: 0,
        money: undefined,
        amount: undefined,
        remark: undefined,
        status: 1,
        orderNum: undefined
      }
      this.resetForm("form")
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "新增团队充值退款订单"
    },
    handleUpdate(row) {
      this.reset()
      const id = row && row.id
      if (!id) {
        return
      }
      getGroupChargeOrder(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改团队充值退款订单"
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return
        }
        const payload = { ...this.form }
        if (payload.id == null) {
          addGroupChargeOrder(payload).then(() => {
            this.$modal.msgSuccess("新增成功")
            this.open = false
            this.getList()
          })
        } else {
          updateGroupChargeOrder(payload).then(() => {
            this.$modal.msgSuccess("修改成功")
            this.open = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      const id = row && row.id
      if (!id) {
        return
      }
      this.$modal.confirm("是否确认删除所选团队充值退款订单？").then(() => {
        return delGroupChargeOrder(id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    handleExport() {
      this.download("ai/group/chargeorder/export", {
        ...this.queryParams
      }, `group_charge_order_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
