<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">    
      <el-form-item label="用户账号" prop="userId2">
        <el-input
            v-model="queryParams.userId2"
            placeholder="请输入用户账号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="昵称" prop="nickName">
        <el-input
            v-model="queryParams.nickName"
            placeholder="请输入昵称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="手机账号" prop="phoneNumber">
        <el-input
            v-model="queryParams.phoneNumber"
            placeholder="请输入手机账号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="免费次数" prop="freeUses">
        <el-input
            v-model="queryParams.freeUses"
            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
                v-for="dict in statusOptions"
                :key="dict.key"
                :label="dict.value"
                :value="dict.key"
            />
        </el-select>
      </el-form-item>    
      <el-form-item label="推广来源" prop="promotionSource">
        <el-input
            v-model="queryParams.promotionSource"
            placeholder="请输入推广来源"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>    
      <el-form-item label="登录类型" prop="loginType">
        <el-select v-model="queryParams.loginType" placeholder="请选择登录类型" clearable size="small">
            <el-option
                v-for="dict in loginTypeOptions"
                :key="dict.key"
                :label="dict.value"
                :value="dict.key"
            />
        </el-select>
      </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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jx_horizon/jxHorizonUser/add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jx_horizon/jxHorizonUser/edit']"
        >修改</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="['jx_horizon/jxHorizonUser/delete']"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="jxHorizonUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />      
      <el-table-column label="" align="center" prop="id" />      
      <el-table-column label="用户账号" align="center" prop="userId2" />      
      <el-table-column label="昵称" align="center" prop="nickName" />      
      <el-table-column label="头像地址" align="center" prop="headUrl" />      
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />      
      <el-table-column label="年龄" align="center" prop="age" />      
      <el-table-column label="手机账号" align="center" prop="phoneNumber" />      
      <el-table-column label="免费次数" align="center" prop="freeUses" />      
      <el-table-column label="账号状态" align="center">
        <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="statusChange(scope.row)"
            ></el-switch>
        </template>
      </el-table-column>      
      <el-table-column label="推广来源" align="center" prop="promotionSource" />      
      <el-table-column label="登录类型" align="center" prop="loginType" :formatter="loginTypeFormat" />      
      <el-table-column label="个人说明" align="center" prop="remark" />      
      <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="['jx_horizon/jxHorizonUser/edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jx_horizon/jxHorizonUser/delete']"
          >删除</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="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">      
      <el-form-item label="系统用户ID" prop="userId">
           <el-input v-model="form.userId" placeholder="请输入系统用户ID" />
      </el-form-item>      
      <el-form-item label="用户账号" prop="userId2">
           <el-input v-model="form.userId2" placeholder="请输入用户账号" />
      </el-form-item>      
      <el-form-item label="昵称" prop="nickName">
           <el-input v-model="form.nickName" placeholder="请输入昵称" />
      </el-form-item>      
      <el-form-item label="头像地址" prop="headUrl">
           <el-input v-model="form.headUrl" placeholder="请输入头像地址" />
      </el-form-item>      
      <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
              <el-option
                  v-for="dict in sexOptions"
                  :key="dict.key"
                  :label="dict.value"                  
                      :value="dict.key"                  
              ></el-option>
          </el-select>
      </el-form-item>      
      <el-form-item label="年龄" prop="age">
           <el-input v-model="form.age" placeholder="请输入年龄" />
      </el-form-item>      
      <el-form-item label="手机账号" prop="phoneNumber">
           <el-input v-model="form.phoneNumber" placeholder="请输入手机账号" />
      </el-form-item>      
      <el-form-item label="免费次数" prop="freeUses">
           <el-input v-model="form.freeUses" placeholder="请输入免费次数" />
      </el-form-item>      
       <el-form-item label="账号状态" prop="status">
           <el-radio-group v-model="form.status">
               <el-radio
                v-for="dict in statusOptions"
                :key="dict.key"
                :label="dict.key"
               >{{dict.value }}</el-radio>
           </el-radio-group>
       </el-form-item>      
      <el-form-item label="地址Ip" prop="ip">
           <el-input v-model="form.ip" placeholder="请输入地址Ip" />
      </el-form-item>      
      <el-form-item label="手机设备编码" prop="cid">
           <el-input v-model="form.cid" placeholder="请输入手机设备编码" />
      </el-form-item>      
      <el-form-item label="推广来源" prop="promotionSource">
           <el-input v-model="form.promotionSource" placeholder="请输入推广来源" />
      </el-form-item>      
      <el-form-item label="登录类型" prop="loginType">
          <el-select v-model="form.loginType" placeholder="请选择登录类型">
              <el-option
                  v-for="dict in loginTypeOptions"
                  :key="dict.key"
                  :label="dict.value"                  
                      :value="dict.key"                  
              ></el-option>
          </el-select>
      </el-form-item>      
      <el-form-item label="个人说明" prop="remark">
           <el-input v-model="form.remark" placeholder="请输入个人说明" />
      </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 {
    listJxHorizonUser,
    getJxHorizonUser,
    delJxHorizonUser,
    addJxHorizonUser,
    updateJxHorizonUser,    
    changeJxHorizonUserStatus,    
} from "@/api/jx_horizon/jxHorizonUser";
export default {
  components:{    
  },
  name: "JxHorizonUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 京西视界用户表格数据
      jxHorizonUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,      
      // sexOptions字典数据
      sexOptions: [],      
      // statusOptions字典数据
      statusOptions: [],      
      // loginTypeOptions字典数据
      loginTypeOptions: [],      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId2: undefined,
        nickName: undefined,
        phoneNumber: undefined,
        freeUses: undefined,
        status: undefined,
        promotionSource: undefined,
        loginType: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: { 
        userId : [
          { required: true, message: "系统用户ID不能为空", trigger: "blur" }
        ],
        nickName : [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        status : [
          { required: true, message: "账号状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {    
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data.values||[];
    });    
    this.getDicts("sys_user_status").then(response => {
      this.statusOptions = response.data.values||[];
    });    
    this.getDicts("sys_login_type").then(response => {
      this.loginTypeOptions = response.data.values||[];
    });
    this.getList();
  },
  methods: {    
    // 账号状态修改
    statusChange(row) {
      console.log('账号信息',row)
      let text = row.status === 1 ? "启用" : "停用";
      this.$confirm('确认要"' + text + '"：吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return changeJxHorizonUserStatus(row.id, row.status);
        }).then(() => {
          this.msgSuccess(text + "成功");
        }).catch(function() {
          row.userStatus =row.userStatus === 0 ?1 : 0;
        });
    },    
    /** 查询京西视界用户列表 */
    getList() {
      this.loading = true;
      listJxHorizonUser(this.queryParams).then(response => {
        let list = response.data.list || [];  
        console.log('获取惊喜视界用户列表',list)      
        this.jxHorizonUserList = list;        
        this.total = response.data.total;
        this.loading = false;
      });
    },    
    // 性别字典翻译
    sexFormat(row, column) {
      return this.selectDictLabel(this.sexOptions, row.sex);
    },    
    // 账号状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },    
    // 登录类型字典翻译
    loginTypeFormat(row, column) {
      return this.selectDictLabel(this.loginTypeOptions, row.loginType);
    },    
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {        
        id: undefined,        
        createdAt: undefined,        
        updatedAt: undefined,        
        deletedAt: undefined,        
        userId: undefined,        
        userId2: undefined,        
        nickName: undefined,        
        headUrl: undefined,        
        sex: undefined,        
        age: undefined,        
        phoneNumber: undefined,        
        freeUses: undefined,        
        status: "0" ,        
        ip: undefined,        
        cid: undefined,        
        promotionSource: undefined,        
        loginType: undefined,        
        remark: undefined,        
      };      
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      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.reset();
      this.open = true;
      this.title = "添加京西视界用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log('修改',row)
      this.reset();
      const id = row.id || this.ids
      getJxHorizonUser(id).then(response => {
        let data = response.data;        
        data.sex = ''+data.sex        
        data.status = ''+data.status        
        data.loginType = ''+data.loginType        
        this.form = data;
        this.open = true;
        this.title = "修改京西视界用户";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateJxHorizonUser(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addJxHorizonUser(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除京西视界用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delJxHorizonUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    }
  }
};
</script>
<style>
</style>