<template>
  <div class="p-2">
    <!--查询区域-->
    <div class="jeecg-basic-table-form-container">
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol"
        :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="6">
            <a-form-item name="optNames">
              <template #label><span title="员工">员工</span></template>
              <j-dict-select-tag v-model:value="queryParam.optNames"
                :dictCode="`nu_biz_employees_info,name,name, 1=1 order by entry_time desc`" placeholder="请选择员工"
                allowClear />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="nuId">
              <template #label><span title="区域名称">区域名称</span></template>
              <j-dict-select-tag v-model:value="queryParam.nuId"
                :dictCode="`nu_base_info,nu_name,nu_id,del_flag = 0 order by area_flag asc`" placeholder="请选择区域名称"
                allowClear />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="instructionTagId">
              <template #label><span title="分类标签">分类标签</span></template>
              <j-dict-select-tag v-model:value="queryParam.instructionTagId"
                :dictCode="`nu_config_service_instruction_tag,instruction_name,id,del_flag = 0 order by sort asc`"
                placeholder="请选择分类标签" allowClear />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="categoryId">
              <template #label><span title="服务类别">服务类别</span></template>
              <j-dict-select-tag type="list" v-model:value="queryParam.categoryId"
                :dictCode="`nu_config_service_category,category_name,id,del_flag = 0 and instruction_id = '${queryParam.instructionTagId || ''}' order by sort asc`"
                placeholder="请选择服务类别" allowClear :ignoreDisabled="true" />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="typeId">
              <template #label><span title="服务类型">服务类型</span></template>
              <j-dict-select-tag type="list" v-model:value="queryParam.typeId"
                :dictCode="`nu_config_service_type,type_name,id,del_flag = 0 and category_id = '${queryParam.categoryId || ''}' order by sort asc`"
                placeholder="请选择服务类型" allowClear :ignoreDisabled="true" />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="directiveName">
              <template #label><span title="服务指令">服务指令</span></template>
              <JInput v-model:value="queryParam.directiveName" placeholder="请输入服务指令" />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="startTime">
              <template #label><span title="指令日期">指令日期</span></template>
              <a-range-picker value-format="YYYY-MM-DD" v-model:value="queryParam.startTime" class="query-group-cust" />
            </a-form-item>
          </a-col>
          <!-- <a-col :lg="6">
            <a-form-item name="optType">
              <template #label><span title="执行类型 1单人 2协助 3转单">执行类型</span></template>
              <j-select-multiple placeholder="请选择执行类型 1单人 2协助 3转单" v-model:value="queryParam.optType"
                dictCode="directive_order_opt_type" allow-clear />
            </a-form-item>
          </a-col> -->
          <a-col :lg="6">
            <a-form-item name="optStatus">
              <template #label><span title="执行状态">执行状态</span></template>
              <j-dict-select-tag v-model:value="queryParam.optStatus" dictCode="directive_order_opt_status"
                placeholder="请选择执行状态" allowClear />
            </a-form-item>
          </a-col>
          <a-col :lg="6">
            <a-form-item name="status">
              <template #label><span title="审核状态">审核状态</span></template>
              <j-dict-select-tag v-model:value="queryParam.status" dictCode="appraisal_status" placeholder="请选择审核状态"
                allowClear />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset"
                  style="margin-left: 8px">重置</a-button>
              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <DirectiveAppraisalModal ref="registerModal" @success="handleSuccess"></DirectiveAppraisalModal>
  </div>
</template>

<script lang="ts" name="appraisal-directiveAppraisal" setup>
import { ref, reactive, watch } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { columns, superQuerySchema } from './DirectiveAppraisal.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, handleRevocation } from './DirectiveAppraisal.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import DirectiveAppraisalModal from './components/DirectiveAppraisalModal.vue'
import { useUserStore } from '/@/store/modules/user';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
import JInput from "/@/components/Form/src/jeecg/components/JInput.vue";
import { cloneDeep } from "lodash-es";
import { useMessage } from '/@/hooks/web/useMessage';

