 <!-- 待办督办 -->
<template>
  <app-container>
    <search-area :need-clear="false" :need-search-more="false" :size="size" type="default" search-more-type="default" @search="search">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.title" :size="size" placeholder="事件标题" clearable/>
      </search-item>
    </search-area>
    <case-list-table :list-query="listQuery" :list="list" :total="total" :columns="columns" :list-loading="listLoading" @changePage="changePage">
      <template slot="operations">
        <el-table-column label="操作" align="center">
          <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="monitor(scope.row)">监察</el-button>
            <el-button type="text" size="small" @click.stop="supervise(scope.row)">督办</el-button>
            <el-button type="text" size="small" @click.stop="cancelSupervise(scope.row)">取消督办</el-button>
          </template>
        </el-table-column>
      </template>
    </case-list-table>
    <el-dialog :visible.sync="superviseDialogVisible" :close-on-click-modal="false" title="督办" append-to-body>
      <el-form ref="superviseForm" :model="superviseForm" :rules="superviseRules" label-width="auto">
        <el-form-item label="督办结果" prop="urgeResult">
          <el-input v-model="superviseForm.urgeResult" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelDialog">取消</el-button>
        <el-button type="primary" @click="submitSupervise">确定</el-button>
      </div>
    </el-dialog>
  </app-container>
</template>

<script>
import AppContainer from '@/components/layout/AppContainer'
import SearchArea from '@/components/SearchArea/SearchArea'
import SearchItem from '@/components/SearchArea/SearchItem'
import CaseListTable from '@/views/caseManage/caseCommon/caseListTable'
// import { getSuperviseList } from '@/api/callCase'
import { supervise, getSuperviseList } from '@/api/caseSupervise'
import { addMonitor } from '@/api/caseMonitor'

export default {
  name: 'WaitForSupervise',
  components: { AppContainer, SearchArea, SearchItem, CaseListTable },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true, // 列表加载动画
      listQuery: {
        title: '', // 事件标题
        offset: 1,
        limit: 20,
        sort: '',
        order: ''
      }, // 筛选条件
      columns: [
        {
          text: '事件标题',
          value: 'title',
          align: 'center'
        },
        {
          text: '联系人',
          value: 'reporterName',
          align: 'center'
        },
        {
          text: '联系方式',
          value: 'reporterPhone',
          align: 'center'
        },
        {
          text: '处理状态',
          value: 'caseStatus',
          align: 'center'
        },
        {
          text: '督办类型',
          value: 'urgeType',
          align: 'center'
        },
        {
          text: '创建时间',
          value: 'createTime',
          width: 90,
          align: 'center'
        },
        {
          text: '督办内容',
          value: 'urgeContent',
          align: 'center'
        },
        {
          text: '督办状态',
          value: 'urgeStatus',
          align: 'center'
        }
      ], // 显示列
      superviseDialogVisible: false,
      superviseForm: {
        id: '',
        urgeStatus: '1',
        urgeResult: ''
      },
      superviseRules: {
        urgeResult: [{ required: true, message: '请填写督办结果', trigger: ['change', 'blur'] }]
      },
      size: 'small'
    }
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    search() {
      this.fetchData(false)
    },
    // 获取数据
    fetchData(isNowPage = true) {
      this.listLoading = true
      if (!isNowPage) { // 是否显示当前页，否则跳转第一页
        this.listQuery.offset = 1
      }
      this.listLoading = true
      getSuperviseList(this.listQuery).then(response => {
        if (response.code === 200) {
          // response.data.rows = [
          //   {
          //     'title': '事件1',
          //     'reporterName': '张三',
          //     'reporterPhone': '15652360420',
          //     'caseStatus': '处理中',
          //     'urgeContent': '督办内容',
          //     'urgeType': '申请督办',
          //     'eorcName': '咨询',
          //     'caseTypeName': '咨询',
          //     'caseDetailTypeName': '垃圾处理',
          //     'createUserName': '王',
          //     'limitedTime': '2020-04-12 00:00:00',
          //     'createTime': '2020-03-12 00:00:00',
          //     'urgeStatus': '未督办',
          //     'urgeTime': '-',
          //     'urgeResult': '-'
          //   }
          // ]
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
        }
      })
    },
    // 任务办理
    goDetail(row) {
      this.$router.push({
        path: '/caseDetail/' + row.caseId,
        query: {
          showProcess: false
        }
      })
    },
    // 督办
    supervise(row) {
      this.superviseForm.id = row.id
      this.superviseDialogVisible = true
    },
    // 监察
    monitor(row) {
      this.$prompt('', '说明', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputErrorMessage: '输入不能为空',
        inputValidator: (value) => {
          if (value) {
            if (value.trim().length < 1) {
              return '输入不能为空'
            } else if (value.trim().length > 100) {
              return '输入字数不能超过100'
            }
          } else {
            return '输入不能为空'
          }
        }
      }).then(({ value }) => {
        if (value) {
          const params = {
            caseId: row.caseId,
            urgeId: row.id,
            superviseContent: value.trim()
          }
          addMonitor(params).then(response => {
            if (response.code === 200) {
              this.$message({
                type: 'success',
                message: '申请监察成功！'
              })
              this.fetchData()
            }
          })
        }
      }).catch(() => {
      })
    },
    // 更换页码等
    changePage(listQuery) {
      this.listQuery = listQuery
      this.fetchData()
    },
    // 取消督办
    cancelSupervise(row) {
      const params = {
        id: row.id,
        urgeStatus: '2'
      }
      supervise(params).then(response => {
        if (response.code === 200) {
          this.$message.success('取消成功')
        }
      })
    },
    // 督办
    submitSupervise() {
      this.$refs['superviseForm'].validate((valid) => {
        if (valid) {
          supervise(this.superviseForm).then(res => {
            this.$message.success('督办成功')
            this.fetchData()
            this.cancelDialog()
          }).catch((res) => {
            this.cancelDialog()
          })
        }
      })
    },
    // 取消弹窗
    cancelDialog() {
      this.superviseDialogVisible = false
      this.resetSuperviseForm()
    },
    // 重置
    resetSuperviseForm() {
      this.$refs['superviseForm'].resetFields()
      this.superviseForm = {
        id: '',
        urgeStatus: '1',
        urgeResult: ''
      }
    }
  }
}
</script>
