<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('business.supplementaryInfo.userAccount.accountCode')" prop="accountCode">
        <el-input v-model="queryParams.accountCode" :placeholder="$t('business.supplementaryInfo.userAccount.inputAccountCode')"
                  clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('business.supplementaryInfo.userAccount.accountType')" prop="accountType">
        <el-select v-model="queryParams.accountType" size="medium" :placeholder="$t('business.supplementaryInfo.userAccount.selectAccountType')">
          <el-option
            v-for="item in options_accountType"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('business.supplementaryInfo.userAccount.accountStatus')" prop="status">
        <el-select v-model="queryParams.status" size="medium" :placeholder="$t('business.supplementaryInfo.userAccount.selectAccountStatus')">
          <el-option
            v-for="item in options_accountStatus"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('common.button.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.button.reset') }}</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="['business:userAccount:edit']">{{ $t('common.button.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="['business:userAccount:export']">{{ $t('common.button.export') }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userAccountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.userName')" align="center">
        <template #default="scope">
          <span v-if="scope.row.userType == 0">{{ scope.row.userInfoName }}</span>
          <span v-if="scope.row.userType == 1">{{ scope.row.merchantName }}</span>
          <span v-if="scope.row.userType == 2">{{ scope.row.agentName }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.accountCode')" align="center" prop="accountCode"/>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.accountName')" align="center" prop="accountName"/>
      <el-table-column
        :label="$t('business.supplementaryInfo.userAccount.accountAmount')"
        prop="accountAmount"
        :formatter="formatCurrency">
      </el-table-column>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.accountType')" align="center" prop="accountType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.accountType == 0"> {{$t('business.supplementaryInfo.userAccount.accountTypeNormal')}} </el-tag>
          <el-tag type="warning" v-if="scope.row.accountType == 1">{{$t('business.supplementaryInfo.userAccount.accountTypeEnterprise')}}</el-tag>
          <el-tag type="success" v-if="scope.row.accountType == 2">{{$t('business.supplementaryInfo.userAccount.accountTypeAgent')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.accountLevel')" align="center" prop="isPrimary">
        <template slot-scope="scope">
          <el-tag type="warning" v-if="scope.row.isPrimary == 0">{{$t('business.supplementaryInfo.userAccount.accountLevelPrimary')}}</el-tag>
          <el-tag v-if="scope.row.isPrimary == 1">{{$t('business.supplementaryInfo.userAccount.accountLevelSub')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('business.supplementaryInfo.userAccount.status')" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status == 0">{{ $t('business.supplementaryInfo.userAccount.statusNormal') }}</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 1">{{ $t('business.supplementaryInfo.userAccount.statusFrozen') }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.tableMsg.operation')" align="center" width="200"
                       class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
                     v-if="(scope.row.id == null)"
                     v-hasPermi="['business:userAccount:edit']">{{ $t('common.button.add') }}
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-if="(scope.row.id != null)"
                     v-hasPermi="['business:userAccount:edit']">{{ $t('common.button.edit') }}
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-lock" @click="accountLock(scope.row)"
                     v-if="(scope.row.id != null && scope.row.status == 0)"
                     v-hasPermi="['business:userAccount:remove']">{{ $t('business.supplementaryInfo.userAccount.statusFrozen') }}
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-lock" @click="accountUnlock(scope.row)"
                     v-if="(scope.row.id != null && scope.row.status == 1)"
                     v-hasPermi="['business:userAccount:remove']">{{ $t('business.supplementaryInfo.userAccount.statusUnFrozen') }}
          </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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('business.supplementaryInfo.userAccount.detailAccountName')" prop="accountName">
          <el-input v-model="form.accountName" :placeholder="$t('business.supplementaryInfo.userAccount.inputAccountName')"/>
        </el-form-item>
        <el-form-item :label="$t('business.supplementaryInfo.userAccount.accountLevel')" prop="isPrimary">
          <el-select v-model="form.isPrimary" :placeholder="$t('business.supplementaryInfo.userAccount.selectAccountLevel')">
            <el-option
              v-for="item in options_accountLevel"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">{{
            $t('common.button.confirm')
          }}
        </el-button>
        <el-button @click="cancel">{{ $t('common.button.cancel') }}</el-button>
      </div>
    </el-dialog>

    <!-- 用户账户详情显示框 -->
    <el-dialog :title="title" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="auto">
        <el-form-item :label="$t('business.supplementaryInfo.userAccount.detailAccountCodeLabel')">
          {{ form.accountCode }}
        </el-form-item>
        <el-form-item :label="$t('business.supplementaryInfo.userAccount.detailAccountNameLabel')">
          {{ form.accountName }}
        </el-form-item>
        <el-form-item :label="$t('business.supplementaryInfo.userAccount.detailAccountAmountLabel')">
          {{ form.accountAmount }}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('common.button.close') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listUserAccount,
  getUserAccount,
  addUserAccount,
  updateUserAccount,
  lockAccount
} from "@/api/business/userAccount";

export default {
  name: "UserAccount",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户账户表格数据
      userAccountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 状态时间范围
      daterangeCreateTime: [],
      // 状态时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        accountId: undefined,
        accountCode: undefined,
        accountName: undefined,
        accountAmount: undefined,
        accountType: undefined,
        status: undefined,
        createTime: undefined,
        updateTime: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      options_accountLevel: [{
        value: "0",
        label: this.$t('business.supplementaryInfo.userAccount.accountLevelPrimary')
      }, {
        value: "1",
        label: this.$t('business.supplementaryInfo.userAccount.accountLevelSub')
      }],
      options_accountType: [{
        value: 0,
        label: this.$t('business.supplementaryInfo.userAccount.accountTypeNormal')
      }, {
        value: 1,
        label: this.$t('business.supplementaryInfo.userAccount.accountTypeEnterprise')
      }],
      options_accountStatus: [{
        value: 0,
        label: this.$t('business.supplementaryInfo.userAccount.statusNormal')
      }, {
        value: 1,
        label: this.$t('business.supplementaryInfo.userAccount.statusFrozen')
      }],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户账户列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeUpdateTime && '' != this.daterangeUpdateTime) {
        this.queryParams.params["beginUpdateTime"] = this.daterangeUpdateTime[0];
        this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      }
      listUserAccount(this.queryParams).then(response => {
        this.userAccountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.detailOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        accountId: undefined,
        accountCode: undefined,
        accountName: undefined,
        accountAmount: undefined,
        status: undefined,
        delFlag: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = this.$t('business.supplementaryInfo.userAccount.addTitle');
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset();
      this.form = row;
      this.detailOpen = true;
      this.title = this.$t('business.supplementaryInfo.userAccount.detailTitle');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUserAccount(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = this.$t('business.supplementaryInfo.userAccount.editTitle');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUserAccount(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.button.updateSuccess'));
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUserAccount(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.button.addSuccess'));
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 冻结按钮操作 */
    accountLock(row) {
      this.$modal.confirm('是否要对账户”' + row.accountName + '“进行冻结？').then(() => {
        this.loading = true;
        row.status = 1;
        return lockAccount(row);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess('账户“' + row.accountName + '”已冻结！');
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /**解冻按钮操作**/
    accountUnlock(row) {
      this.$modal.confirm('是否解除账户”' + row.accountName + '“的冻结状态？').then(() => {
        this.loading = true;
        row.status = 0;
        return lockAccount(row);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess('已解除对账户“' + row.accountName + '”的冻结状态！');
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/userAccount/export', {
        ...this.queryParams
      }, `userAccount_${new Date().getTime()}.xlsx`)
    },
    /**格式化金额*/
    formatCurrency(row, column) {
      return new Intl.NumberFormat('zh-CN', {
        style: 'currency',
        currency: 'CNY',
        minimumFractionDigits: 2
      }).format(row[column.property]);
    }
  }
};
</script>