const { createMessage } = useMessage();
const formRef = ref();
const queryParam = reactive<any>({
  startTime: (() => {
    const d = new Date();
    d.setDate(d.getDate() - 1); // 减一天
    const dateStr = d.toISOString().split('T')[0];
    return [dateStr, dateStr];
  })()
});
const toggleSearchStatus = ref<boolean>(false);
const registerModal = ref();
const userStore = useUserStore();
watch(
  () => queryParam.instructionTagId,
  () => {
    queryParam.categoryId = ''
    queryParam.typeId = ''
  }
)

// 当服务类别变动，清空服务类型
watch(
  () => queryParam.categoryId,
  () => {
    queryParam.typeId = ''
  }
)
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '护理单元-服务指令-工单',
    api: list,
    columns,
    canResize: false,
    useSearchForm: false,
    actionColumn: {
      width: 120,
      fixed: 'right',
    },
    beforeFetch: async (params) => {
      let rangerQuery = await setRangeQuery();
      return Object.assign(params, rangerQuery);
    },
  },
  exportConfig: {
    name: "护理单元-服务指令-工单",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
});
const [registerTable, { reload, collapseAll, updateTableDataRecord, findTableDataRecord, getDataSource }, { rowSelection, selectedRowKeys }] = tableContext;
const labelCol = reactive({
  xs: 24,
  sm: 4,
  xl: 6,
  xxl: 4
});
const wrapperCol = reactive({
  xs: 24,
  sm: 20,
});


/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  registerModal.value.disableSubmit = false;
  registerModal.value.edit(record);
}

/**
 * 详情
 */
function handleDetail(record: Recordable) {
  registerModal.value.disableSubmit = true;
  registerModal.value.edit(record);
}

/**
 * 成功回调
 */
function handleSuccess() {
  (selectedRowKeys.value = []) && reload();
}

/**
 * 撤回
 * @param record 
 */
function handleRevocationFunc(record) {
  handleRevocation({ id: record.id }).then(res => {
    createMessage.success('操作成功');
    reload()
  })
}

/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    {
      label: '详情',
      onClick: handleDetail.bind(null, record),
    }, {
      label: '考核',
      onClick: handleEdit.bind(null, record),
      ifShow: record.status == '1' || record.status == '4'
    }, {
      label: '撤回',
      ifShow: record.status == '2' || record.status == '3',
      popConfirm: {
        title: '是否确认撤回',
        confirm: handleRevocationFunc.bind(null, record),
      },
    },
  ];
}

/**
 * 查询
 */
function searchQuery() {
  reload();
}

/**
 * 重置
 */
function searchReset() {
  formRef.value.resetFields();
  selectedRowKeys.value = [];
  //刷新数据
  reload();
}



let rangeField = 'startTime,'

/**
 * 设置范围查询条件
 */
async function setRangeQuery() {
  let queryParamClone = cloneDeep(queryParam);
  if (rangeField) {
    let fieldsValue = rangeField.split(',');
    fieldsValue.forEach(item => {
      if (queryParamClone[item]) {
        let range = queryParamClone[item];
        queryParamClone[item + '_begin'] = range[0];
        queryParamClone[item + '_end'] = range[1];
        delete queryParamClone[item];
      } else {
        queryParamClone[item + '_begin'] = '';
        queryParamClone[item + '_end'] = '';
      }
    })
  }
  return queryParamClone;
}
</script>

<style lang="less" scoped>
.jeecg-basic-table-form-container {
  padding: 0;

  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }

  .query-group-cust {
    min-width: 100px !important;
  }

  .query-group-split-cust {
    width: 30px;
    display: inline-block;
    text-align: center
  }

  .ant-form-item:not(.ant-form-item-with-help) {
    margin-bottom: 16px;
    height: 32px;
  }

  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
}
</style>
