<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="登录名称" prop="userLogin">
        <el-input
          v-model="queryParams.userLogin"
          placeholder="请输入登录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="登录密码" prop="userPwd">-->
<!--        <el-input-->
<!--          v-model="queryParams.userPwd"-->
<!--          placeholder="请输入登录密码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </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="['shopuser:shopuser: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="['shopuser:shopuser: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="['shopuser:shopuser:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['shopuser:shopuser:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopuserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="userId" />-->
      <el-table-column label="登录名称" align="center" prop="userLogin" />

      <el-table-column label="法人姓名" align="center" prop="userName" />
      <el-table-column label="联系电话" align="center" prop="userTel" />
      <el-table-column label="店铺状态" align="center" prop="status" >
        <template slot-scope="scope">
          <p  v-if="scope.row.status==='0'">有效</p>
          <p v-if="scope.row.status==='1'">无效</p>
        </template>
      </el-table-column>



<!--      <el-table-column label="登录密码" align="center" prop="userPwd" type="password" />-->
      <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="['shopuser:shopuser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['shopuser:shopuser: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"
    />

    <!-- 添加或修改店铺用户对话框 -->
    <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="userLogin">
          <el-input v-model="form.userLogin" placeholder="请输入登录名称" />
        </el-form-item>
        <el-form-item label="登录密码" prop="userPwd">
          <el-input v-model="form.userPwd" placeholder="请输入登录密码" type="password" />
        </el-form-item>

        <el-form-item label="法人姓名" prop="userName">
          <el-input
            v-model="form.userName"
            placeholder="请输入法人姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="法人电话" prop="userTel">
          <el-input
            v-model="form.userTel"
            placeholder="请输入法人电话"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="店铺状态">
          <el-radio v-model="form.status" label="0">有效</el-radio>
          <el-radio v-model="form.status" label="1">失效</el-radio>
        </el-form-item>
<!--        <el-form-item label="关联店铺">-->
<!--          <el-select v-model="form.shopsId" placeholder="请选择">-->
<!--            <el-option-->
<!--              v-for="item in postOptions"-->
<!--              :key="item.shopsId"-->
<!--              :label="item.shopsName"-->
<!--              :value="item.shopsId"-->
<!--              :disabled="item.status == 1"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShopuser, getShopuser, delShopuser, addShopuser, updateShopuser, exportShopuser,getShopList } from "@/api/shopuser/shopuser";

export default {
  name: "Shopuser",
  data() {
    return {
      //状态下拉
      options: [{
        value: '0',
        label: '有效'
      }, {
        value: '1',
        label: '无效'
      }],
      postOptions: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺用户表格数据
      shopuserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userLogin: null,
        userPwd: null,
        shopsId: null,
        userName: null,
        status: null,
        userTel: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getShopList();
  },
  methods: {

    getShopList(){

      let that=this;
      getShopList().then(response => {

        console.log(response)
        console.log(response.data)
        that.postOptions=response.data

        console.log( that.postOptions)
      });
    },
    /** 查询店铺用户列表 */
    getList() {

      this.loading = true;
      listShopuser(this.queryParams).then(response => {
        this.shopuserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userId: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        userLogin: null,
        userPwd: null,
        shopsId: null,
        status: null,
        userStatus: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加店铺用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids
      getShopuser(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改店铺用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateShopuser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShopuser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$modal.confirm('是否确认删除店铺用户编号为"' + userIds + '"的数据项？').then(function() {
        return delShopuser(userIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有店铺用户数据项？').then(() => {
        this.exportLoading = true;
        return exportShopuser(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
