<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item :label="$t('marginlist.用户id')" prop="userId">
        <el-input
          v-model="queryParams.userId"
          :placeholder="$t('marginlist.请输入用户id')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     <!-- <el-form-item label="保证金类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择保证金类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_pos_margin"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item :label="$t('marginlist.支付时间')" prop="cretim">
        <el-date-picker clearable
          v-model="queryParams.cretim"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('marginlist.请选择时间')">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('marginlist.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('marginlist.重置')}}</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="['system:marginlist:export']"
        >{{$t('marginlist.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="marginlistList" @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('marginlist.用户id')" align="center" prop="userId" />
      <el-table-column :label="$t('marginlist.用户类型')" align="center" prop="type">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.type==1" type="info" effect="dark">{{generateTitle('商户')}}</el-tag>
          <el-tag v-if="scope.row.type==2" type="primary" effect="dark">{{generateTitle('骑手')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('marginlist.保证金额')" align="center" prop="amount" />
      <el-table-column :label="$t('marginlist.支付时间')" align="center" prop="cretim"/>
      <el-table-column :label="$t('marginlist.操作')" 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="['system:marginlist:edit']"
          >{{$t('marginlist.查看')}}</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"
    />

    <!-- 添加或修改usermargin对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.用户账号')}}</div>
        <div>{{user.userName}}</div>
      </div>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.用户昵称')}}</div>
        <div>{{user.nickName}}</div>
      </div>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.用户手机')}}</div>
        <div>{{user.phone}}</div>
      </div>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.用户类型')}}</div>
        <div>{{user.userType==1?generateTitle('商户'):user.userType==2?generateTitle('骑手'):generateTitle('用户')}}</div>
      </div>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.保证金')}}</div>
         <div>{{form.amount}}</div>
      </div>
      <div class="hengxl">
        <div class="neir">{{$t('marginlist.支付时间')}}</div>
         <div>{{form.cretim}}</div>
      </div>
     <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel">{{$t('marginlist.确定')}}</el-button>
        <el-button @click="cancel">{{$t('marginlist.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMarginlist, getMarginlist, delMarginlist, addMarginlist, updateMarginlist } from "@/api/system/marginlist";
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/infouser/user";
import {generateTitle} from '../../../utils/i18.js'
export default {
  name: "Marginlist",
  dicts: ['sys_pos_margin'],
  data() {
    return {
      user:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // usermargin表格数据
      marginlistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        type: null,
        amount: null,
        cretim: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    generateTitle,
    /** 查询usermargin列表 */
    getList() {
      this.loading = true;
      listMarginlist(this.queryParams).then(response => {
        this.marginlistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        type: null,
        amount: null,
        cretim: 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 = "添加usermargin";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMarginlist(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('marginlist.查看保证金');
        getUser(response.data.userId).then(response => {
          console.log('用户详情',response.data)
          this.user = response.data;
        });
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMarginlist(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMarginlist(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除usermargin编号为"' + ids + '"的数据项？').then(function() {
        return delMarginlist(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/marginlist/export', {
        ...this.queryParams
      }, `marginlist_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
  .hengxl{
    display: flex;
    align-items: center;
    flex-direction: row;
    margin-bottom: 20px;
  }
  .neir{
    width: 70px;
    text-align: right;
    font-weight: bold;
    margin-right: 10px;
  }
</style>
