<template>

  <browser :component-loading="componentLoading"
           :api-config="apiConfig"
           :table-config="tableConfig"
           :permissions="permissions"
           :dialog-config="dialogConfig"
           :actions="actions"
           :custom-query="true"
           @custom-query="handleQuery"
           @update:dialog-config="dialogConfig = $event"
           @update:actions="actions = $event">
    <template #queryPanel="{ queryParams: query }">
      <el-form ref="queryForm" :model="query" label-width="100px">
        <el-row class="query-condition">
          <el-col :span="6">
            <el-form-item label="案件名称" prop="industryType">
              <el-input v-model="query.caseName" clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="立案日期" prop="fillingDate">
              <el-date-picker
                  v-model="query.fillingDate"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="结案日期" prop="caseClosingDate">
              <el-date-picker
                  v-model="query.caseClosingDate"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </template>
    <el-table-column label="案件号" prop="caseNum" min-width="220"></el-table-column>
    <el-table-column label="案件名称" prop="caseName" min-width="300"></el-table-column>
    <el-table-column label="立案日期" prop="fillingDate" min-width="150"></el-table-column>
    <el-table-column label="承办人" prop="caseHandler" min-width="150"></el-table-column>
    <el-table-column label="结案日期" prop="caseClosingDate" min-width="150"></el-table-column>

    <template #dialogContent="{ dialogConfig: dialog }">
      <document-list :dialogConfig="dialog.data" :documents="dialog.documents"></document-list>
    </template>
  </browser>
</template>
<script setup>
import {reactive, toRefs, getCurrentInstance, watch, computed, ref, nextTick} from 'vue'
import browser from '@/components/Browser.vue'
import documentList from '@pages/xzzfgl/caseMan/DocumentList.vue'
import caseApi from '@/api/lawenforcement/Case'
import documentApi from '@/api/lawenforcement/Document.js'
import {ElMessage} from 'element-plus'

const state = reactive({
  componentLoading: false,
  queryParams: {},
  permissions: {
    query: true,
    add: false,
    modify: false,
    detail: false,
    delete: false,
    importFile: false,
    exportFile: false,
    exportSelectFile: false,
    downloadTemp: false,
  },
  tableConfig: {
    multipleSelect: false,
    hasControlColumn: true,
    defaultSort: {
      prop: 'fillingDate',
      order: 'desc'
    },
    controlWidth: '200'
  },
  apiConfig: {
    api: caseApi,
    modelId: 'caseId',
    documentapi: documentApi
  },
  dialogConfig: {
    type: 'min',
    show: false,
    showFooter: false,
    mode: 'detail',
    title: '文书清单',
    loading: false,
    data: {},
    documents: [],
    actions: {
      handleDialogOk: handleDialogOk
    },
  },
  actions: {
    detail: handleDetail
  }
})

const {componentLoading, queryParams, permissions, tableConfig, apiConfig, dialogConfig, actions} = toRefs(state)

function handleQuery(query) {
  tableConfig.value.tableLoading = true
  query.status = "reviewing_done"//查询立案后的所有案卷
  apiConfig.value.api.query(query).then(res => {
    if (res.success) {
      tableConfig.value.tableData = res
      console.log(tableConfig.value.tableData)
      tableConfig.value.tableLoading = false
    }
  })
}

