<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" hidden>
            <a-form-item name="nuId">
              <template #label><span title="库房">库房</span></template>
              <j-dict-select-tag type='list' placeholder="请选择库房" v-model:value="queryParam.nuId"
                dictCode="nu_base_info,nu_name,nu_id,area_flag = '3'" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="5">
            <a-form-item name="wlParamInfo">
              <template #label><span title="物料信息">物料信息</span></template>
              <a-input placeholder="请填写物料信息" v-model:value="queryParam.wlParamInfo" allow-clear></a-input>
            </a-form-item>
          </a-col>

          <a-col :lg="5">
            <a-form-item name="categoryId">
              <template #label><span title="一级分类">一级分类</span></template>
              <j-dict-select-tag v-model:value="queryParam.categoryId"
                :dictCode="`nu_config_material_category,category_name,id,del_flag = 0 and iz_enabled = 0 order by sort asc`"
                placeholder="请选择一级分类" allowClear :ignoreDisabled="true"
                @select="handleSearch({ 'levle': 1, 'key': queryParam.categoryId }, false)" />
              <!-- <span v-else>请选择源平台</span> -->
            </a-form-item>
          </a-col>

          <a-col :lg="5">
            <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_material_type,type_name,id,del_flag = 0 and iz_enabled = 0 and category_id = '${queryParam.categoryId || ''}' `"
                placeholder="请选择二级分类" allowClear :ignoreDisabled="true"
                @select="handleSearch({ 'levle': 2, 'categoryId': queryParam.categoryId, 'key': queryParam.typeId }, false)" />
            </a-form-item>
          </a-col>

          <a-col :lg="5">
            <a-form-item name="medicationId">
              <template #label><span title="三级分类">三级分类</span></template>
              <j-dict-select-tag type="list" v-model:value="queryParam.medicationId"
                :dictCode="`nu_config_material_medication,medication_name,id,del_flag = 0 and iz_enabled = 0 and category_id = '${queryParam.categoryId || ''}' and type_id = '${queryParam.typeId || ''}' `"
                placeholder="请选择三级分类" allowClear :ignoreDisabled="true"
                @select="handleSearch({ 'levle': 3, 'categoryId': queryParam.categoryId, 'typeId': queryParam.typeId, 'key': queryParam.medicationId }, false)" />
            </a-form-item>
          </a-col>

          <a-col :xl="2" :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">
      <!--插槽:table标题-->
      <template #tableTitle>
        <a-button type="primary" v-auth="'warehouseMaterialInfo:bl_warehouse_material_info:add'" @click="handleAdd"
          preIcon="ant-design:plus-outlined"> 添加物料</a-button>
        <a-button type="primary" v-auth="'warehouseMaterialInfo:bl_warehouse_material_info:add'" @click="handleDel"
          preIcon="ant-design:plus-outlined"> 移除物料</a-button>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <BlWarehouseMaterialInfoModal ref="registerModal" @success="handleSuccess"></BlWarehouseMaterialInfoModal>
    <!-- 添加物料 -->
    <CheckWuliaoModal ref="registerAddModal" @success="handleSuccess"></CheckWuliaoModal>
    <!-- 移除物料 -->
    <DelWuliaoModal ref="registerDelModal" @success="handleSuccess"></DelWuliaoModal>
    <!-- 出入库详情 -->
    <NuWarehouseMaterialCrkInfoListModal ref="registerCrkModal" @success="handleSuccess">
    </NuWarehouseMaterialCrkInfoListModal>
  </div>
</template>

<script lang="ts" name="warehouseMaterialInfo-blWarehouseMaterialInfo" setup>
import { ref, reactive } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { columns, superQuerySchema } from './BlWarehouseMaterialInfo.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './BlWarehouseMaterialInfo.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import BlWarehouseMaterialInfoModal from './components/BlWarehouseMaterialInfoModal.vue'
import CheckWuliaoModal from './components/CheckWuliaoModal.vue'
import DelWuliaoModal from './components/DelWuliaoModal.vue'
import NuWarehouseMaterialCrkInfoListModal from '/@/views/invoicing/warehouseMaterialInfo/NuWarehouseMaterialCrkInfoListModal.vue'
import { useUserStore } from '/@/store/modules/user';
import JSelectMultiple from '/@/components/Form/src/jeecg/components/JSelectMultiple.vue';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { JInput } from '/@/components/Form';
import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";


const formRef = ref();
const queryParam = reactive<any>({});
const toggleSearchStatus = ref<boolean>(false);
const registerModal = ref();
const registerAddModal = ref();
const registerDelModal = ref();
const registerCrkModal = ref();
const userStore = useUserStore();
const { createConfirm } = useMessage();
//注册table数据
const { prefixCls, tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '库房物料配置信息',
    api: list,
    columns,
    canResize: false,
    useSearchForm: false,
    immediate: false,
    actionColumn: {
      width: 180,
      fixed: 'right',
    },
    beforeFetch: async (params) => {
      return Object.assign(params, queryParam);
    },
  },
  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: 6
});
const wrapperCol = reactive({
  xs: 24,
  sm: 18,
});

// 高级查询配置
const superQueryConfig = reactive(superQuerySchema);

/**
 * 高级查询事件
 */
function handleSuperQuery(params) {
  Object.keys(params).map((k) => {
    queryParam[k] = params[k];
  });
  searchQuery();
}

/**
 * 新增事件
 */
function handleAdd() {
  var params = { nuId: queryParam.nuId }
  registerAddModal.value.disableSubmit = false;
  registerAddModal.value.edit(params);
}
/**
 * 新增事件
 */
function handleDel() {
  var params = { nuId: queryParam.nuId }
  registerDelModal.value.disableSubmit = false;
  registerDelModal.value.edit(params);
}

/**
 * 编辑事件
 */
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 handleCrkjl(record: Recordable) {
  registerCrkModal.value.disableSubmit = true;
  registerCrkModal.value.init(record);
}

/**
 * 删除事件
 */
async function handleDelete(record) {
  await deleteOne({ id: record.id }, handleSuccess);
}

/**
 * 批量删除事件
 */
async function batchHandleDelete() {
  await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
}

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

/**
 * 操作栏
 */
function getTableAction(record) {
  return [
    // {
    //   label: '编辑',
    //   onClick: handleEdit.bind(null, record),
    //   auth: 'warehouseMaterialInfo:bl_warehouse_material_info:edit'
    // },
    // {
    //   label: '详情',
    //   onClick: handleDetail.bind(null, record),
    // },
    {
      label: '启用',
      onClick: handleQyty.bind(null, record, 'Y'),
      auth: 'warehouseMaterialInfo:bl_warehouse_material_info:edit',
      ifShow: record.izEnabled == 'N'
    },
    {
      label: '停用',
      onClick: handleQyty.bind(null, record, 'N'),
      auth: 'warehouseMaterialInfo:bl_warehouse_material_info:edit',
      ifShow: record.izEnabled == 'Y'
    },
    {
      label: '记录',
      onClick: handleCrkjl.bind(null, record, '1'),
    },
  ];
}
function handleQyty(record, izEnabled) {
  
    var  title = "是否启用";
    var  content = "是否启用此物料";
    if(izEnabled == '1'){
      title = "是否停用";
      content = "是否停用此物料";
    }
    createConfirm({
      iconType: 'warning',
      title: title,
      content: content,
      okText: '确认',
      cancelText: '取消',
      onOk: () => {
        defHttp.put({ url: '/invoicing/blWarehouseMaterialInfo/edit', params: { id: record.id, izEnabled: izEnabled } }).then(res => {
          handleSuccess();
        });
      }
    });
  
}


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

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


function init(record) {
  queryParam.nuId = record.nuId;
  reload();
}

function handleSearch(data, izReload = true) {
  console.log("🚀 ~ handleSearch ~ data:", data)
  var levle = parseInt(data.levle)
  if (levle == 1) {
    queryParam.categoryId = data.key
    queryParam.typeId = ''
    queryParam.medicationId = ''
  } else if (levle == 2) {
    queryParam.categoryId = data.categoryId
    queryParam.typeId = data.key
    queryParam.medicationId = ''
  } else if (levle == 3) {
    queryParam.categoryId = data.categoryId
    queryParam.typeId = data.typeId
    queryParam.medicationId = data.key
  }
  if (izReload) {
    reload();
  }
}

defineExpose({
  init,
});

</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>
