<template>
  <div class="app-container" style="padding: 0;">
    <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="['business:repairFormProjectDispatch:add']"
        >选择人员</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table v-loading="loading" :data="repairFormProjectDispatchList" @selection-change="handleSelectionChange">
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="班组名称" align="center" prop="groupName" />
      <el-table-column label="人员工号" align="center" prop="maintainJobNumber" />
      <el-table-column label="人员名称" align="center" prop="maintainUserName" />
      <el-table-column label="岗位名称" align="center" prop="postName" />
<!--      <el-table-column label="在岗状态" align="center" prop="state" />-->
      <el-table-column label="分配工时" align="center" prop="dispatchManHour" />
      <el-table-column label="扣罚工时" align="center" prop="deductManHour">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.deductManHour" @change="submitForm(scope.row)" controls-position="right" size="mini" :min="0" :max="Number(scope.row.dispatchManHour)" :precision="1" style="width: 100%" />
        </template>
      </el-table-column>
      <el-table-column label="实际工时" align="center" prop="actualManHour">
        <template slot-scope="scope">
          {{scope.row.actualManHour = Number(scope.row.dispatchManHour) - Number(scope.row.deductManHour).toFixed(1)}}
        </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:repairFormProjectDispatch:edit']"-->
<!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:repairFormProjectDispatch: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="60%" append-to-body>
      <user ref="user" :data="data" :userIds="repairFormProjectDispatchList.map(item => item.id)"/>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="userSubmitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listRepairFormProjectDispatch,
  getRepairFormProjectDispatch,
  delRepairFormProjectDispatch,
  addRepairFormProjectDispatch,
  updateRepairFormProjectDispatch,
  addRepairFormProjectDispatchP
} from '@/api/business/repairFormProjectDispatch'

import user from "../user/index"
import item from '@/layout/components/Sidebar/Item.vue'

export default {
  name: "RepairFormProjectDispatch",
  components: {
    user
  },
  props:{
    repairFormProjectId: {},
    data: {
      type: Object,
      default: {}
    }
  },
  watch: {
    data(){
      this.getList()
    }
  },
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修单-报修项目-派工表格数据
      repairFormProjectDispatchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairFormId: undefined,
        repairFormProjectId: undefined,
        deptId: undefined,
        deptName: undefined,
        groupId: undefined,
        groupName: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        repairFormId: [
          { required: true, message: "报修单ID不能为空", trigger: "blur" }
        ],
        repairFormProjectId: [
          { required: true, message: "报修单项目ID不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门ID不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "部门名称不能为空", trigger: "blur" }
        ],
        groupId: [
          { required: true, message: "班组ID不能为空", trigger: "blur" }
        ],
        groupName: [
          { required: true, message: "班组名称不能为空", trigger: "blur" }
        ],
        maintainUserId: [
          { required: true, message: "维修人员ID不能为空", trigger: "blur" }
        ],
        maintainUserName: [
          { required: true, message: "维修人员名称不能为空", trigger: "blur" }
        ],
        maintainUserState: [
          { required: true, message: "维修人员状态不能为空", trigger: "blur" }
        ],
        postId: [
          { required: true, message: "岗位ID不能为空", trigger: "blur" }
        ],
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" }
        ],
        dispatchManHour: [
          { required: true, message: "分配工时不能为空", trigger: "blur" }
        ],
        deductManHour: [
          { required: true, message: "扣罚工时不能为空", trigger: "blur" }
        ],
        actualManHour: [
          { required: true, message: "实际工时不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询报修单-报修项目-派工列表 */
    getList() {
      this.loading = true;
      listRepairFormProjectDispatch({ ...this.queryParams, repairFormProjectId: this.data.repairFormProjectId }).then(response => {
        this.repairFormProjectDispatchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        repairFormId: undefined,
        repairFormProjectId: undefined,
        deptId: undefined,
        deptName: undefined,
        groupId: undefined,
        groupName: undefined,
        maintainUserId: undefined,
        maintainUserName: undefined,
        maintainUserState: undefined,
        postId: undefined,
        postName: undefined,
        dispatchManHour: undefined,
        deductManHour: undefined,
        actualManHour: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      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.loading = true;
      this.reset();
      const id = row.id || this.ids
      getRepairFormProjectDispatch(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改报修单-报修项目-派工";
      });
    },
    /** 提交按钮 */
    submitForm(row) {
      this.buttonLoading = true;
      updateRepairFormProjectDispatch(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      }).finally(() => {
        this.buttonLoading = false;
      });
    },
    /** 提交按钮 */
    userSubmitForm() {
      if(this.$refs.user.selectList){
        this.buttonLoading = true;
        addRepairFormProjectDispatchP({ projectId: this.data.repairFormProjectId, dispatchIdList: this.$refs.user.selectList.map(item => item.id) }).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open1 = false;
          this.getList();
        }).finally(() => {
          this.buttonLoading = false;
        });
      }
      this.open = false
      this.$refs.user.selectList = []
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报修单-报修项目-派工编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delRepairFormProjectDispatch(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/repairFormProjectDispatch/export', {
        ...this.queryParams
      }, `repairFormProjectDispatch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
