<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="派工单号" prop="planCode">
        <el-input
          v-model="queryParams.planCode"
          placeholder="请输入派工单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工单编号" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
          placeholder="请输入工单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行状态" prop="executionStatus">
        <el-select v-model="queryParams.executionStatus" placeholder="请选择执行状态" clearable>
          <el-option
            v-for="dict in dict.type.dms_execution_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="接收状态" prop="receiveStatus">
        <el-select v-model="queryParams.receiveStatus" placeholder="请选择接收状态" clearable>
          <el-option
            v-for="dict in dict.type.dms_receive_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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-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="['dms:dispatchPlan:add']"
        >新增派工</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="['dms:dispatchPlan: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="['dms:dispatchPlan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dispatchPlanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="派工单号" align="center" prop="planCode" width="160" />
      <el-table-column label="工单编号" align="center" prop="workOrderCode" width="160" />
      <el-table-column label="设备名称" align="center" prop="deviceName" show-overflow-tooltip />
      <el-table-column label="派工类型" align="center" prop="dispatchType" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_dispatch_type" :value="scope.row.dispatchType"/>
        </template>
      </el-table-column>
      <el-table-column label="执行人/团队" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.dispatchType === '1'">{{ scope.row.executorName }}</span>
          <span v-else>{{ scope.row.teamName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="优先级" align="center" prop="priority" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_priority" :value="scope.row.priority"/>
        </template>
      </el-table-column>
      <el-table-column label="接收状态" align="center" prop="receiveStatus" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_receive_status" :value="scope.row.receiveStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="执行状态" align="center" prop="executionStatus" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_execution_status" :value="scope.row.executionStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="派工时间" align="center" prop="dispatchTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dispatchTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="要求完成" align="center" prop="requireEndTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.requireEndTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="派工人" align="center" prop="dispatcherName" width="100" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['dms:dispatchPlan:query']"
          >查看</el-button>
          <el-button
            v-if="scope.row.receiveStatus === '0'"
            size="mini"
            type="text"
            @click="handleReceive(scope.row)"
            v-hasPermi="['dms:dispatchPlan:receive']"
          >接收</el-button>
          <el-button
            v-if="scope.row.receiveStatus === '0'"
            size="mini"
            type="text"
            style="color: #F56C6C"
            @click="handleReject(scope.row)"
            v-hasPermi="['dms:dispatchPlan:reject']"
          >拒绝</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="拒绝派工" :visible.sync="rejectDialogVisible" width="500px" append-to-body>
      <el-form ref="rejectForm" :model="rejectForm" label-width="100px">
        <el-form-item label="派工单号">
          <el-input v-model="rejectForm.planCode" disabled />
        </el-form-item>
        <el-form-item label="拒绝原因" prop="rejectReason">
          <el-input v-model="rejectForm.rejectReason" type="textarea" :rows="4" placeholder="请输入拒绝原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="rejectDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitReject">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDispatchPlan, getDispatchPlan, delDispatchPlan, receiveDispatch, rejectDispatch } from '@/api/dms/dispatchPlan'

export default {
  name: 'DispatchPlan',
  dicts: ['dms_dispatch_type', 'dms_execution_status', 'dms_receive_status', 'dms_priority'],
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      dispatchPlanList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planCode: null,
        workOrderCode: null,
        deviceName: null,
        executionStatus: null,
        receiveStatus: null
      },
      rejectDialogVisible: false,
      rejectForm: {
        planId: null,
        planCode: null,
        rejectReason: null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listDispatchPlan(this.queryParams).then(response => {
        this.dispatchPlanList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/dms/dispatchPlan/detail/new')
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.$router.push({
        path: '/dms/dispatchPlan/detail/' + row.planId,
        query: { view: 'true' }
      })
    },
    /** 接收派工 */
    handleReceive(row) {
      this.$modal.confirm('是否确认接收派工单"' + row.planCode + '"？').then(() => {
        return receiveDispatch({ planId: row.planId })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('接收成功')
      }).catch(() => {})
    },
    /** 拒绝派工 */
    handleReject(row) {
      this.rejectForm = {
        planId: row.planId,
        planCode: row.planCode,
        rejectReason: null
      }
      this.rejectDialogVisible = true
    },
    /** 提交拒绝 */
    submitReject() {
      if (!this.rejectForm.rejectReason) {
        this.$message.warning('请输入拒绝原因')
        return
      }
      rejectDispatch(this.rejectForm).then(() => {
        this.$modal.msgSuccess('操作成功')
        this.rejectDialogVisible = false
        this.getList()
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids
      this.$modal.confirm('是否确认删除选中的派工计划？').then(() => {
        return delDispatchPlan(planIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dispatchPlan/export', {
        ...this.queryParams
      }, `dispatchPlan_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
