<template>
 <div class="h100">
   <common-layout
       @getCheckedTable="getCheckedTable"
     ref="layoutRef"
     :form-attrs="{
     spanAttrs: {
       span:4
     }
     }"

     :get-table-list="getTableList"
     :table-header="qualityCheckReportHomeTableHeader"
     :filter-form="qualityCheckReportHomeFilterForm"
   :table-attrs="{
     'has-operation':true,
     'multi-select':true

   }">
   <template #exportBtn>
     <el-button type="primary" :icon="Upload" @click="mulitExport">批量导出</el-button>
   </template>
     <template #UNIT_CODE="{data}">
       {{store.allSelectUnit.find(i => i.value === data.UNIT_CODE)?.label}}
     </template>
     <template #REPORT_TYPE="{data}">
       {{getReportType(data.REPORT_TYPE)}}
     </template>
     <template #STATE="{data}">
       {{getState(data.STATE)}}
     </template>
     <template #FEEDBACK_STATE="{data}">
       {{getReportFallback(data.FEEDBACK_STATE)}}
     </template>
     <template #INSERTTIME="{data}">
       {{formatDateFun(+new Date(data.INSERTTIME),'yyyy年MM月dd日')}}
     </template>
     <template #operation="{data}">
       <template v-if="data.STATE > 1">
         <el-button type="info" @click="$emit('changeOperation','detail',data)">详情</el-button>
         <el-button type="info" v-if="data.FEEDBACK_STATE === '1'" @click="$emit('changeOperation','hf',data)">回复</el-button>
         <el-button type="info" @click="singleExport(data.UUID)">导出</el-button>
       </template>
       <template v-else>
         <el-button  type="info"  @click="$emit('changeOperation','edit',data)">编辑</el-button>
       </template>
       <el-button type="info" v-if="data.STATE ==='1'" @click="$emit('changeOperation','check',data)">审核</el-button>
     </template>
   </common-layout>
 </div>
</template>

<script>
import CommonLayout from "@/components/CommonLayout.vue";
import {qualityCheckReportHomeFilterForm} from "@/data/all-forms";
import {qualityCheckReportHomeTableHeader} from "@/data/all-table-header";
import {ref} from "vue";
import {Upload} from "@element-plus/icons-vue";
import {mulitCrateReportPath, qualityCheckReportHomeTableData} from "@/service/data-quelity-check/qiality-check-report";

import {formatDateFun} from "../../../../../../unit/time";
import {baseUrl} from "@/service/config";
import {useRootStore} from "@/stores/useStore";
import {elAlert, showMessage} from "@/unit/element-ui/tip";
import {useActiveGetTableData} from "@/unit/use-method/usePagination";

export default {
  name: "QualityCheckReportHome",
  methods: {formatDateFun},
  computed: {
    Upload() {
      return Upload
    }
  },
  components: {CommonLayout},
  setup() {
    useActiveGetTableData(getTableList)
    const store = useRootStore()
    const checkedArr = ref([])
    function getCheckedTable(che){
      checkedArr.value = che
    }
    function mulitExport() {
      if (!(checkedArr.value.length > 0)){
        elAlert('请选择要导出的质检报告')
      }else {
        mulitCrateReportPath(checkedArr.value.map(i => i.UUID).join(',')).then(res => {
           try {
             const data = res.msg[0].Rows
             if (data.length > 0){
               let paths = data.join(',')
               window.location.href = baseUrl + "/ptjkInterface/exportTemplateWords?appkey="+useRootStore().userInfo.ptjk_userkeycode+"&paths=" + paths;
             }
           }catch{
             showMessage({
               type:'error',
               message:'生成报告失败'
             })
           }
        })
      }
    }
    function singleExport(uuid){
      window.location.href = baseUrl + "/ptjkInterface/exportTemplateWord?appkey="+useRootStore().userInfo.ptjk_userkeycode+"&UUID=" + uuid;
    }
    const layoutRef = ref(null)
    function getReportType(value) {
      if(value == "1"){ return "年报"; } return "月报";
    }
    function getState(value){
      switch(value){ case "1": return "保存"; case "2": return "已审核"; }
    }
    function getReportFallback(value){
      switch (value) { case "0": return "未反馈"; case "1": return "已反馈"; case "2": return "已回复"; }
    }
    function getTableList() {
      qualityCheckReportHomeTableData({
        PAGEINDEX:layoutRef.value.pageIndex,
        PAGESIZE:layoutRef.value.pageSize,
        UNIT_CODE:layoutRef.value.form.UNIT_CODE,
        FEEDBACK_STATE:layoutRef.value.form.FEEDBACK_STATE,
        REPORT_TYPE:layoutRef.value.form.REPORT_TYPE,
      }).then(res => {
        layoutRef.value.getResData(res.msg[0].Rows||[],res.msg[0].TotalRowCount)
      })

    }
    return {
      qualityCheckReportHomeFilterForm,
      qualityCheckReportHomeTableHeader,
      layoutRef,getTableList,
      getReportType,
      getState,
      getReportFallback,
      singleExport,
      getCheckedTable,
      mulitExport,
      store

    }
  }
}
</script>

<style scoped lang="scss">

</style>
