<template>
  <div>
    <div class="jeecg-basic-table-form-container" @keyup.enter="searchQuery">
        <a-form :model="queryParam" :label-col="labelCol" :wrapper-col="wrapperCol">
          <a-row :gutter="24">
            <a-col :lg="6">
                    <a-form-item :label="searchFormSchema[0].label">
                        <JInput placeholder="请输入" v-model:value="queryParam[searchFormSchema[0].field]"></JInput>
                    </a-form-item>
                </a-col>
                 <a-col :lg="6">
                     <a-form-item :label="searchFormSchema[1].label">
                      <JSearchSelect placeholder="请选择" v-model:value="queryParam[searchFormSchema[1].field]" dict="projecttype"></JSearchSelect>
                      </a-form-item>
                 </a-col>
                 <a-col :lg="6">
                     <a-form-item :label="searchFormSchema[2].label">
                      <JSelectDept placeholder="请选择"  v-model:value="jSelectDeptVal" :multiple="false" @change="updateJSelectDept"></JSelectDept>
                     </a-form-item>
                 </a-col>
                 <a-col :lg="6">
                    <a-form-item>
                      <a-space :size="5">
                        <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery" >查询</a-button>
                        <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset" >重置</a-button>
                      </a-space>
                    </a-form-item>
                 </a-col>
            </a-row>
        </a-form>

    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
     <!--插槽:table标题-->
      <template #tableTitle>
      </template>
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)"/>
      </template>
      <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'configStatus'">
              <span v-if="record.submitStatus=='1'" style="color:green">准备完成</span>
              <span v-else style="color:red">
                {{ getConfigStep(record.configStep) }}
                ({{ record.configStep==0?1:record.configStep}}/7)
              </span>
          </template>
      </template> 
      <!--字段回显插槽-->
      <template #htmlSlot="{text}">
         <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{text}">
         {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{text}">
         <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
         <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small" @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <StProjectConfigModal @register="registerModal" @success="handleSuccess"></StProjectConfigModal>
    <StProjectImportModal @register="registerImportModal" @success="handleSuccess"></StProjectImportModal>
    <StProjectPrepareViewModal @register="registerViewModal" @success="handleSuccess"></StProjectPrepareViewModal>
    </div>
  </div>
</template>

<script lang="ts" name="project.create-stProjectCre" setup>
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { Modal } from 'ant-design-vue';
import { createVNode, reactive, ref, toRaw, unref } from 'vue';
import { submitPrepate } from './StProjectConfig.api';
import { batchDelete, getExportUrl, getImportUrl, listPrepar } from './StProjectCre.api';
import { columnsPrepar, searchFormSchema } from './StProjectCre.data';
import StProjectConfigModal from './components/StProjectConfigModal.vue';
import StProjectImportModal from './components/StProjectImportModal.vue';
import StProjectPrepareViewModal from './components/StProjectPrepareViewModal.vue';
import { JInput, JSearchSelect, JSelectDept } from '/@/components/Form';
import { useModal } from '/@/components/Modal';
import { BasicTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { useUserStore } from '/@/store/modules/user';
import { downloadFile } from '/@/utils/common/renderUtils';

  const checkedKeys = ref<Array<string | number>>([]);
  const showBtn = ref(false)
  const jSelectDeptVal = ref([])
  const userStore = useUserStore();
  //注册model
  const [registerModal, {openModal}] = useModal();
  const [registerImportModal, {openModal:openImportModal}] = useModal();
  const [registerViewModal, {openModal:openViewModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '项目准备',
           api: listPrepar,
           columns:columnsPrepar,
           canResize:false,
           useSearchForm: false,
           showTableSetting: false,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 240,
               fixed:'right'
            },
            beforeFetch(params) {
              params = Object.assign(params,{ column: 'approveTime', order: 'desc'})
            },
      },
       exportConfig: {
            name:"项目立项",
            url: getExportUrl,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload,setProps},{ rowSelection, selectedRowKeys }] = tableContext

  const labelCol = reactive({
    xs: {span: 24},
    sm: {span: 8},
  })
  const wrapperCol = reactive({
      xs: {span: 24},
      sm: {span: 16},
  })
  const queryParam = reactive({
  });
  function initParam() {
    const schemas = unref(searchFormSchema);
    schemas.forEach((item) => {
      queryParam[item.field] = ''
    });
    jSelectDeptVal.value = []
  }
  initParam()
  function updateJSelectDept(val){
    if(val.length>0){
      queryParam[searchFormSchema[2].field] = val[0]
    }else{
      jSelectDeptVal.value = []
      queryParam[searchFormSchema[2].field] = ''
    }
  }
  let config = ['配置检查范围','配置检查范围','配置检查活动','配置实施人员','配置检查方法','配置责任分工','配置信息采集','配置访谈计划']
  function getConfigStep(step){
    return config[step]
  }
  function searchQuery(){
      setProps({searchInfo:toRaw(queryParam)});
      reload()
  }
  function searchReset(){
    initParam()
    reload();
}
   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 编辑事件
    */
  function handleEdit(record: Recordable) {
    //  openModal(true, {
    //    record,
    //    isUpdate: true,
    //    showFooter: true,
    //  });
   }
   function handleSubmit(record: Recordable) {
    Modal.confirm({
        title: '项目准备提交',
        icon: createVNode(ExclamationCircleOutlined),
        content: '你是否确认提交项目准备内容？',
        okText: '确认',
        cancelText: '取消',
        onOk() {
          submitPrepate({projectCreId:record.id}).then(rep=>{
            reload();
          })
        },
      });
   }
   function handleConfig(record: Recordable) {
     openModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   function handleImport(record: Recordable){
    openImportModal(true, {
       record,
       isUpdate: true,
       showFooter: true,
     });
   }
   function handleView(record: Recordable){
    openViewModal(true, {
       record,
       isUpdate: true,
       showFooter: false,
     });
   }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
    console.log('--成功回调-');
    (selectedRowKeys.value = []) && reload();
   }
   /**
      * 操作栏
      */
  function getTableAction(record){
       return [
       {
           label: '配置',
           disabled: record['configStatus']=='1'||record['submitStatus']=='1',
           onClick: handleConfig.bind(null, record),
         },
         {
           label: '提交',
           disabled: record['submitStatus']=='1',
           onClick: handleSubmit.bind(null, record),
         },
         {
           label: '导入配置',
           disabled: record['submitStatus']=='1',
           onClick: handleImport.bind(null, record),
         },
         {
           label: '查看',
           onClick: handleView.bind(null, record),
         }, 
       ]
   }
     /**
        * 下拉操作栏
        */
  function getDropDownAction(record){
       return [
         
       ]
   }


</script>

<style scoped>

</style>