<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item :label="$t('billing.用户id')" prop="userId">
        <el-input
          v-model="queryParams.userId"
          :placeholder="$t('billing.请输入用户id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('billing.类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('billing.请选择类型')" clearable>
          <el-option
            v-for="dict in dict.type.sys_billing_type"
            :key="dict.value"
            :label="generateTitle(dict.label)"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('billing.订单id')" prop="ddId">
        <el-input
          v-model="queryParams.ddId"
          :placeholder="$t('billing.请输入订单id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('billing.状态')" prop="state">
        <el-select v-model="queryParams.state" :placeholder="$t('billing.请选择状态')" clearable>
          <el-option
            v-for="dict in dict.type.sys_billing_state"
            :key="dict.value"
            :label="generateTitle(dict.label)"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('billing.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('billing.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
     <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['Billing:billing:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['Billing:billing:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['Billing:billing:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['Billing:billing:export']"
        >{{$t('billing.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billingList" @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="$t('billing.用户id')" align="center" prop="userId" />
      <el-table-column :label="$t('billing.类型')" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==0" type="success" effect="dark">{{generateTitle('分成')}}</el-tag>
          <el-tag v-if="scope.row.type==1" type="light" effect="dark">{{generateTitle('充值')}}</el-tag>
          <el-tag v-if="scope.row.type==2" type="danger" effect="dark">{{generateTitle('提现')}}</el-tag>
          <el-tag v-if="scope.row.type==3" type="info" effect="dark">{{generateTitle('消费')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('billing.订单id')" align="center" prop="ddId" />
      <el-table-column :label="$t('billing.金额')" align="center" prop="amount" />
      <el-table-column :label="$t('billing.状态')" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state==0" type="primary">{{generateTitle('完成')}}</el-tag>
          <el-tag v-if="scope.row.state==1" type="warning">{{generateTitle('退回')}}</el-tag>
          <el-tag v-if="scope.row.state==2" type="info">{{generateTitle('作废')}}</el-tag>
          <el-tag v-if="scope.row.state==3" type="danger">{{generateTitle('未支付')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('billing.说明')" align="center" prop="illustrate" />
      <el-table-column :label="$t('billing.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['Billing:billing:edit']"
          >{{$t('billing.详情')}}</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['Billing:billing:remove']"
          >删除</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"
    />

    <!-- 添加或修改Billing对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="$t('message.language')=='语言'?'80px':$t('message.language')=='language'?'165px':'200px'">
        <el-form-item :label="$t('billing.用户id')" prop="userId">
          <el-input v-model="form.userId" :placeholder="$t('billing.请输入用户id')" />
        </el-form-item>
        <el-form-item :label="$t('billing.类型')" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio
              v-for="dict in dict.type.sys_billing_type"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('billing.订单id')" prop="ddId">
          <el-input v-model="form.ddId" :placeholder="$t('billing.请输入订单id')" />
        </el-form-item>
        <el-form-item :label="$t('billing.金额')" prop="amount">
          <el-input v-model="form.amount" :placeholder="$t('billing.请输入金额')" />
        </el-form-item>
        <el-form-item :label="$t('billing.状态')" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio
              v-for="dict in dict.type.sys_billing_state"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('billing.说明')" prop="illustrate">
          <el-input v-model="form.illustrate" type="textarea" :placeholder="$t('billing.请输入内容')" />
        </el-form-item>
        <el-form-item :label="$t('billing.支付或收款方式')" prop="paymentMethod">
          <el-input v-model="form.paymentMethod" :placeholder="$t('billing.请输入支付或收款方式')" />
        </el-form-item>
        <el-form-item :label="$t('billing.收支账号')" prop="accountNumber">
          <el-input v-model="form.accountNumber" :placeholder="$t('billing.请输入收支账号')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">{{$t('billing.确定')}}</el-button>
        <el-button @click="cancel">{{$t('billing.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBilling, getBilling, delBilling, addBilling, updateBilling } from "@/api/Billing/billing";
import {generateTitle} from '../../../utils/i18.js'
export default {
  name: "Billing",
  dicts: ['sys_billing_type', 'sys_billing_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // Billing表格数据
      billingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        type: null,
        ddId: null,
        state: null,
        paymentMethod: null,
        accountNumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询Billing列表 */
    getList() {
      this.loading = true;
      listBilling(this.queryParams).then(response => {
        this.billingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        type: null,
        ddId: null,
        amount: null,
        state: null,
        illustrate: null,
        paymentMethod: null,
        accountNumber: 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 = "添加Billing";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBilling(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('billing.查看账单');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBilling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBilling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除Billing编号为"' + ids + '"的数据项？').then(function() {
        return delBilling(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('Billing/billing/export', {
        ...this.queryParams
      }, `billing_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
