<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="1200px">
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-row :gutter="20">
        <el-col :span="5">
          <el-form-item label="销售单" prop="no">
            <el-input
              v-model="queryParams.no"
              placeholder="请输入销售单编号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-150px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="客户" prop="customerId">
            <el-input
              v-model="queryParams.customerId"
              placeholder="请输入客户编号"
              clearable
              @keyup.enter="handleQuery"
              class="!w-150px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              @keyup.enter="handleQuery"
              class="!w-150px"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="下单时间" prop="orderTime">
            <el-date-picker
              v-model="queryParams.orderTime"
              value-format="YYYY-MM-DD HH:mm:ss"
              type="daterange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
              class="!w-240px"
            />
          </el-form-item>
        </el-col>

      </el-row>
      <el-row align="middle">
        <el-form-item  class="!w-240px">
          <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
          <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        </el-form-item>
      </el-row>

    </el-form>
  </ContentWrap>

  <!-- 列表 -->

    <ContentWrap>
      <el-table
        v-loading="loading"
        :data="list"
        :stripe="true"
        :show-overflow-tooltip="true"
        highlight-current-row
        @current-change="handleCurrentChange"
      >

        <el-table-column label="销售单编号" align="center" prop="no" />
        <el-table-column label="销售状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :type="DICT_TYPE.ERP_AUDIT_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="客户" align="center" prop="customerName" />
        <el-table-column label="合计数量" align="center" prop="totalCount" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="下单时间"
          align="center"
          prop="orderTime"
          :formatter="dateFormatter"
          width="180px"
        />
      </el-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  <!-- 子表的列表 -->
    <el-tabs model-value="saleOrderItems">
      <el-tab-pane label="销售单明细项" name="saleOrderItems">
        <el-table
          v-loading="loading" :data="listDetail"
          :stripe="true" :show-overflow-tooltip="true" @selection-change="handleSelectionChange"
        >
          <el-table-column width="30" label="选择" type="selection" />
          <el-table-column label="产品" align="center" prop="productName" />
          <el-table-column label="数量" align="center" prop="count" />
          <el-table-column label="单位" align="center" prop="unitName" />
          <el-table-column label="单价" align="center" prop="productPrice" />
          <el-table-column label="总价" align="center" prop="totalPrice" />
          <el-table-column label="备注" align="center" prop="remark" />

          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            :formatter="dateFormatter"
            width="180px"
          />

        </el-table>
      </el-tab-pane>
    </el-tabs>


    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="loading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>

</template>
<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import {SaleOrderApi, SaleOrderItemsVO, SaleOrderVO} from '@/api/mes/saleorder'


/** 销售订单 列表 */
defineOptions({ name: 'SaleOrder' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const listDetail = ref([]) // 列表的数据

const loading = ref(true) // 列表的加载中
const list = ref<SaleOrderVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  no: undefined,
  status: undefined,
  customerId: undefined,
  orderTime: [],
  remark: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中


/** 查询列表 */
const getList = async () => {
  loading.value = true
  listDetail.value = null
  try {
    queryParams.status = 20
    const data = await SaleOrderApi.getSaleOrderPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = async (row) => {
  currentRow.value = row
  loading.value = true
  try {
    listDetail.value = await SaleOrderApi.getSaleOrderItemsByOrderId(row.id)
  } finally {
    loading.value = false
  }
}

/** 打开弹窗 */
const open = async () => {
  dialogVisible.value = true
  dialogTitle.value = "选择销售单明细项"
  listDetail.value = null
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 提交请求
  try {
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success', selectionList.value)
  } finally {
    console.log("-----submitForm----")
  }
}


/** 初始化 **/
onMounted(() => {
  getList()
})

/** 选中操作 */
const selectionList = ref<SaleOrderItemsVO[]>([])
const handleSelectionChange = (rows: SaleOrderItemsVO[]) => {
  selectionList.value = rows
  //console.log(selectionList.value[0].productName)
}

</script>