function handleDetail(row) {
  let xh = row.caseNum.slice(-5).substring(0, 3)
  let year = row.caseNum.substring(5, 9)
  let area = ''
  let wsmu = [
    {
      documentNo: 1,
      documentName: '立案审批表',
      documentType: '立案类',
      documentCode: '（西安）应急立〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {

    {
      documentNo: 3,
      documentName: '现场检查记录',
      documentType: '调查取证类',
      documentCode: '（西安）应急现记〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 4,
      documentName: '抽样取证凭证',
      documentType: '调查取证类',
      documentCode: '（西安）应急抽〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 5,
      documentName: '鉴定委托书',
      documentType: '调查取证类',
      documentCode: '（西安）应急鉴〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 6,
      documentName: '现场处理措施决定书',
      documentType: '现场措施类',
      documentCode: '（西安）应急现决〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 7,
      documentName: '责令限期整改指令书',
      documentType: '现场措施类',
      documentCode: '（西安）应急责改〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 8,
      documentName: '先行登记保存证据通知书',
      documentType: '调查取证类',
      documentCode: '（西安）应急先通〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 9,
      documentName: '先行登记保存证据处理决定书',
      documentType: '调查取证类',
      documentCode: '（西安）应急先决〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {documentNo: 10, documentName: '安全生产行政执法证据材料-书证', documentType: '材料类', documentCode: '', documenContent: {}},
    {
      documentNo: 11,
      documentName: '询问通知书',
      documentType: '调查取证类',
      documentCode: '（西安）应急询〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {documentNo: 12, documentName: '授权委托书', documentType: '调查取证类', documentCode: '', documenContent: {}},
    {documentNo: 13, documentName: '受托人身份证复印件', documentType: '材料类', documentCode: '', documenContent: {}},
    {documentNo: 14, documentName: '调查询问笔录', documentType: '调查取证类', documentCode: '', documenContent: {}},
    {documentNo: 15, documentName: '法定代表人身份证明', documentType: '材料类', documentCode: '', documenContent: {}},
    {documentNo: 16, documentName: '法定代表人身份证复印件', documentType: '材料类', documentCode: '', documenContent: {}},
    {
      documentNo: 18,
      documentName: '整改复查意见书',
      documentType: '现场措施类',
      documentCode: '（西安）应急复查〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 19,
      documentName: '案件调查终结报告',
      documentType: '调查取证类',
      documentCode: '（西安）应急终报〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 20,
      documentName: '行政执法决定法制审核意见书',
      documentType: '告知听证类',
      documentCode: '（西安）应急法审〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 21,
      documentName: '行政处罚告知书',
      documentType: '告知听证类',
      documentCode: '（西安）应急罚告〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 22,
      documentName: '行政处罚听证会通知书',
      documentType: '告知听证类',
      documentCode: '（西安）应急听通〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {documentNo: 23, documentName: '听证笔录', documentType: '告知听证类', documentCode: '', documenContent: {}},
    {
      documentNo: 24,
      documentName: '听证会报告书',
      documentType: '告知听证类',
      documentCode: '（西安）应急听报〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },


    {
      documentNo: 25,
      documentName: '行政处罚集体讨论记录',
      documentType: '告知听证类',
      documentCode: '（西安）应急罚集〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 26,
      documentName: '案件处理呈批表',
      documentType: '审查决定类',
      documentCode: '（西安）应急处呈〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 28,
      documentName: '行政处罚决定书',
      documentType: '审查决定类',
      documentCode: '（西安）应急罚决〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 29,
      documentName: '行政处罚决定履行催告书',
      documentType: '送达执行类',
      documentCode: '（西安）应急加罚〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 30,
      documentName: '延期（分期）缴纳罚款批准书',
      documentType: '送达执行类',
      documentCode: '（西安）应急缴批〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 31,
      documentName: '文书送达回执',
      documentType: '审查决定类',
      documentCode: '（西安）应急回〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {
      documentNo: 32,
      documentName: '结案审批表',
      documentType: '结案归档类',
      documentCode: '（西安）应急结〔' + year + '〕' + area + xh + '号',
      documenContent: {}
    },
    {documentNo: 33, documentName: '卷内备考表', documentType: '结案归档类', documentCode: '', documenContent: {}},
  ]
  dialogConfig.value.loading = true
  //查找案件文书
  apiConfig.value.documentapi.getDocuments({caseId: row.caseId}).then(res => {
    if (res.success) {
      let existDoc = res.data
      existDoc.forEach(data => {
        const index = wsmu.findIndex(d => d.documentNo === data.documentNo);
        if (index !== -1) {
          wsmu[index] = data; // 替换元素
        }
      })
      dialogConfig.value.documents = wsmu;
      dialogConfig.value.data = Object.assign(dialogConfig.value.data, row)
      dialogConfig.value.mode = 'detail'
      dialogConfig.value.loading = false
      dialogConfig.value.show = true
    }
  }).catch(() => dialogConfig.value.loading = false)

}


function handleDialogOk(formRef) {
  let _event = actions.value
  dialogConfig.value.loading = true
  ElMessage.success('操作成功')
  dialogConfig.value.loading = false
}

const downloadFile = (row) => { // 下
  if (row && row.downUrl) {
    window.open(row.downUrl);
  }
}
</script>
<style lang="scss" scoped>
</style>
