<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="用户账号" prop="email">
        <el-input v-model="queryParams.email" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="queryParams.realName" placeholder="请输入真实姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系电话" prop="concatPhone">
        <el-input v-model="queryParams.concatPhone" placeholder="请输入联系电话" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="工作单位名称" prop="registerCompanyName">
        <el-input v-model="queryParams.registerCompanyName" placeholder="请输入工作单位名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位类别" prop="companyClass">
        <el-input v-model="queryParams.companyClass" placeholder="请输入单位类别" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户来源" prop="memberSource">
        <el-input v-model="queryParams.memberSource" placeholder="请输入用户来源" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户账号" prop="userAccount">
        <el-input v-model="queryParams.userAccount" placeholder="请输入用户账号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户类别" prop="userClass">
        <el-input v-model="queryParams.userClass" placeholder="请输入用户类别" clearable @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="['cs:csOrderMemberUser:add']">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['cs:csOrderMemberUser:edit']">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['cs:csOrderMemberUser:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cs:csOrderMemberUser:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="csOrderMemberUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="sort">
        <template slot-scope="scope">
          {{ ((queryParams.pageNum - 1) * queryParams.pageSize) + (scope.$index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="用户账号" align="center" prop="userAccount" />
      <el-table-column label="邮箱" align="center" prop="email" />
       <el-table-column label="联系电话" align="center" prop="concatPhone" /> 
      <el-table-column label="真实姓名" align="center" prop="realName" />
      <el-table-column label="单位名(注册信息)" align="center" prop="registerCompanyName" />
      <el-table-column label="单位类别" align="center" prop="companyClass" />
      <el-table-column label="用户来源" align="center" prop="memberSource" />
      <el-table-column label="用户类别" align="center" prop="userClass" />
      <el-table-column label="用户类型" align="center" prop="memberType">
        <template slot-scope="scope">
          <span>{{ scope.row.memberType == '0' ? '内部用户' : '公开用户' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开户状态" align="center" prop="openStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.openStatus == '0' ? '未开户' : '已开户未绑定' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="职称/职务" align="center" prop="professionalTitle" />
      <el-table-column label="标签" align="center" prop="memberTag" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="状态" align="center" prop="userStatus">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.userStatus" @change="updateUserStatus(scope.row, $event)" active-value="0"
            inactive-value="1" active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remarks" />
      <el-table-column label="创建时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['cs:csOrderMemberUser:edit']">工单用户详情</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csOrderMemberUser:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改工单用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="邮箱（账号）" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱（账号）" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input show-password v-model="form.password" placeholder="长度在6到25个字符，且至少包含1个字母、数字、特殊字符" />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input show-password v-model="form.checkPass" placeholder="长度在6到25个字符，且至少包含1个字母、数字、特殊字符" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="联系电话" prop="concatPhone">
          <el-input v-model="form.concatPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="单位名(注册信息)" prop="registerCompanyName">
          <el-input v-model="form.registerCompanyName" 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>

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

<script>
import {
  listCsOrderMemberUser, getCsOrderMemberUser,
  delCsOrderMemberUser, addCsOrderMemberUser, updateUserStatus,
  updateCsOrderMemberUser
} from "@/api/cs/csOrderMemberUser";
import {mailValidator, phoneValidator, pwdValValidate} from "@/utils/validate";

export default {
  name: "CsOrderMemberUser",

  data() {
  var checkEmail = (rule, value, callback) => {
        const regEmail = /^\w+@\w+(\.\w+)+$/
        const pattern=/[a-zA-Z\u0300-\u036F]+/
        if (pattern.test(value) || regEmail.test(value) ) {
          // 合法邮箱
          return callback()
        }
        callback(new Error('请输入合法邮箱'))
      }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单用户信息表格数据
      csOrderMemberUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        password: null,
        name: null,
        realName: null,
        phone: null,
        concatPhone: null,
        email: null,
        registerCompanyName: null,
        companyId: null,
        companyName: null,
        companyClassId: null,
        companyClass: null,
        deptId: null,
        deptName: null,
        professionalTitle: null,
        occupation: null,
        jobNumber: null,
        status: null,
        sort: null,
        acUid: null,
        guideStatus: null,
        loginTime: null,
        lastLoginTime: null,
        loginToken: null,
        memberSourceId: null,
        memberSource: null,
        memberType: null,
        openStatus: null,
        userType: '0',
        userAccount: null,
        userClassId: null,
        userClass: null,
        isPay: null,
        nowBalance: null,
        centerLiaisonId: null,
        centerLiaison: null,
        serviceStatus: null,
        queueTypeId: null,
        queueType: null,
        theme: null,
        nodeNum: null,
        allocationTime: null,
        useTime: null,
        useContacts: null,
        recoveryRecord: null,
        memberTag: null,
        remarks: null,
        invitePeople: null,
        inviteCode: null,
        createAt: null,
        updateAt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        email: [
          { required: true, message: "请输入用户邮箱", trigger: "blur" },
          { required: true, validator:'checkEmail', trigger: 'blur' }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { required: true, validator: pwdValValidate, trigger: "blur" }
        ],
        checkPass: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { required: true, validator: pwdValValidate, trigger: "blur" }
        ],
        realName: [
          { required: true, message: "请输入真实姓名", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { required: true, validator: phoneValidator, trigger: "blur" }
        ],
        concatPhone: [
          { required: true, message: "请输入联系电话", trigger: "blur" },
          { required: true, validator: phoneValidator, trigger: "blur" }
        ],
        registerCompanyName: [
          { required: true, message: "请输入单位名称", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    updateUserStatus(row, value) {
      updateUserStatus({
        memberUserId: row.memberUserId,
        userStatus: value
      }).then(res => {
        if (res.code == 200) {
          this.$message({
            message: '修改用户状态成功',
            type: 'success'
          })
        }
      })
    },
    /** 查询工单用户信息列表 */
    getList() {
      this.loading = true;
      listCsOrderMemberUser(this.queryParams).then(response => {
        this.csOrderMemberUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        memberUserId: null,
        email: null,
        password: null,
        checkPass: null,
        realName: null,
        phone: null,
        concatPhone: null,
        registerCompanyName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.memberUserId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工单用户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ name: 'workOrderUserDetail', params: { id: row.memberUserId } })
      return
      this.reset();
      const memberUserId = row.memberUserId || this.ids
      getCsOrderMemberUser(memberUserId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工单用户信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.memberUserId != null) {
            this.open = false;
            this.getList();
            // updateCsOrderMemberUser(this.form).then(response => {
            //   this.$modal.msgSuccess("修改成功");
            // });
          } else {
            addCsOrderMemberUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const memberUserIds = row.memberUserId || this.ids;
      this.$modal.confirm('是否确认删除工单用户信息编号为"' + memberUserIds + '"的数据项？').then(function () {
        return delCsOrderMemberUser(memberUserIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cs/csOrderMemberUser/export', {
        ...this.queryParams
      }, `工单用户管理_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
