<template>
  <div class="app-container">
    <el-form
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      label-width="68px"
    >
      <el-form-item label="用户昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          clearable
          placeholder="请输入用户昵称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" clearable placeholder="性别">
          <el-option
            v-for="sex in sexOption"
            :key="sex.value"
            :label="sex.label"
            :value="sex.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="openId" prop="openId">
        <el-input
          v-model="queryParams.openId"
          clearable
          placeholder="请输入openId"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="dateRange"
          end-placeholder="结束时间"
          range-separator="-"
          size="small"
          start-placeholder="开始时间"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['orderDishes:userWechat:add']"
          >新增</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:userWechat:edit']"
          :disabled="single"
          icon="el-icon-edit"
          size="mini"
          type="success"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:userWechat:remove']"
          >删除</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['system:userWechat:export']"
          icon="el-icon-download"
          size="mini"
          type="warning"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="userWechatList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="应用编号" prop="appId" />
      <el-table-column align="center" label="用户头像" prop="headimgurl">
        <template slot-scope="scope">
          <img :src="scope.row.headimgurl" height="40" width="40" />
        </template>
      </el-table-column>
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="openId"
        prop="openId"
      />
      <el-table-column
        :show-overflow-tooltip="true"
        align="center"
        label="用户昵称"
        prop="nickName"
      />
      <el-table-column
        :formatter="sexFormat"
        align="center"
        label="性别"
        prop="sex"
      />
      <el-table-column align="center" label="真实姓名" prop="realName" />
      <el-table-column align="center" label="手机号码" prop="phoneNumber" />
      <el-table-column align="center" label="语言" prop="language" />
      <el-table-column align="center" label="国家" prop="country" />
      <el-table-column align="center" label="城市" prop="city" />
      <el-table-column align="center" label="省份" prop="province" />
      <el-table-column
        align="center"
        class-name="small-padding fixed-width"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:userWechat:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-hasPermi="['system:userWechat:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改微信用户对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      width="800px"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <!-- <el-col :span="12">
            <el-form-item label="应用Id" prop="appId">
              <el-input
                v-model="form.appId"
                placeholder="应用Id"
                :disabled="isDisabled"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="开放平台绑定id" prop="unionId">
              <el-input
                v-model="form.unionId"
                placeholder="请输入开放平台绑定id"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="日期" prop="bondTime">
              <el-date-picker
                v-model="form.bondTime"
                type="datetime"
                placeholder="选择日期时间"
                align="right"
              >
              </el-date-picker>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="城市" prop="city">
              <el-input
                v-model="form.city"
                :disabled="isDisabled"
                placeholder="城市"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="国家" prop="country">
              <el-input
                v-model="form.country"
                :disabled="isDisabled"
                placeholder="国家"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-input
                v-model="form.createTime"
                :disabled="isDisabled"
                placeholder="创建时间"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="语言" prop="language">
              <el-input
                v-model="form.language"
                :disabled="isDisabled"
                placeholder="语言"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="用户昵称" prop="nickName">
              <el-input
                v-model="form.nickName"
                :disabled="isDisabled"
                placeholder="用户昵称"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="真实姓名" prop="realName">
              <el-input
                v-model="form.realName"
                :disabled="isDisabled"
                placeholder="真实姓名"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" prop="phoneNumber">
              <el-input
                v-model="form.phoneNumber"
                :disabled="isDisabled"
                placeholder="手机号码"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="openId" prop="openId">
              <el-input
                v-model="form.openId"
                :disabled="isDisabled"
                placeholder="微信唯一标识"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省份" prop="province">
              <el-input
                v-model="form.province"
                :disabled="isDisabled"
                placeholder="省份"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <!-- <el-input v-model="form.sex" placeholder="请输入性别" :disabled="isDisabled" /> -->
              <el-select
                v-model="form.sex"
                :disabled="isDisabled"
                clearable
                placeholder="请选择性别"
              >
                <el-option
                  v-for="sex in sexOption"
                  :key="sex.value"
                  :label="sex.label"
                  :value="sex.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-input
                v-model="form.updateTime"
                placeholder="请输入更新时间"
                :disabled="isDisabled"
              />
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12"> -->
            <!-- <el-form-item label="更新时间" prop="updatedTime">
          <el-input v-model="form.updatedTime" placeholder="请输入更新时间" />
        </el-form-item> -->
          <!-- </el-col> -->
        </el-row>
      </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 {
  listUserWechat,
  getUserWechat,
  delUserWechat,
  addUserWechat,
  updateUserWechat,
  exportUserWechat
} from "@/api/system/userWechat";

export default {
  name: 'UserWechat',
  data() {
    return {
      dateRange: [],
      sexOption: [
        { value: '1', label: '男' },
        { value: '2', label: '女' }
      ],
      isDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 微信用户表格数据
      userWechatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      statusOptions: [],
      // 状态数据字典
      typeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        createBy: undefined,
        appId: undefined,
        headimgurl: undefined,
        bondTime: undefined,
        city: undefined,
        country: undefined,
        createTime: undefined,
        language: undefined,
        nickName: undefined,
        realName: undefined,
        phoneNumber: undefined,
        openId: undefined,
        province: undefined,
        sex: undefined,
        unionId: undefined,
        updateTime: undefined,
        updatedTime: undefined,
        timestamp:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xxx: [{ required: true, message: "xxx不能为空", trigger: "blur" }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    rangeDateToAre() {
      if (this.dateRange != null) {
        this.queryParams.beginTime = this.dateRange[0];
        this.queryParams.endTime = this.dateRange[1];
      }
    },
    sexFormat(row) {
      if(row.sex == ''||row.sex == null){
        return '';
      }else{
        return row.sex == "1" ? "男" : "女";
      }
    },
    /** 查询微信用户列表 */
    getList() {
      this.loading = true;
      listUserWechat(this.queryParams).then(response => {
        this.userWechatList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        appId: undefined,
        headimgurl: undefined,
        bondTime: undefined,
        city: undefined,
        country: undefined,
        createTime: undefined,
        language: undefined,
        nickName: undefined,
        realName: undefined,
        phoneNumber: undefined,
        openId: undefined,
        province: undefined,
        sex: undefined,
        unionId: undefined,
        updateTime: undefined,
        updatedTime: undefined,
        beginTime: undefined,
        endTime: undefined,
        timestamp:undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.rangeDateToAre();
      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.isDisabled = false;
      this.reset();
      this.open = true;
      this.title = "添加微信用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isDisabled = true
      this.reset()
      const id = row.id || this.ids;
      getUserWechat(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改微信用户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.form.bondTime!=null){
        this.form.bondTime = new Date(this.form.bondTime.toString().replace(/-/g,'/')).getTime();
      }
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateUserWechat(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addUserWechat(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除id为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delUserWechat(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有微信用户数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportUserWechat(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    }
  }
};
</script>
