<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="充值状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="充值状态" clearable>
          <el-option v-for="item in orderStatus_option" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="微信单号" prop="outTradeNo">
        <el-input v-model="queryParams.outTradeNo" placeholder="请输入微信订单号" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['fulu:info:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="微信订单号" align="center" prop="outTradeNo" />
      <el-table-column label="天猫订单号" align="center" prop="orderDataJson.tmallNo" />
      <el-table-column label="手机号" align="center" prop="orderDataJson.rechargePhoneNo" />
      <el-table-column label="运营商" align="center" prop="orderDataJson.operators" :formatter="operators_format" />
      <el-table-column label="状态操作" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <el-button v-if="scope.row.orderStatus == '1'" type="primary" icon="el-icon-check" @click="IsSuccessful(scope.row)">提交充值</el-button>
          <el-button v-if="scope.row.orderStatus == '2'" type="success" disabled>充值成功</el-button>
          <el-button v-if="scope.row.orderStatus == '3'" type="danger" disabled>已退款</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" />
  </div>
</template>

<script>
import { listInfo, activities, exportInfo } from '@/api/phoneBill/info'

export default {
  name: 'Info',
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 福禄商品表格数据
      infoList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        iconId: null,
        name: null,
        introduction: null,
        productIds: null,
        fuluGoodsType: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        productIds: [{ required: true, message: '分类商品ids不能为空', trigger: 'blur' }],
        status: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      },
      operators_option: null,
      operators_format: null,
      orderStatus_option:null
    }
  },
  created() {
    this.getList()
    this.orderStatus_option = [
      { dictValue: 1, dictLabel: '未充值' },
      { dictValue: 2, dictLabel: '充值成功' },
      { dictValue: 3, dictLabel: '已退款' }
    ]
    this.operators_option = [
      { dictValue: 1, dictLabel: '移动' },
      { dictValue: 2, dictLabel: '联通' },
      { dictValue: 3, dictLabel: '电信' }
    ]
    this.operators_format = (row, column) => {
      return this.selectDictLabel(this.operators_option, row.orderDataJson.operators)
    }
  },
  methods: {
    //确认充值成功
    IsSuccessful(row) {
      this.$confirm('此操作将修改状态为充值成功, 很难改回, 是否继续?', '很重要的提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          activities(row.outTradeNo).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '状态修改成功!'
              })
              this.getList()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    //图标上传成功返回函数
    handleAvatarSuccess(response) {
      this.form.iconId = response.data.fileId
      this.form.iconUrl = response.data.fileUrl
    },
    /** 查询福禄商品列表 */
    getList() {
      this.loading = true
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        iconId: null,
        name: null,
        introduction: null,
        remark: null,
        productIds: null,
        fuluGoodsType: null,
        status: '0',
        createTime: null,
        updateTime: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加福禄商品'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改福禄商品'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除福禄商品编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delInfo(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有福禄商品数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportInfo(queryParams)
        })
        .then(response => {
          this.download(response.data)
        })
    }
  }
}
</script>
