<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="registerPhone">
        <el-input
          v-model="queryParams.registerPhone"
          placeholder="请输入注册手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店名" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option :label="item.name" :value="item.label" v-for="(item,index) in statusOptions" :key="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="提交时间" prop="createAt">
        <el-date-picker
          v-model="createAtRange"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </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="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['mall:mallMemberRegister: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="['mall:mallMemberRegister: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="['mall:mallMemberRegister:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallMemberRegisterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店名" align="center" prop="shopName" />
      <el-table-column label="手机号" align="center" prop="registerPhone" />
      <el-table-column label="店主名" align="center" prop="personName" />
      <el-table-column label="省份" align="center" prop="province" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="区域" align="center" prop="area" />
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <div v-if="scope.row.status == 0">待审核</div>
          <div v-if="scope.row.status == 1">通过</div>
          <div v-if="scope.row.status == 2">拒绝</div>
        </template>
      </el-table-column>
      <el-table-column label="提交时间" align="center" prop="createAt" />
      <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-info"
            @click="handleUpdate(scope.row,0)"
            v-if="scope.row.status == 0"
            v-hasPermi="['mall:mallMemberRegister:edit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,1)"
            v-hasPermi="['mall:mallMemberRegister:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallMemberRegister: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="100px">
        <el-form-item label="注册手机号" prop="registerPhone" v-if="flag == 1">
          <el-input v-model="form.registerPhone" placeholder="请输入注册手机号" disabled />
        </el-form-item>
        <el-form-item label="店名" prop="shopName" v-if="flag == 1">
          <el-input v-model="form.shopName" placeholder="请输入店名" disabled />
        </el-form-item>
        <el-form-item label="店主名" prop="personName" v-if="flag == 1">
          <el-input v-model="form.personName" placeholder="请输入店主名" disabled />
        </el-form-item>
        <el-form-item label="省" prop="province" v-if="flag == 1">
          <el-input v-model="form.province" placeholder="请输入省" disabled />
        </el-form-item>
        <el-form-item label="市" prop="city" v-if="flag == 1">
          <el-input v-model="form.city" placeholder="请输入市" disabled />
        </el-form-item>
        <el-form-item label="区" prop="area" v-if="flag == 1">
          <el-input v-model="form.area" placeholder="请输入区" disabled />
        </el-form-item>
        <el-form-item label="详细地址" prop="address" v-if="flag == 1">
          <el-input v-model="form.address" placeholder="请输入详细地址" disabled />
        </el-form-item>
        <el-form-item label="业务员" prop="salesmanId" v-if="flag == 0 || form.status != 0">
          <el-select v-model="form.salesmanId" filterable placeholder="请选择" :disabled="flag == 1">
            <el-option
              v-for="(item,index) in salesmanList"
              :key="index"
              :label="item.name+item.phone"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.status" :disabled="flag == 1">
            <el-radio :label="item.label" v-for="(item,index) in statusOptions" :key="index">{{ item.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="提交时间" prop="createAt" v-if="flag == 1">
          <el-input v-model="form.createAt" placeholder="请输入创建时间" disabled />
        </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 { examine, listMallMemberRegister, getMallMemberRegister, delMallMemberRegister, addMallMemberRegister, updateMallMemberRegister, exportMallMemberRegister } from "@/api/mall/mallMemberRegister";
import { selectList } from "@/api/mall/mallSalesman";

export default {
  name: "MallMemberRegister",
  data() {
    return {
      statusOptions:[
        {name:'待审核',label:0},
        {name:'通过',label:1},
        {name:'拒绝',label:2},
      ],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户注册表格数据
      mallMemberRegisterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      createAtRange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        registerPhone: null,
        shopName: null,
        personName: null,
        province: null,
        city: null,
        area: null,
        address: null,
        password: null,
        status: null,
        createAt: null,
        updateAt: null,
        delFlag: null,
        params:{
          beginCreateAt: null,
          endCreateAt: null
        }
      },
      // 表单参数
      form: {
        salesmanId: 0
      },
      // 表单校验
      rules: {
        salesmanId: [
          { required: true, message: "请选择业务员", trigger: "blur" }
        ],
      },
      salesmanList:[],
      flag:0
    };
  },
  created() {
    this.getList();
    selectList().then(res =>{
      this.salesmanList = res.data
    })
  },
  methods: {
    /** 查询用户注册列表 */
    getList() {
      this.loading = true;
      if (null != this.createAtRange && '' != this.createAtRange) {
        this.queryParams.params["beginCreateAt"] = this.createAtRange[0];
        this.queryParams.params["endCreateAt"] = this.createAtRange[1];
      }
      listMallMemberRegister(this.queryParams).then(response => {
        this.mallMemberRegisterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        registerPhone: null,
        shopName: null,
        personName: null,
        province: null,
        city: null,
        area: null,
        address: null,
        password: null,
        status: 0,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.createAtRange = []
      this.queryParams.params["beginCreateAt"] = '';
      this.queryParams.params["endCreateAt"] = '';
      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,flag) {
      this.flag = flag
      this.reset();
      const id = row.id || this.ids
      getMallMemberRegister(id).then(response => {
        this.form = response.data;
        this.open = true;
        if (this.flag == 0){
          this.title = "审核";
        }else{
          this.title = "用户注册信息";
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if (this.flag == 0){
              examine(this.form).then(response => {
                this.msgSuccess("审核成功");
                this.open = false;
                this.getList();
              });
            }else if(this.flag == 1){
              // updateMallMemberRegister(this.form).then(response => {
              //   this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              // });
            }
          } else {
            // addMallMemberRegister(this.form).then(response => {
            //   this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户注册编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMallMemberRegister(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户注册数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallMemberRegister(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
