<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <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="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="业务员" prop="salesmanId">
        <el-select clearable v-model="queryParams.salesmanId" filterable placeholder="请选择">
          <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="账户状态" prop="status">-->
        <!--<el-select v-model="queryParams.status" placeholder="请选择账户状态" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="vip" prop="vip">
        <el-select v-model="queryParams.vip" placeholder="请选择vip" clearable size="small">
          <el-option label="否" value="0" />
          <el-option label="是" value="1" />
        </el-select>
      </el-form-item>

      <el-form-item label="历史消费金额" prop="stockStart">
        <el-input
          v-model="queryParams.params.allMoneyBegin"
          type="number"
          placeholder="消费金额（起）"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <span style="margin-right:15px">-</span>
      <el-form-item prop="stockEnd">
        <el-input
          v-model="queryParams.params.allMoneyEnd"
          type="number"
          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-table v-loading="loading" :data="mallMemberUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="主键ID" align="center" prop="id" />-->
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="用户头像" align="center" prop="avatar" >
      <template slot-scope="scope">
        <el-image :src="scope.row.avatar" style="width: 80px" :preview-src-list="[scope.row.avatar]"/>
      </template>
      </el-table-column>
      <el-table-column label="用户名称" align="center" prop="name" />
      <el-table-column label="vip" align="center" prop="vip" >
        <template slot-scope="scope">
          <div v-if="scope.row.vip == '0'">否</div>
          <div v-if="scope.row.vip == '1'">是</div>
        </template>
      </el-table-column>
      <el-table-column label="所属业务员" align="center" prop="salesmanName" />
      <!--<el-table-column label="账户状态" align="center" prop="status" />-->
      <el-table-column label="历史消费金额" align="center" prop="allMoney" />
      <el-table-column label="最后登录时间" align="center" prop="loginAt" width="160" />
      <el-table-column label="注册时间" align="center" prop="createAt" width="160" />

    </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="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="用户头像" prop="avatar">
          <imageUpload v-model="form.avatar" :limit="1"/>
        </el-form-item>
        <el-form-item label="用户名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户名称" />
        </el-form-item>
        <el-form-item label="vip" prop="vip">
          <template>
            <el-radio v-model="form.vip" label="0">否</el-radio>
            <el-radio v-model="form.vip" label="1">是</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="业务员" prop="salesmanId">
          <el-select v-model="form.salesmanId" filterable placeholder="请选择" >
            <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="店铺名称" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="地址" prop="memberUserAddress">
          <el-cascader
          v-model="memberUserAddress"
          :options="regionList"
          @change="handleChange"></el-cascader>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="账户状态" prop="status">
            <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>
      <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 { listMallMemberUser, getMallMemberUser, delMallMemberUser, addMallMemberUser,
 updateMallMemberUser, exportMallMemberUser } from "@/api/mall/mallMemberUser";
import { selectList } from "@/api/mall/mallSalesman";
import { getRegionSelectList } from "@/api/mall/mallRegion";
import {customerinformationlist} from '@/api/mall/customerinformation.js'
export default {
  name: "MallMemberUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 会员表格数据
      mallMemberUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        params: {
          allMoneyBegin: null,
          allMoneyEnd: null
        },
        pageNum: 1,
        pageSize: 10,
        phone: null,
        name: null,
        status: null,
        vip: null,
        salesmanId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      salesmanList:[],
      regionList:[],
      memberUserAddress:[]
    };
  },
  created() {
    this.getList();
    selectList().then(res =>{
      this.salesmanList = res.data
    })
    // 省市区列表
    // getRegionSelectList().then(res =>{
    //   this.regionList = res
    // })
  },
  methods: {
    //省市区选择
    handleChange(e){
      console.log(e)
      var addressArr = this.memberUserAddress
      addressArr.forEach((item,index) =>{
        if(index == 0){
          this.form.province = addressArr[index]
        }
        if(index == 1){
          this.form.city = addressArr[index]
        }
        if(index == 2){
          this.form.area = addressArr[index]
        }
      })
    },
    /** 查询会员列表 */
    getList() {
      this.loading = true;
      customerinformationlist(this.queryParams).then(response => {
        this.mallMemberUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        wechat: null,
        sex: null,
        openid: null,
        avatar: null,
        name: null,
        password: null,
        token: null,
        status: "0",
        vip: "0",
        introUser: null,
        money: null,
        allMoney: null,
        qrCode: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      };
      this.memberUserAddress = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.params.allMoneyBegin = null;
      this.queryParams.params.allMoneyEnd = null;
      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.reset();
      const id = row.id || this.ids
      getMallMemberUser(id).then(response => {
        this.form = response.data;
        this.form.status = parseInt(this.form.status)
        this.open = true;
        this.title = "修改会员";
        var arr = []
        var province = this.form.province
        var city = this.form.city
        var area = this.form.area
        arr.push(province,city,area)
        this.memberUserAddress = arr
        console.log(this.memberUserAddress)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMallMemberUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMallMemberUser(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 delMallMemberUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportMallMemberUser(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
