<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="注册日期">
        <el-date-picker
          v-model="searchDate"
          type="daterange"
          value-format="yyyy-MM-dd HH:mm:ss"
          align="left"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          size="small"
          style="width: 260px"
          :default-time="['00:00:00', '23:59:59']"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名" prop="nikeName">
        <el-input
          v-model="queryParams.nikeName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="全部" value></el-option>
          <el-option label="正常" value="0"></el-option>
          <el-option label="停用" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="handleQuery"
          plain
        >搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery"
        >重置</el-button>

      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="dataList" border stripe>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column
        label="用户名"
        align="center"
        prop="nikeName"
        show-overflow-tooltip
      />
      <el-table-column
        label="详细地址"
        align="center"
        prop="detail"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ parseAddress(scope.row) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="注册时间"
        align="center"
        prop="createTime"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="shopState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == '0'" type="success">
            正常</el-tag
          >
          <el-tag v-if="scope.row.status == '1'" type="info">
            停用</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-circle-check"
            @click="takeOff(scope.row)"
            v-hasPermi="['shop:audit:audit']"
            v-if="scope.row.status == '0'"
          >停用
          </el-button>
          <el-button
            type="text"
            icon="el-icon-circle-check"
            @click="takeOn(scope.row)"
            v-hasPermi="['shop:audit:audit']"
            v-if="scope.row.status == '1'"
          >开启
          </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"
        label-width="120px"
        style="height: 100px"
      >
        <el-input
          type="textarea"
          :rows="4"
          v-model="form.closeReason"
          placeholder="请输入停用的原因"
        />
      </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 { memberList, takeOn, takeOff } from "@/api/member/member";

export default {
  name: "adminMember",
  data() {
    return {
      showSearch: true,
      loading: true,
      total: 0,
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: "",
        status: "",
        nikeName: "",
        beginTime: "",
        endTime: ""
      },
      form: {},
      // 弹出层标题
      title: "",
      open: false,

      searchDate: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待审核列表 */
    getList() {
      this.loading = true;
      this.loading = true;
      if(this.searchDate == null){
        this.searchDate = ''
      }
      const [searchBegin, searchEnd] = this.searchDate;
      let sBegin = searchBegin;
      let sEnd = searchEnd;
      if (typeof searchBegin == "undefined") {
        sBegin = "";
      }
      if (typeof searchEnd == "undefined") {
        sEnd = "";
      }
      this.queryParams.beginTime = sBegin;
      this.queryParams.endTime = sEnd;
      memberList(this.queryParams)
        .then((response) => {
          console.log(response)
          this.dataList = response.rows;

          this.total = parseInt(response.total);
        }).catch(err => {
          console.log(err)
        }).finally(() => {
          this.loading = false;
        });
    },
    // 表单重置
    reset() {
      this.form = {
        memberId: null,
        closeReason: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.searchDate = [];
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        phone: "",
        status: "",
        nikeName: "",
        beginTime: "",
        endTime: ""
      };
      this.handleQuery();
    },

    parseAddress(row) {
      if (row.addressList != null && row.addressList.length > 0) {
        return row.addressList[0].address + row.addressList[0].detailed
      } else {
        return "--"
      }

    },
    takeOff(row) {
      this.reset();
      this.form.memberId = row.memberId;
      this.open = true;
      this.title = "停用";
    },
    takeOn(row) {
      const memberId = row.memberId;
      const nikeName = row.nikeName == null ? "" : row.nikeName;
      this.$confirm(
        '确认启用用户: <span style="font-weight: bold">' +
        nikeName +
        "</span> 吗?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          dangerouslyUseHTMLString: true,
          type: "warning",
        }
      ).then(() => {
        takeOn({
          memberId: memberId
        }).then(res => {
          if (res.code == 200) {
            this.$message.success("启用成功！")
            this.getList();
          } else {
            this.$message.error("启用失败！")
          }
        }).catch(err => {
          this.$message.error("操作失败！")
        })
      }).catch(() => {

      });
    },

    /** 提交按钮 */
    submitForm() {
      if (this.form.closeReason == null || this.form.closeReason == '') {
        this.$message.error("停用理由必填")
        return;
      }
      if (this.form.memberId != null) {
        takeOff(this.form).then(res => {
          if (res. code == 200) {
            this.$message.success("停用成功");
          } else {
            this.$message.error(res.msg)
          }
          this.open = false;
          this.getList();
          console.log(res)
        }).catch(err => {
          console.log(err)
          this.$message.error("操作失败")
        })

      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  },
};
</script>

<style>
</style>
