<template>
  <div class="app-container system-oneself">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          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>
        <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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:oneself:add']"
        >新增</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:oneself:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="oneselfList">
      <el-table-column type="index" label="序号" align="center" :index="indexFormatter" width="80px"></el-table-column>
      <el-table-column label="账号" align="left" prop="userName" />
      <el-table-column label="昵称" align="left" prop="nickName" />
      <el-table-column label="邮箱" align="left" prop="email" />
      <el-table-column label="手机号码" align="left" width="160" prop="phonenumber" />
      <el-table-column label="性别" align="center" prop="sex" width="80" :formatter="sexFormatter" />
      <el-table-column label="头像" align="center" width="100" prop="avatar">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image
              style="width: 30px; height: 35px;"
              :src="getIotFileUrl(scope.row.avatar)"
              :preview-src-list="[getIotFileUrl(scope.row.avatar)]"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="帐号状态" align="center" width="120" prop="status">
        <template slot-scope="scope">
          <el-popconfirm
            @confirm="statusUpdate(scope.row)"
            @cancel="() => { scope.row.status === '0' ? scope.row.status = '1': scope.row.status = '0'}"
            :title="scope.row.status === '0'? '确定启用此账号吗？':'确定停用此账号吗？'"
          >
            <el-switch
              slot="reference"
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="0"
              inactive-value="1"
            ></el-switch>
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200" 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="['system:oneself:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:oneself: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 class="eldialog-wrap" :close-on-click-modal="false" :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="16">
            <el-form-item label="账号:" prop="userName">
              <el-input v-model="form.userName" 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="email">
              <el-input v-model="form.email" placeholder="请输入用户邮箱" />
            </el-form-item>

            <!-- <el-form-item label="密码:" prop="password">
              <el-input type="password" v-model="form.password" placeholder="请输入密码" />
            </el-form-item>-->
          </el-col>
          <el-col :span="8">
            <el-form-item label class="table-avatar" style="margin-left: 0px;" prop="avatar">
              <upload-image
                :fileType="fileType"
                :imageUrl="form.avatar"
                @eventUrl="(data) => {form.avatar = data}"
              />
              <span
                style="
                line-height: 20px;
                font-size: 15px;
                font-weight: 600;
                display: flex;
                width: 100%;
                justify-content: center;
                "
              >用户头像</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="手机号码:" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>

        <el-form-item label="性别:" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio :label="vals" v-for="(keys, vals) in sexOption" :key="keys">{{keys}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOneself,
  getOneself,
  delOneself,
  addOneself,
  updateOneself,
  exportOneself
} from "@/api/system/oneself";
import { getIotFileUrl } from "@/utils/hciot";
import UploadImage from "@/components/Upload/Image.vue";

const sexOption = {
  0: "男",
  1: "女",
  2: "未知"
};

export default {
  name: "Oneself",
  components: {
    UploadImage
  },
  data() {
    return {
      sexOption,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个人用户信息表格数据
      oneselfList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      fileType: ["image/jpeg", "image/png"],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        phonenumber: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1(3[0-9]|4[5,7]|5[0,1,2,3,5,6,7,8,9]|6[2,5,6,7]|7[0,1,7,8]|8[0-9]|9[1,8,9])\d{8}$/,
            message: "手机号码不正确。"
          }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户账号不能为空", trigger: "blur" }
        ],
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            pattern: /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
            message: "邮箱格式不正确。"
          }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getIotFileUrl,
    statusUpdate(row) {
      updateOneself(row).then(response => {
        if (row.status === "0") {
          this.msgSuccess("已启用!");
        } else if (row.status === "1") {
          this.msgSuccess("已停用!");
        }
        this.getList();
      });
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    sexFormatter(row) {
      return this.sexOption[row.sex];
    },
    statusFormatter(row) {
      return row.status === "0" ? "正常" : "停用";
    },
    /** 查询个人用户信息列表 */
    getList() {
      this.loading = true;
      listOneself(this.queryParams).then(response => {
        this.oneselfList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        userName: null,
        nickName: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.userId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加个人用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId;
      getOneself(userId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个人用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.userId != null) {
            updateOneself(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOneself(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId;
      this.$confirm("是否删除该选项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delOneself(userIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有个人用户信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportOneself(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        });
    }
  }
};
</script>
<style lang="scss">
.system-oneself {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
    .table-avatar {
      .el-form-item__content {
        margin-left: 0px !important;
      }
    }
  }
}
</style>
