<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="search" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.caseId" placeholder="事件编号" size="small" clearable/>
      </search-item>
      <search-item/>
      <search-item>
        <el-input v-model.trim="listQuery.title" placeholder="事件标题" size="small" clearable/>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="timeRange"
          type="datetimerange"
          size="small"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="请示开始时间"
          end-placeholder="请示结束时间"/>
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage">
      <template slot="columns">
        <el-table-column label="操作" align="center" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="goDetail(scope.row)">详情</el-button>
            <el-button type="text" size="small" @click.stop="processReport(scope.row)">处理</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>

    <el-dialog :visible.sync="processDialogVisible" :close-on-click-modal="false" title="处理请示" append-to-body @close="cancelProcess">
      <el-form ref="reportForm" :model="reportForm" label-width="auto">
        <el-row>
          <el-form-item label="处理方式">
            <el-radio-group v-model="reportForm.processType" @change="selectProcessType">
              <el-radio v-for="item in processTypeList" :key="item.value" :label="item.value">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-row>
        <el-row >
          <el-col v-if="showDeptSelect" :span="12">
            <el-form-item label="请示处理部门">
              <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 v-if="showUserSelect" :span="12">
            <el-form-item label="请示处理人">
              <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-row>
          <el-form-item label="处理结果">
            <el-input v-model="reportForm.processContent" type="textarea" maxlength="50" show-word-limit/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelProcess">取消</el-button>
        <el-button type="primary" @click="submitProcess">确定</el-button>
      </div>
    </el-dialog>
  </app-container>
</template>

<script>
import NormalTable from '@/components/NomalTable'
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import { getDeptList, getUserList } from '@/api/callCase'
import { getReportType } from '@/api/allDict'
import { workReportList, processReport } from '@/api/caseReport'
export default {
  name: 'WorkReportList',
  components: { SearchItem, SearchArea, AppContainer, NormalTable },
  data() {
    return {
      listQuery: {
        caseId: '', // 事件编号
        title: '', // 事件标题
        startTime: '',
        endTime: '',
        offset: 1,
        limit: 20,
        sort: 'reportTime',
        order: 'desc'
      }, // 筛选条件
      timeRange: [],
      columns: [
        {
          text: '事件标题',
          value: 'title',
          align: 'center'
        },
        {
          text: '事件状态',
          value: 'stateName',
          align: 'center'
        },
        {
          text: '请示人',
          value: 'reportUserName',
          align: 'center'
        },
        {
          text: '请示内容',
          value: 'reportContent',
          align: 'center'
        },
        {
          text: '请示时间',
          value: 'reportTime',
          align: 'center'
        },
        {
          text: '处理人',
          value: 'processUserName',
          align: 'center'
        },
        {
          text: '处理状态',
          value: 'processStateName',
          align: 'center'
        },
        {
          text: '处理方式',
          value: 'processTypeName',
          align: 'center'
        },
        {
          text: '处理时间',
          value: 'processTime',
          align: 'center'
        },
        {
          text: '处理结果',
          value: 'processContent',
          align: 'center'
        },
        {
          text: '限办日期',
          value: 'limitedTime',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      tableOption: {
        head: {
          show: false, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: false // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      processTypeList: [],
      reportForm: {
        id: '',
        processType: '',
        processContent: '',
        processDept: '',
        processUser: ''
      },
      processDialogVisible: false,
      showDeptSelect: false,
      showUserSelect: false,
      reportDeptList: [],
      reportUserList: [],
      deptSelectLoading: false,
      userSelectLoading: false,
      userQuery: {
        deptid: '', // 部门id （默认为当前用户部门）
        hasMine: '', // 是否包含本人，1包含, 0不包含
        roleTips: '' // 角色标识，这里默认noraml
      }
    }
  },
  watch: {
    timeRange(val) {
      if (val && val.length > 0) {
        this.listQuery.startTime = val[0]
        this.listQuery.endTime = val[1]
      } else {
        this.listQuery.startTime = ''
        this.listQuery.endTime = ''
      }
    }
  },
  created() {
    this.fetchData()
    this.fetchProcessTypeList()
    this.fetchReportDeptList()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      workReportList(this.listQuery).then(res => {
        this.list = res.data.rows
        this.total = res.data.total
        this.listLoading = false
      })
    },
    search() {
      this.listQuery.offset = 1
      this.fetchData()
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery.caseId = ''
      this.listQuery.title = ''
      this.timeRange = []
    },
    // 查看详情
    goDetail(row) {
      this.$router.push({
        path: '/caseDetail/' + row.caseId,
        query: {
          showProcess: false
        }
      })
    },
    processReport(row) {
      this.processDialogVisible = true
      this.reportForm.id = row.id
    },
    fetchProcessTypeList() {
      getReportType().then(res => {
        this.processTypeList = res.data
      })
    },
    // 获取部门列表
    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()
      }
    },
    cancelProcess() {
      this.processDialogVisible = false
      this.reportForm = {
        id: '',
        processType: '',
        processContent: '',
        processDept: '',
        processUser: ''
      }
    },
    selectProcessType() {
      if (this.reportForm.processType === '3') {
        this.showDeptSelect = true
        this.showUserSelect = true
      } else {
        this.showDeptSelect = false
        this.showUserSelect = false
        this.reportForm.processDept = ''
        this.reportForm.processUser = ''
      }
    },
    submitProcess() {
      processReport(this.reportForm).then(res => {
        this.$message.success('处理成功')
        this.cancelProcess()
        this.fetchData()
      })
    }

  }
}
</script>

<style scoped>

</style>

