<!--
 * @Description: 
 * @Author: gcz
 * @Date: 2024-03-20 09:10:27
 * @LastEditors: gcz
 * @LastEditTime: 2024-03-26 11:13:15
 * @FilePath: \tourism_admin_ui\src\views\merchant\merchantMr.vue
 * @Copyright: Copyright (c) 2016~2024 by gcz, All Rights Reserved. 
-->
<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="商家名称" prop="merchantName">
        <el-input v-model="queryParams.merchantName" placeholder="请输入商家名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商家手机号" prop="contactsMobile" label-width="100px">
        <el-input v-model="queryParams.contactsMobile" placeholder="请输入商家手机号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="商家状态" clearable>
          <el-option v-for="dict in statusList" :key="dict.value" :label="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">搜索</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="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['merchantMr:merchantMr: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="['system:post: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="['system:post: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="['system:post:export']"
          >导出</el-button> 
        </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" width="60" type="index"></el-table-column>
      <el-table-column label="账号" align="center" prop="accountNo" />
      <el-table-column label="商家名称" align="center" prop="merchantName" />
      <el-table-column label="商家地址" align="center" prop="address" />
      <el-table-column label="商家手机号" align="center" prop="contactsMobile" />
      <el-table-column label="appid" align="center" prop="appid" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <!-- <dict-tag :options="statusList" :value="scope.row.status"/> -->
          <el-tag type="primary" v-if="scope.row.status === 0">正常</el-tag>
          <el-tag type="danger" v-else-if="scope.row.status === 1">停用</el-tag>
          <!-- <el-tag type="warning" v-else-if="scope.row.status === 2">待审核</el-tag> -->
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" 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="['merchantMr:merchantMr:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['merchantMr:merchantMr:delete']">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-key" @click="handleResetPwd(scope.row)"
            v-hasPermi="['merchantMr:merchantMr:edit']">重置密码</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="帐号" prop="accountNo">
          <el-input v-model="form.accountNo" placeholder="请输入商家帐号" :disabled="title=='修改商家'" />
        </el-form-item>
        <el-form-item label="商家名称" prop="merchantName">
          <el-input v-model="form.merchantName" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactsName">
          <el-input v-model="form.contactsName" placeholder="请输入商家联系人名称" />
        </el-form-item>
        <el-form-item label="手机号" prop="contactsMobile">
          <el-input v-model="form.contactsMobile" placeholder="请输入编商家联系人手机号" />
        </el-form-item>
        <el-form-item label="商家地址" prop="address">
          <el-input v-model="form.address" />
        </el-form-item>
        <el-form-item label="商家状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusList" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-loading.fullscreen.lock="loading" element-loading-text="请稍等..."
          element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)">
          <span v-if="loading">提交中...</span>
          <span v-else>保存</span>
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { pageList, insertOrUpdate, getSelectById, deleteById, enable, resetPwd } from "@/api/merchant";

export default {
  name: "merchantMr",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        merchantName: undefined,
        contactsMobile: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantName: [
          { required: true, message: "商家名称不能为空", trigger: "blur" }
        ],
        accountNo: [
          { required: true, message: "帐号不能为空", trigger: "blur" }
        ],
        contactsName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactsMobile: [
          { required: true, message: "商家联系人手机号不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "商家地址不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "商家状态不能为空", trigger: "blur" }
        ],
      },
      statusList: [
        { value: 0, label: "正常", row: { listClass: 'primary' } },
        { value: 1, label: "停用", row: { listClass: 'danger' } },
        // { value: 2, label: "待审核", row: { listClass: 'warning' } },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商家列表 */
    getList() {
      this.loading = true;
      pageList(this.queryParams).then(response => {
        this.dataList = response.data.rows;
        this.total = response.data.total;
      }).finally(()=>{
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        accountNo: undefined,
        merchantName: undefined,
        contactsName: undefined,
        contactsMobile: undefined,
        address: undefined,
        appid: undefined,
        remark: undefined,
      };
      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.loading = true;
      this.reset();
      const id = row.id || this.ids
      getSelectById(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商家";
      }).finally(()=>{
        this.loading = false
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.form.id != undefined) {
            insertOrUpdate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            insertOrUpdate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商家"' + row.merchantName + '"的数据项？').then(function () {
        return deleteById(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 重置密码按钮操作 */
    handleResetPwd(row) {
      this.$confirm("是否对" + row.merchantName + "的数据进行密码重置？重置后的密码为123456！", '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        resetPwd({ merchantId: row.id, newPassword: '123456' }).then((res) => {
          if (res.code == 200) {
            this.$message({
              type: 'success',
              message: '操作成功!'
            });
            this.getList();
          }
        });
      }).catch(() => {});

      // this.$prompt('请输入"' + row.merchantName + '"的新密码', "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   closeOnClickModal: false,
      //   inputPattern: /^.{5,20}$/,
      //   inputErrorMessage: "用户密码长度必须介于 5 和 20 之间",
      //   inputValidator: (value) => {
      //     if (/<|>|"|'|\||\\/.test(value)) {
      //       return "不能包含非法字符：< > \" ' \\\ |"
      //     }
      //   },
      // }).then(({ value }) => {
      //   resetPwd(row.userId, value).then(response => {
      //     this.$modal.msgSuccess("修改成功，新密码是：" + value);
      //   });
      // }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>