<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable filterable>
          <el-option v-for="dict in typeList" :key="dict.value + 'typeList'" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" :disabled="single" @click="handleUpdate" v-hasPermi="['business:COMPANYUSER:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['business:COMPANYUSER:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  :data="COMPANYUSERList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="left" prop="nickName">
        <template slot-scope="scope">
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="userName">
        <template slot-scope="scope">
          {{ scope.row.userName }}
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 1">普通用户</span>
          <span v-else-if="scope.row.type == 2">专家</span>
        </template>
      </el-table-column>
      <el-table-column label="学历" align="center" prop="education">
        <template slot-scope="scope">
          {{ scope.row.education }}
        </template>
      </el-table-column>
      <el-table-column label="工作经验" align="center" prop="experience">
        <template slot-scope="scope">
          {{ scope.row.experience }}
        </template>
      </el-table-column>
      <el-table-column label="专业领域" align="center" prop="field">
        <template slot-scope="scope">
          {{ scope.row.field }}
        </template>
      </el-table-column>
      <el-table-column label="研究方向" align="center" prop="research">
        <template slot-scope="scope">
          {{ scope.row.research }}
        </template>
      </el-table-column>
      <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="['business:COMPANYUSER:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['business:COMPANYUSER: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 :title="title" :visible.sync="open" width="550px" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型" filterable style="width: 100%">
            <el-option v-for="dict in typeList" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="最高学历" prop="education">
          <el-input v-model="form.education" placeholder="请输入学历"></el-input>
        </el-form-item>
        <el-form-item label="工作经验" prop="experience">
          <el-input v-model="form.experience" placeholder="请输入工作经验"></el-input>
        </el-form-item>
        <el-form-item label="专业领域" prop="field">
          <el-input v-model="form.field" placeholder="请输入专业领域"></el-input>
        </el-form-item>
        <el-form-item label="研究方向" prop="research">
          <el-input v-model="form.research" placeholder="请输入研究方向"></el-input>
        </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 { listCOMPANYUSER, getCOMPANYUSER, delCOMPANYUSER, addCOMPANYUSER, updateCOMPANYUSER } from '@/api/portal/COMPANYUSER/COMPANYUSER.js';

export default {
  name: 'COMPANYUSER',
  data() {
    return {
      typeList: [
        { label: '普通用户', value: '1' },
        { label: '专家', value: '2' }
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 门户用户表格数据
      COMPANYUSERList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        nickName: null,
        userName: null,
        password: null,
        loginIp: null,
        loginDate: null,
        type: null,
        baseInfo: null,
        education: null,
        experience: null,
        field: null,
        research: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [{ required: true, message: '$comment不能为空', trigger: 'blur' }],
        delFlag: [{ required: true, message: '删除标识不能为空', trigger: 'blur' }],
        nickName: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        userName: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }],
        loginIp: [{ required: true, message: '登录ip不能为空', trigger: 'blur' }],
        loginDate: [{ required: true, message: '登录日期不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '类型不能为空', trigger: 'change' }],
        baseInfo: [{ required: true, message: '基本信息不能为空', trigger: 'blur' }],
        education: [{ required: true, message: '学历不能为空', trigger: 'blur' }],
        experience: [{ required: true, message: '工作经验不能为空', trigger: 'blur' }],
        field: [{ required: true, message: '专业领域不能为空', trigger: 'blur' }],
        research: [{ required: true, message: '研究方向不能为空', trigger: 'blur' }]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询门户用户列表 */
    getList() {
      this.loading = true;
      listCOMPANYUSER(this.queryParams).then(response => {
        this.COMPANYUSERList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        nickName: null,
        userName: null,
        password: null,
        loginIp: null,
        loginDate: null,
        type: null,
        baseInfo: null,
        education: null,
        experience: null,
        field: null,
        research: 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.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加门户用户';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCOMPANYUSER(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改门户用户';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCOMPANYUSER(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addCOMPANYUSER(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除这个门户用户吗？')
        .then(function () {
          return delCOMPANYUSER(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess('删除成功');
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'business/COMPANYUSER/export',
        {
          ...this.queryParams
        },
        `COMPANYUSER_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>
