<!--待办事件-->
<template>
  <app-container>
    <case-search :list-query="listQuery" @search="search"/>
    <case-list-table :list-query="listQuery" :list="list" :total="total" :list-loading="listLoading" @changePage="changePage">
      <template slot="operations">
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="processCase(scope.row)">任务办理</el-button>
            <el-button v-if="hasPerm('/applyDelay')" type="text" size="small" @click.stop="applyDelay(scope.row)">申请延期</el-button>
            <el-button v-if="hasPerm('/applyReport')" type="text" size="small" @click.stop="applyReport(scope.row)">上报请示</el-button>
          </template>
        </el-table-column>
      </template>
    </case-list-table>

    <el-dialog :visible.sync="delayDialogVisible" :close-on-click-modal="false" title="申请延期" append-to-body @close="cancelDelay">
      <el-form ref="delayForm" :model="delayForm" :rules="delayRules" label-width="auto">
        <el-form-item label="延期时间" prop="delayTime">
          <el-date-picker v-model="delayForm.delayTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择延期时间"/>
        </el-form-item>
        <el-form-item label="申请理由" prop="applyReason">
          <el-input v-model="delayForm.applyReason" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDelay()">取消</el-button>
        <el-button type="primary" @click="submitDelay()">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="reportDialogVisible" :close-on-click-modal="false" title="上报请示" append-to-body @close="cancelReport">
      <el-form ref="reportForm" :model="reportForm" :rules="reportRules" label-width="auto">
        <el-row >
          <el-col :span="12">
            <el-form-item label="上报处理部门" prop="processDept">
              <el-select v-model="reportForm.processDept" :loading="deptSelectLoading" filterable placeholder="请选择处理部门" @change="selectDept">
                <el-option v-for="item in reportDeptList" :key="item.id" :label="item.simplename" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上报处理人" prop="processUser">
              <el-select v-model="reportForm.processUser" :loading="userSelectLoading" filterable placeholder="请选择处理人">
                <el-option v-for="item in reportUserList" :key="item.id" :label="item.name" :value="item.id"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="请示说明" prop="reportContent">
          <el-input v-model="reportForm.reportContent" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelReport">取消</el-button>
        <el-button type="primary" @click="submitReport">确定</el-button>
      </div>
    </el-dialog>
  </app-container>
</template>

<script>
import AppContainer from '@/components/layout/AppContainer'
import CaseSearch from './caseCommon/caseSearch'
import CaseListTable from './caseCommon/caseListTable'
import { workList, getDeptList, getUserList } from '@/api/callCase'
import { applyDelay } from '@/api/caseDelay'
import { addReport } from '@/api/caseReport'

export default {
  name: 'WaitForHandle',
  components: { CaseSearch, CaseListTable, AppContainer },
  data() {
    return {
      listQuery: {
        caseId: '', // 事件编号
        title: '', // 事件标题
        description: '', // 事件内容
        reporterPhone: '', // 联系方式
        reporterName: '', // 联系人
        startTime: '', // 来电开始时间
        endTime: '', // 来电结束时间
        caseState: '', // 处理方式
        state: '', // 处理状态
        isDelay: '', // 事件状态
        source: '', // 事件来源
        caseLevel: '', // 紧急程度
        offset: 1,
        limit: 20,
        sort: 'createTime',
        order: 'desc'
      }, // 筛选条件
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      delayDialogVisible: false, // 申请延期对话框
      delayForm: {
        caseId: '',
        delayTime: '',
        applyReason: ''
      },
      delayRules: {
        delayTime: [{ required: true, message: '请选择延期时间', trigger: ['change', 'blur'] }],
        applyReason: [{ required: true, message: '请填写申请理由', trigger: ['change', 'blur'] }]
      },
      reportDialogVisible: false, // 上报请示对话框
      reportForm: {
        caseId: '',
        processDept: '',
        processUser: '',
        reportContent: ''
      },
      reportRules: {
        processDept: [{ required: true, message: '请选择上报处理部门', trigger: ['change', 'blur'] }],
        processUser: [{ required: true, message: '请选择上报处理人', trigger: ['change', 'blur'] }],
        reportContent: [{ required: true, message: '请填写请示说明', trigger: ['change', 'blur'] }]
      },
      reportDeptList: [],
      deptSelectLoading: false,
      reportUserList: [],
      userSelectLoading: false,
      userQuery: {
        deptid: '', // 部门id （默认为当前用户部门）
        hasMine: '', // 是否包含本人，1包含, 0不包含
        roleTips: '' // 角色标识，这里默认noraml
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchReportDeptList()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      workList(this.listQuery).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      })
    },
    search(listQuery) {
      console.log('search')
      this.listQuery.offset = 1
      this.fetchData()
    },
    changePage(listQuery) {
      console.log('changePage')
      this.fetchData()
    },
    // 任务办理
    processCase(row) {
      this.$router.push({
        path: '/caseDetail/' + row.id,
        query: {
          taskId: row.taskId,
          showProcess: true
        }
      })
    },
    // 申请延期
    applyDelay(row) {
      this.delayDialogVisible = true
      this.delayForm.caseId = row.id
    },
    resetDeleyForm() {
      this.$refs['delayForm'].resetFields()
      this.delayForm = {
        id: '',
        delayTime: '',
        applyReason: ''
      }
    },
    cancelDelay() {
      this.delayDialogVisible = false
      this.resetDeleyForm()
    },
    submitDelay() {
      this.$refs['delayForm'].validate((valid) => {
        if (valid) {
          applyDelay(this.delayForm).then(res => {
            this.$message.success('申请延期成功')
            this.cancelDelay()
          }).catch((res) => {
            this.cancelDelay()
          })
        }
      })
    },
    // 上报请示
    applyReport(row) {
      this.reportDialogVisible = true
      this.reportForm.caseId = row.id
    },
    resetReportForm() {
      this.$refs['reportForm'].resetFields()
      this.reportForm = {
        caseId: '',
        processDept: '',
        processUser: '',
        reportContent: ''
      }
    },
    cancelReport() {
      this.reportDialogVisible = false
      this.resetReportForm()
    },
    submitReport() {
      this.$refs['reportForm'].validate((valid) => {
        if (valid) {
          addReport(this.reportForm).then(res => {
            this.$message.success('上报请示成功')
            this.cancelReport()
          }).catch((res) => {
            this.cancelReport()
          })
        }
      })
    },
    // 获取部门列表
    fetchReportDeptList() {
      this.deptSelectLoading = true
      getDeptList().then(response => {
        this.reportDeptList = response.data
        this.deptSelectLoading = false
      })
    },
    // 获取人员列表
    fetchReportUserList() {
      this.userSelectLoading = true
      getUserList(this.userQuery).then(response => {
        this.reportUserList = response.data
        this.userSelectLoading = false
      })
    },
    // 选择部门
    selectDept(val) {
      this.reportUserList = []
      this.reportForm.processUser = ''
      if (val) {
        this.userQuery.deptid = val
        this.fetchReportUserList()
      }
    }

  }
}
</script>

<style scoped>

</style>
