<template>
  <div class="app-container system-oneself">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      class="main-search-card"
      label-width="68px"
    >
      <el-form-item label="用户账号" prop="userName">
        <el-input
          v-model="queryParams.userName"
          clearable
          placeholder="请输入用户账号"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          clearable
          placeholder="请输入手机号码"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </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>
    <div class="main-content-card">
    <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
          v-hasPermi="['system:oneself:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出</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 :index="indexFormatter" align="center" label="序号" type="index" width="80px"></el-table-column>
      <el-table-column align="left" label="账号" prop="userName" />
      <el-table-column align="left" label="昵称" prop="nickName" />
      <el-table-column align="left" label="邮箱" prop="email" />
      <el-table-column align="left" label="手机号码" prop="phonenumber" width="160" />
      <el-table-column :formatter="sexFormatter" align="center" label="性别" prop="sex" width="80" />
      <el-table-column align="center" label="头像" prop="avatar" width="100">
        <template slot-scope="scope">
          <div class="demo-image__preview">
            <el-image
              :preview-src-list="[getIotFileUrl(scope.row.avatar)]"
              :src="getIotFileUrl(scope.row.avatar)"
              style="width: 30px; height: 35px;"
            ></el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="帐号状态" prop="status" width="120">
        <template slot-scope="scope">
          <el-popconfirm
            :title="scope.row.status === '0'? '确定启用此账号吗？':'确定停用此账号吗？'"
            @cancel="() => { scope.row.status === '0' ? scope.row.status = '1': scope.row.status = '0'}"
            @confirm="statusUpdate(scope.row)"
          >
            <el-switch
              slot="reference"
              v-model="scope.row.status"
              active-color="#13ce66"
              active-value="0"
              inactive-color="#ff4949"
              inactive-value="1"
            ></el-switch>
          </el-popconfirm>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:oneself:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['system:oneself: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"
    />
    </div>
    <!-- 添加或修改个人用户信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" class="eldialog-wrap" 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 class="table-avatar" label prop="avatar" style="margin-left: 0px;">
              <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 v-for="(keys, vals) in sexOption" :key="keys" :label="vals">{{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>
