<template>
  <div class="w-full h-full bg-white p-4">
    <p class="font-bold text-lg leading-10 border-b-2">订单变量文件</p>
    <el-form class="pt-4" :inline="true" :model="formState">
      <el-form-item label="客户名称">
        <el-input v-model="formState.customerName" placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item label="产品编码">
        <el-input v-model="formState.productCode" placeholder="产品编码"></el-input>
      </el-form-item>
      <el-form-item label="订单文件ID">
        <el-input v-model="formState.orderVarFileId" placeholder="产品编码"></el-input>
      </el-form-item>

      <el-form-item label="模板文件ID">
        <el-input v-model="formState.templateFileId" placeholder="产品编码"></el-input>
      </el-form-item>

      <el-form-item label="订单编码">
        <el-input v-model="formState.orderCode" placeholder="订单编码"></el-input>
      </el-form-item>
      <el-form-item label="订单项次">
        <el-input v-model="formState.orderItemNo" placeholder="订单项次"></el-input>
      </el-form-item>
      <el-form-item label="订单状态">
        <el-select v-model="formState.orderFileStatus" placeholder="" clearable>
          <template v-for="item in statusOptions">
            <el-option :value="item.value" :label="item.label"></el-option>
          </template>
        </el-select>
      </el-form-item>
<!-- 
      <el-form-item label="创建人">
        <el-input v-model="formState.createby_dict" placeholder="">
          <template #append>
            <el-button :icon="MoreFilled" @click="visibleUserModal = true"></el-button>
          </template>
        </el-input>
      </el-form-item> -->


      <el-form-item>
        <el-button type="default" @click="onReset">重置</el-button>
        <el-button type="primary" @click="onQuery">查询</el-button>
      </el-form-item>
    </el-form>

    <div class="">

      <vxe-toolbar>
        <template #buttons>
          <el-button type="danger" @click="removeRows">删除</el-button>
        </template>
      </vxe-toolbar>

      <vxe-table
        ref="xTable"
        size="small"
        min-height="460"
        border
        :column-config="{
          resizable: true
        }"
        :loading="loading"
        :data="tableData"
      >
        <vxe-column type="checkbox" width="50"></vxe-column>
        <vxe-column field="customerName" title="客户名称" width="180"></vxe-column>
        <vxe-column field="productCode" title="产品编码" width="120"></vxe-column>
        <vxe-column field="orderNo" title="订单编码" width="120"></vxe-column>
        <vxe-column field="rowNo" title="订单项次" width="100"></vxe-column>
        <vxe-column field="clientCmd" title="客户指令号" width="150"></vxe-column>
        <vxe-column field="orderVarFileId" title="订单变量文件ID" width="180">
          <template #default="{ row }">
            <el-link
              target="_blank"
              :href="'/preview-excel?filePath=' + row.mergeFilePath + '&type=var'"
              >{{ row.orderVarFileId }}</el-link>
          </template>
        </vxe-column>
        <vxe-column title="模板文件ID" min-width="180">
          <template #default="{ row }">
            <el-link
              target="_blank"
              :href="'/preview-excel?fileId=' + row.templateFileId + '&type=var'"
              >{{ row.templateFileId }}</el-link
            >
          </template>
        </vxe-column>
        <vxe-column field="orderFileStatus_dictText" title="订单状态" width="120"></vxe-column>

        <vxe-column field="createTime" title="创建时间" width="180"></vxe-column>
       
      </vxe-table>

      <vxe-pager
        size="small"
        background
        v-model:current-page="formState.currentPage"
        v-model:page-size="formState.pageSize"
        :total="formState.total"
        @page-change="onQuery"
        :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'Sizes', 'FullJump', 'Total']"
      >
      </vxe-pager>
    </div>

    <UserModal v-model:visible="visibleUserModal" @confirm="confirmUser" />
  </div>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import { getVarOrderFilePage ,deleteOrderFiles} from '@/api/var'
import { ElMessage } from 'element-plus'
import type { VxeTableInstance } from 'vxe-table'
import { apiDictList } from '@/api/common'
import { useRouter } from 'vue-router'
import { queryParamsFilter } from '@/utils/query'
import { MoreFilled } from '@element-plus/icons-vue'
import UserModal from '@/components/UserModal/UserModal.vue'

const router = useRouter()
const loading = ref(false)
const formState = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0,
  productCode: '',
  customerName: '',
  orderCode: '',
  createBy:'',
  createby_dict:'',
  orderItemNo: '',
  orderFileStatus: '',
  templateFileId:'',
  orderVarFileId:''
// column: 'createTime',
  // order: 'desc'
})

const visibleUserModal = ref(false)
const visibleModal = ref(false)
const xTable = ref<VxeTableInstance>()
  const statusOptions = ref([])

const queryDict = async (code: string) => {
  const { data } = await apiDictList({
    dictCodes: code
  })
  statusOptions.value = data.result[code]
}
const tableData = ref([])

const onReset = () => {
  Object.assign(formState, {
    productCode: '',
    customerName: '',
    orderCode: '',
    createBy:'',
    createby_dict:'',
    orderItemNo: '',
    orderFileStatus:'',
    templateFileId:'',
    orderVarFileId:''
  })
}


const removeRows = async () => {
  const list = xTable.value?.getCheckboxRecords()
  if (list?.length) {
    const ids = list.map((v) => v.orderVarFileId).join(',')
    await deleteOrderFiles(ids)
    ElMessage.success('删除成功')
    onQuery()
  }
}

const confirmUser = (list: any) => {
  const [user] = list
  if (user) {
    formState.createBy = user.userId
    formState.createby_dict = user.nickName
  }
}
const onQuery = async () => {
  loading.value = true
  try {
    const { data } = await getVarOrderFilePage(queryParamsFilter(formState))
    tableData.value = data.result.records
    formState.total = +data.result.total
  } catch {}
  loading.value = false
}

onMounted(() => {
  onQuery()
  queryDict('order_file_status')
})
</script>
<style lang="scss" scoped></style>
