<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.caseId" placeholder="事件编号" clearable/>
      </search-item>
      <search-item/>
      <search-item>
        <el-input v-model.trim="listQuery.title" placeholder="事件标题" clearable/>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.description" placeholder="事件内容" clearable/>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.reporterPhone" placeholder="联系方式" clearable/>
      </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="160">
          <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="approve(scope.row)">同意</el-button>
            <el-button type="text" size="small" @click.stop="reject(scope.row)">拒绝</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <el-dialog :visible.sync="rejectDialogVisible" :close-on-click-modal="false" title="拒绝延期" append-to-body @close="cancelReject">
      <el-form ref="rejectForm" :model="rejectForm" :rules="rejectRules" label-width="auto">
        <el-form-item label="驳回理由" prop="rejectReason">
          <el-input v-model="rejectForm.rejectReason" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelReject">取消</el-button>
        <el-button type="primary" @click="submitReject">确定</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 { applyList, approveDelay, rejectDelay } from '@/api/caseDelay'
export default {
  name: 'DelayApplyList',
  components: { SearchItem, SearchArea, AppContainer, NormalTable },
  data() {
    return {
      listQuery: {
        caseId: '', // 事件编号
        title: '', // 事件标题
        description: '', // 事件内容
        reporterPhone: '', // 联系方式
        offset: 1,
        limit: 20,
        sort: 'applyTime',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '事件标题',
          value: 'title',
          align: 'center'
        },
        {
          text: '延期时间',
          value: 'delayTime',
          align: 'center'
        },
        {
          text: '申请说明',
          value: 'applyReason',
          align: 'center'
        },
        {
          text: '申请人',
          value: 'applyUserName',
          align: 'center'
        },
        {
          text: '申请时间',
          value: 'applyTime',
          align: 'center'
        },
        {
          text: '状态',
          value: 'applyStateName',
          align: 'center'
        },
        {
          text: '审核人',
          value: 'approveUserName',
          align: 'center'
        },
        {
          text: '审核时间',
          value: 'approveTime',
          align: 'center'
        },
        {
          text: '审核驳回理由',
          value: 'rejectReason',
          align: 'center'
        }
      ], // 显示列
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      tableOption: {
        head: {
          show: false, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: false // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      rejectDialogVisible: false,
      rejectForm: {
        id: '',
        rejectReason: ''
      },
      rejectRules: {
        rejectReason: [{ required: true, message: '请填写驳回理由', trigger: ['change', 'blur'] }]
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      applyList(this.listQuery).then(res => {
        this.list = res.data.rows
        this.total = res.total
        this.listLoading = false
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    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: '', // 事件编号
        title: '', // 事件标题
        description: '', // 事件内容
        reporterPhone: '', // 联系方式
        offset: 1,
        limit: 20,
        sort: 'applyTime',
        order: 'desc'
      }
      this.$message.success('clearInput')
    },
    // 查看详情
    goDetail(row) {
      this.$router.push({
        path: '/caseDetail/' + row.caseId,
        query: {
          showProcess: false
        }
      })
    },
    approve(row) {
      this.$confirm('确认同意延期吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        approveDelay(row.id).then(res => {
          this.$message.success('已确认')
          this.fetchData()
        })
      })
    },
    reject(row) {
      this.$confirm('确认拒绝延期吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.rejectForm.id = row.id
        this.rejectDialogVisible = true
      })
    },
    resetRejectForm() {
      this.$refs['rejectForm'].resetFields()
      this.rejectForm = {
        id: '',
        rejectReason: ''
      }
    },
    cancelReject() {
      this.rejectDialogVisible = false
      this.resetRejectForm()
    },
    submitReject() {
      rejectDelay(this.rejectForm).then(res => {
        this.$message.success('已拒绝')
        this.cancelReject()
        this.fetchData()
      })
    }
  }
}
</script>

<style scoped>

</style>
