<!--
  * 入库明细
  *
  * @Author:    1024创新实验室-主任：卓大
  * @Date:      2022-08-15 20:15:49
  * @Wechat:    zhuda1024
  * @Email:     lab1024@163.com
  * @Copyright  1024创新实验室 （ https://1024lab.net ），Since 2012
-->
<template>
  <div>
    <div class="header">
      <div>
        关键字：
        <a-input style="width: 250px" v-model:value="queryForm.keyword" placeholder="姓名/手机号/登录账号"/>
        <a-button class="button-style" type="primary" @click="onSearch">搜索</a-button>
        <a-button class="button-style" type="default" @click="resetQuery">重置</a-button>
      </div>

      <div class="smart-table-setting-block">
        <a-button class="button-style" type="primary" @click="showForm" :disabled="props.asnId==0">
          <template #icon>
            <PlusOutlined/>
          </template>
          添加明细
        </a-button>
        <a-button class="button-style" type="primary" danger
                  :disabled="props.asnId==0 || selectedRowKeyList.length == 0">
          <template #icon>
            <DeleteOutlined/>
          </template>
          批量移除
        </a-button>
        <TableOperator v-model="columns" :tableId="TABLE_ID_CONST.BUSINESS.RECEIVE.ASN_DETAIL" :refresh="queryData"/>
      </div>

    </div>
    <a-table
        :loading="tableLoading"
        :dataSource="tableData"
        :columns="columns"
        :pagination="false"
        rowKey="asnDetailId"
        :row-selection="{ selectedRowKeys: selectedRowKeyList, onChange: onSelectChange }"
        size="small"
        bordered
    >
      <template #bodyCell="{ text, record, index, column }">
        <template v-if="column.dataIndex === 'action'">
          <div class="smart-table-operate">
            <a-button @click="showForm(record)" type="link" v-privilege="'asnDetail:update'">
              <template #icon>
                <EditOutlined/>
              </template>
              编辑
            </a-button>
            <a-button @click="onDelete(record)" danger type="link" v-privilege="'asnDetail:delete'">
              <template #icon>
                <DeleteOutlined/>
              </template>
              删除
            </a-button>
          </div>
        </template>

      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
          showSizeChanger
          showQuickJumper
          show-less-items
          :pageSizeOptions="PAGE_SIZE_OPTIONS"
          :defaultPageSize="queryForm.pageSize"
          v-model:current="queryForm.pageNum"
          v-model:pageSize="queryForm.pageSize"
          :total="total"
          @change="queryData"
          @showSizeChange="queryData"
          :show-total="(total:number) => `共${total}条`"
      />
    </div>
    <!--新建/编辑-->
    <AsnDetailForm ref="formRef" @reloadList="queryData"/>
  </div>
</template>
<script setup lang="ts">
import _ from 'lodash';
import {reactive, ref, watch} from 'vue';
import {PAGE_SIZE, PAGE_SIZE_OPTIONS} from '/@/constants/common-const';
import {smartSentry} from '/@/lib/smart-sentry';
import {TABLE_ID_CONST} from "/@/constants/support/table-id-const";
import TableOperator from "/@/components/support/table-operator/index.vue";
import {asnDetailApi} from "/@/api/business/receive/asnDetail/asn-detail-api";
import AsnDetailForm from "/@/views/business/receive/asnDetail/asn-detail-form.vue";
import {message, Modal} from "ant-design-vue";
import {SmartLoading} from '/@/components/framework/smart-loading';

const props = defineProps({
  asnId: {
    type: Number,
    default: 0,
  },
});
const columns = reactive([
  {
    title: '入库明细id',
    dataIndex: 'asnDetailId',
    ellipsis: true,
  },
  {
    title: '物料编码',
    dataIndex: 'itemCode',
    ellipsis: true,
  },
  {
    title: '物料名称',
    dataIndex: 'itemName',
    ellipsis: true,
  },
  {
    title: '订单数量',
    dataIndex: 'orderQuantity',
    ellipsis: true,
  },
  {
    title: '收货数量',
    dataIndex: 'receivedQuantity',
    ellipsis: true,
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 140,
  },
]);

// --------------------------- 查询 ---------------------------
const defaultQueryForm = {
  pageNum: 1,
  pageSize: PAGE_SIZE,
  asnId: undefined,
  keyword: undefined,
}

// 查询表单
const queryForm = reactive({...defaultQueryForm});
// 表格加载loading
const tableLoading = ref(false);
// 表格数据
const tableData = ref([]);
// 总数
const total = ref(0);


// 重置查询条件
function resetQuery() {
  let pageSize = queryForm.pageSize;
  Object.assign(queryForm, defaultQueryForm);
  queryForm.pageSize = pageSize;
  queryData();
}

// 搜索
function onSearch() {
  queryForm.pageNum = 1;
  queryData();
}

//查询数据
async function queryData() {
  tableLoading.value = true;
  try {
    queryForm.asnId = props.asnId;
    let queryResult = await asnDetailApi.queryPage(queryForm);
    tableData.value = queryResult.data.list;
    total.value = queryResult.data.total;
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    tableLoading.value = false;
  }
}

// 选择表格行
const selectedRowKeyList = ref([]);

function onSelectChange(selectedRowKeys: any) {
  selectedRowKeyList.value = selectedRowKeys;
}

//新建/编辑
const formRef = ref();

function showForm(data: object) {
  formRef.value.show(data, props.asnId);
}

//确认是否删除？
function onDelete(data) {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选吗?',
    okText: '删除',
    okType: 'danger',
    onOk() {
      requestDelete(data);
    },
    cancelText: '取消',
    onCancel() {
    },
  });
}

//请求删除
async function requestDelete(data) {
  SmartLoading.show();
  try {
    await asnDetailApi.delete(data.asnDetailId);
    message.success('删除成功');
    await queryData();
  } catch (e) {
    smartSentry.captureError(e);
  } finally {
    SmartLoading.hide();
  }
}

watch(
    () => props.asnId,
    (e) => {
      if (e) {
        queryData();
      }
    },
    {immediate: true}
);
</script>

<style scoped lang="less">
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px
}

.button-style {
  margin-left: 10px;
}
</style>
