<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="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入模块名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="queryParams.projectId" placeholder="请选择所属项目" clearable>
          <el-option
            v-for="item in projectOptions"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接取人" prop="assignee">
        <el-input
          v-model="queryParams.assignee"
          placeholder="请输入接取人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="接取状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="待接取" value="0" />
          <el-option label="已接取" value="1" />
          <el-option label="已完成" value="2" />
        </el-select>
      </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-table
      v-loading="loading"
      :data="moduleList"
      @selection-change="handleSelectionChange"
      @row-click="handleRowClick"
      @cell-click="handleCellClick"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模块名称" align="center" prop="moduleName">
        <template slot-scope="scope">
          <el-link type="primary" @click.stop="openSubDrawer(scope.row)">{{ scope.row.moduleName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="所属项目" align="center" prop="projectName" />
      <el-table-column label="接取状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === '0' ? 'info' : scope.row.status === '1' ? 'warning' : 'success'"
            disable-transitions
          >
            {{ {0: '待接取', 1: '已接取', 2: '已完成'}[scope.row.status] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="接取人" align="center" prop="assignee" />
      <el-table-column label="接取时间" align="center" prop="assignTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.assignTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </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="['project:module:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['project:module:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-position"
            @click="handleAssign(scope.row)"
            v-hasPermi="['project:module:assign']"
          >指派</el-button>
          <el-button
            v-if="scope.row.status === '0'"
            size="mini"
            type="text"
            icon="el-icon-thumb"
            @click="handleClaim(scope.row)"
            v-hasPermi="['project:module:claim']"
          >接取</el-button>
          <el-button
            v-if="scope.row.status === '1' && scope.row.assignee === userName"
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleGiveup(scope.row)"
            v-hasPermi="['project:module:giveup']"
          >放弃</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="80px">
        <el-form-item label="模块名称" prop="moduleName">
          <el-input v-model="form.moduleName" placeholder="请输入模块名称" />
        </el-form-item>
        <el-form-item label="所属项目" prop="projectId">
          <el-select v-model="form.projectId" placeholder="请选择所属项目">
            <el-option
              v-for="item in projectOptions"
              :key="item.projectId"
              :label="item.projectName"
              :value="item.projectId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="模块描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="接取人" prop="assignee" v-if="form.moduleId">
          <el-input v-model="form.assignee" 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>

    <!-- 指派模块对话框 -->
    <el-dialog title="指派模块" :visible.sync="assignOpen" width="500px" append-to-body>
      <el-form ref="assignForm" :model="assignForm" label-width="80px">
        <el-form-item label="模块名称">
          <el-input v-model="assignForm.moduleName" :disabled="true" />
        </el-form-item>
        <el-form-item label="指派给" prop="assignee">
          <el-select v-model="assignForm.assignee" placeholder="请选择用户" filterable>
            <el-option
              v-for="item in userOptions"
              :key="item.userName"
              :label="item.nickName"
              :value="item.userName"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitAssign">确 定</el-button>
        <el-button @click="assignOpen = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listModule, getModule, delModule, addModule, updateModule, assignModule, claimModule, giveupModule } from "@/api/project/module"
import { listProject } from "@/api/project/project"
import { listUser } from "@/api/system/user"
import { listSubmoduleByModule, addSubmodule } from "@/api/project/submodule"

export default {
  name: "Module",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模块表格数据
      moduleList: [],
      // 项目选项
      projectOptions: [],
      // 用户选项
      userOptions: [],
      // 当前用户名
      userName: this.$store.state.user.name,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
     
      currentModule: null,
      // 新增子模块弹窗
      subOpen: false,
      subTitle: "新增子模块",
      subForm: {
        subName: null,
        description: null
      },
      subRules: {
        subName: [{ required: true, message: "子模块名称不能为空", trigger: "blur" }]
      },
      // 指派弹出层
      assignOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moduleName: null,
        projectId: null,
        assignee: null,
        status: null,
      },
      // 指派表单
      assignForm: {
        moduleId: null,
        moduleName: null,
        assignee: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moduleName: [
          { required: true, message: "模块名称不能为空", trigger: "blur" }
        ],
        projectId: [
          { required: true, message: "所属项目不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getProjectList();
    this.getUserList();
  },
  methods: {
    /** 查询模块列表 */
    getList() {
      this.loading = true;
      listModule(this.queryParams).then(response => {
        this.moduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取项目列表 */
    getProjectList() {
      listProject().then(response => {
        this.projectOptions = response.rows;
      });
    },
    /** 获取用户列表 */
    getUserList() {
      listUser({ pageNum: 1, pageSize: 100 }).then(response => {
        this.userOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moduleId: null,
        moduleName: null,
        projectId: null,
        description: null,
        status: "0",
        assignee: null,
        assignTime: null,
        finishTime: 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.moduleId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模块";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const moduleId = row.moduleId || this.ids;
      getModule(moduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模块";
      });
    },
    /** 指派按钮操作 */
    handleAssign(row) {
      this.assignForm = {
        moduleId: row.moduleId,
        moduleName: row.moduleName,
        assignee: null
      };
      this.assignOpen = true;
    },
    /** 接取按钮操作 */
    handleClaim(row) {
      this.$modal.confirm('确认接取模块"' + row.moduleName + '"？').then(() => {
        return claimModule(row.moduleId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("接取成功");
      });
    },
    /** 放弃按钮操作 */
    handleGiveup(row) {
      this.$modal.confirm('确认放弃模块"' + row.moduleName + '"？').then(() => {
        return giveupModule(row.moduleId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("放弃成功");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.moduleId != null) {
            updateModule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交指派 */
    submitAssign() {
      if (!this.assignForm.assignee) {
        this.$modal.msgError("请选择要指派的用户");
        return;
      }
      assignModule(this.assignForm).then(response => {
        this.$modal.msgSuccess("指派成功");
        this.assignOpen = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const moduleIds = row.moduleId || this.ids;
      this.$modal.confirm('是否确认删除模块编号为"' + moduleIds + '"的数据项？').then(function() {
        return delModule(moduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('project/module/export', {
        ...this.queryParams
      }, `module_${new Date().getTime()}.xlsx`);
    }
  }
};
</script>
