<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="模板文件ID">
        <el-input v-model="formState.varFileId" placeholder="" clearable />
      </el-form-item>

      <el-form-item label="产品编码">
        <el-input v-model="formState.productCode" placeholder="产品编码"></el-input>
      </el-form-item>

      <el-form-item label="客户">
        <el-input v-model="formState.customerId_dict" placeholder="" readonly>
          <template #append>
            <el-button :icon="MoreFilled" @click="visibleModal = true"></el-button>
          </template>
        </el-input>
      </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 label="创建时间">
        <el-date-picker
          v-model="formState.createTime"
          type="daterange"
          value-format="YYYY-MM-DD"
          placeholder=""
        ></el-date-picker>
      </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-table
        ref="xTable"
        size="small"
        min-height="460"
        border
        :loading="loading"
        :data="tableData"
      >
        <vxe-column type="checkbox" width="50"></vxe-column>
        <vxe-column field="customerId_dictText" title="客户名称" width="120"></vxe-column>
        <vxe-column field="productCode" title="产品编码" width="120"></vxe-column>
        <vxe-column field="varFileId" title="模板文件ID" width="180">
          <template #default="{ row }">
            <el-link
              target="_blank"
              :href="'/preview-excel?fileId=' + row.varFileId + '&type=var'"
              >{{ row.varFileId }}</el-link
            >
          </template>
        </vxe-column>
        <vxe-column title="原始文件地址" min-width="300">
          <template #default="{ row }">
            <el-link
              v-for="item in row.filePath.split(';')"
              :key="item"
              target="_blank"
              :href="'/preview-excel?filePath=' + item + '&type=var'"
              >{{ item }}</el-link
            >
          </template>
        </vxe-column>
        <vxe-column title="模板文件地址" min-width="300">
          <template #default="{ row }">
            <el-link
              target="_blank"
              :href="'/preview-excel?fileId=' + row.varFileId + '&type=var'"
              >{{ row.mergeFilePath }}</el-link
            >
          </template>
        </vxe-column>
        <vxe-column field="createTime" title="创建时间" width="180"></vxe-column>
        <vxe-column field="action" title="操作" width="80" fixed="right">
          <template #default="{ row }">
            <el-link
              type="primary"
              style="font-size: 12px"
              :href="'/var-excel?fileId=' + row.varFileId"
              target="_blank"
              >查看</el-link
            >
          </template>
        </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>

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

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

const router = useRouter()
const loading = ref(false)
const formState = reactive({
  varFileId: '',
  customerId: '',
  customerId_dict: '',
  productCode:'',
  createBy: '',
  createby_dict: '',
  createTime: null,
  currentPage: 1,
  pageSize: 10,
  total: 0
  // column: 'createTime',
  // order: 'desc'
})

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

const tableData = ref([])

const onReset = () => {
  Object.assign(formState, {
    productCode:'',
    customerId: '',
    customerId_dict: '',
    varFileId: '',
    createBy: '',
    createby_dict: '',
    createTime: null
  })
}
const confirmCustomer = (list: any) => {
  const [item] = list
  if (!item) return
  formState.customerId = item.customerId
  formState.customerId_dict = item.customerName
}

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 getVarFilePage(queryParamsFilter(formState))
    tableData.value = data.result.records
    formState.total = +data.result.total
  } catch {}
  loading.value = false
}

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