<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目" prop="projectPid">
        <el-select v-model="queryParams.projectPid"
                   filterable
                    clearable
        >
          <el-option v-for="itemP in projectList" :key="itemP.projectPid" :label="itemP.projectName"
            :value="itemP.projectPid" />
        </el-select>
      </el-form-item>
      <el-form-item label="成员" prop="userId">
        <el-select v-model="queryParams.userId" filterable clearable>
          <el-option v-for="itemU in userList" :key="itemU.userId" :label="itemU.userName" :value="itemU.userId" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="角色" prop="roleId">
        <el-select v-model="queryParams.roleId">
          <el-option v-for="itemR in roleList" :key="itemR.roleId" :label="itemR.roleName" :value="itemR.roleId" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="进入项目日期" prop="mBdate">
        <el-date-picker clearable v-model="queryParams.mBdate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择进入项目日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="撤出项目日期" prop="mEdate">
        <el-date-picker clearable v-model="queryParams.mEdate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择撤出项目日期">
        </el-date-picker>
      </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:staff: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="['system:staff: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="['system:staff: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="['system:staff:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="staffList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="projectPid" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="成员名称" align="center" prop="userName" />
      <!-- <el-table-column label="角色" align="center" prop="roleName" /> -->
      <el-table-column label="进入项目日期" align="center" prop="mBdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mBdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="撤出项目日期" align="center" prop="mEdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.mEdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" prop="createTime" />
      <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="['system:staff:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:staff: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="项目" prop="projectPid">
          <el-select v-model="form.projectPid"
                     filterable
                     clearable>
            <el-option v-for="itemP in projectList" :key="itemP.projectPid" :label="itemP.projectName"
              :value="itemP.projectPid" />
          </el-select>
        </el-form-item>
        <el-form-item label="成员" prop="userName">
          <el-select v-model="form.userId" clearable style="width: 100%;" @focus="handleFocusUserInput"
                     clearable>
            <el-option v-for="itemU in userList" :key="itemU.userId.toString()" :label="itemU.userName"
              :value="itemU.userId.toString()" />
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="form.roleId"
                     filterable
                     clearable>
            <el-option v-for="itemR in roleList" :key="itemR.roleId" :label="itemR.roleName" :value="itemR.roleId" />
          </el-select>
        </el-form-item>
        <el-form-item label="进入项目日期" prop="mBdate">
          <el-date-picker clearable v-model="form.mBdate" type="date" value-format="yyyy-MM-dd" placeholder="请选择进入项目日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="撤出项目日期" prop="mEdate">
          <el-date-picker clearable v-model="form.mEdate" type="date" value-format="yyyy-MM-dd" placeholder="请选择撤出项目日期">
          </el-date-picker>
        </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>

    <el-dialog title="选择用户" :visible.sync="showUserDialog" width="500" append-to-body>
      <el-form :model="queryUserParams" ref="queryUserForm" size="small" :inline="true" v-show="showSearch"
        label-width="68px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="queryUserParams.userName" placeholder="请输入用户名" clearable />
        </el-form-item>
        <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>
      </el-form-item>
      </el-form>

      <el-table :data="userList" style="width: 100%" @row-click="selectUser">
        <el-table-column prop="userId" label="用户ID"></el-table-column>
        <el-table-column prop="userName" label="用户名"></el-table-column>
        <el-table-column prop="userTypeName" label="用户类型"></el-table-column>
      </el-table>
      <div class="pagination-container" style="margin-top: 15px">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="queryUserParams.pageNum" :page-sizes="[10, 20, 30, 50]" :page-size="queryUserParams.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="userTotal"></el-pagination>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { listStaff, getStaff, delStaff, addStaff, updateStaff, projectList, userList, roleList } from "@/api/system/staff";

export default {
  name: "Staff",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目成员表格数据
      staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectPid: null,
        userId: null,
        roleId: null,
        mBdate: null,
        mEdate: null,
      },
      queryUserParams: {
        pageNum: 1,
        pageSize: 10,
        username: null
      },
      projectList: [],
      userList: [],
      roleList: [],
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        projectPid: [
          { required: true, message: '项目不能为空', trigger: 'red' }
        ],
        userId: [
          { required: true, message: '成员不能为空', trigger: 'red' }
        ],
      },
      showUserDialog: false,
      userTotal: 0,
    };
  },
  created() {
    this.getList();
    this.getProjectList();
    this.getUserList();
    this.getRoleList();
  },
  methods: {

    handleFocusUserInput() {
      this.showUserDialog = true;
      this.getUserList();
      this.getList();
    },

    // 选择用户
    selectUser(row) {
      this.form.approver = '';
      this.form.userId = row.userId;
      this.form.userName = row.userName;
      this.userList.push({ userName: row.userName, userId: row.userId });
      this.showUserDialog = false;
      this.userList = [];
    },

    // 获取用户列表
    getUserList() {
      userList(this.queryUserParams).then(response => {
        console.log(response, 'queryUserParams');
        this.userList = response.rows;
        this.userTotal = response.total;
      });
    },

    // 分页大小变化
    handleSizeChange(val) {
      this.queryUserParams.pageSize = val;
      this.getUserList();
    },

    // 当前页变化
    handleCurrentChange(val) {
      this.queryUserParams.pageNum = val;
      this.getUserList();
    },


    /** 查询项目成员列表 */
    getList() {
      this.loading = true;
      listStaff(this.queryParams).then(response => {
        this.staffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectPid: null,
        userId: null,
        role: null,
        mBdate: null,
        mEdate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
      this.resetForm("queryUserForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    resetUserQuery() {
      this.resetForm("queryUserParams");
      this.handleUserQuery();
    },

    handleUserQuery() {
      this.queryParams.pageNum = 1;
      this.getUserList();
    },

    // 多选框选中数据
    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, 'row');
      this.reset();
      const id = row.id || this.ids
      this.queryUserParams.userId = row.userId;
      this.getUserList();
      getStaff(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目成员";
      });
      this.queryUserParams.userId = '';
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStaff(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStaff(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除项目成员编号为"' + ids + '"的数据项？').then(function () {
        return delStaff(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/staff/export', {
        ...this.queryParams
      }, `staff_${new Date().getTime()}.xlsx`)
    },

    /**获取项目列表 */
    getProjectList() {
      projectList().then(response => {
        this.projectList = response.rows;
      });
    },

    /** 获取用户列表 */
    // getUserList() {
    //   userList().then(response => {
    //     this.userList = response.rows;
    //   });
    // },

    getRoleList() {
      roleList().then(response => {
        this.roleList = response.rows;
      });
    },
  }
};
</script>
