<template>
    <div class="app-container">
        <div class="searchPanel">
            <el-form :inline="true" class="searchPanelForm">
                <el-form-item label="供应商:">
                    <el-select class="filterSelect" v-model="queryParams.supplierId" filterable remote reserve-keyword
                        :remote-method="getSupplierList" :loading="selectLoading" placeholder="请输入" remote-show-suffix
                        clearable style="min-width: 70px;">
                        <el-option v-for="item in supplierList" :key="item.supplierId" :label="item.supplierName"
                            :value="item.supplierId" />
                    </el-select>
                </el-form-item>
                <el-form-item label="类型:">
                    <el-select v-model="queryParams.type" placeholder="请选择" style="min-width: 30px;"
                        @change="handleChangeType" clearable>
                        <el-option v-for="item in types" :key="item.value" :label="item.label" :value="item.value" />
                    </el-select>
                </el-form-item>
            </el-form>
        </div>
        <el-card class="mt20">
            <el-row :gutter="10" class="my_row">
                <el-col :span="24">
                    <el-button type="primary" class="primaryBtn" v-hasPermi="['pitchPrice:query']" @click="handleQuery">查询</el-button>
                    <el-button type="primary" class="primaryBtn" v-hasPermi="['pitchPrice:reset']" @click="resetQuery">重置</el-button>
                    <el-button type="primary" class="primaryBtn" v-hasPermi="['pitchPrice:add']" @click="handleAdd">新建</el-button>
                    <el-button type="primary" class="primaryBtn" v-hasPermi="['pitchPrice:import']" @click="handleImport">导入</el-button>
                </el-col>
            </el-row>

            <el-table v-if="queryParams.type == 1" v-loading="loading" :data="pitchPriceList"
                min-height="calc(100vh - 308px)">
                <el-table-column label="序号" align="center" width="80">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="供应商" align="left" prop="supplierName" width="180"
                    :show-overflow-tooltip="true" />
                <el-table-column label="城市" align="center" prop="city" width="130" />
                <el-table-column label="场站" align="center" prop="station" width="100" />
                <el-table-column label="媒体位置" align="center" prop="mediaPlacement" min-width="260"
                    :show-overflow-tooltip="true" />
                <el-table-column label="频次" align="center" prop="frequency" width="150" :show-overflow-tooltip="true" />
                <el-table-column label="刊例价" align="center" prop="subscriptionPrice" width="120" />
                <el-table-column label="单位" align="center" prop="unit" width="100" />
                <el-table-column label="折扣" align="center" prop="discount" width="100" />
                <el-table-column label="首次制作安装费(元/次)" align="center" prop="firstMakeFee" min-width="230" />
                <el-table-column label="换画费(RMB)" align="center" prop="frameChangeFee" width="150" />
                <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" @click="handleUpdate(scope.row)"
                            v-hasPermi="['pitchPrice:edit']">修改</el-button>
                        <el-button link type="primary" @click="handleDelete(scope.row)"
                            v-hasPermi="['pitchPrice:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-table v-if="queryParams.type == 2" v-loading="loading" :data="pitchPriceList"
                height="calc(100vh - 308px)">
                <el-table-column label="序号" align="center" width="80">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="供应商" align="left" prop="supplierName" width="180"
                    :show-overflow-tooltip="true" />
                <el-table-column label="城市" align="center" prop="city" width="130" />
                <el-table-column label="资源描述" align="center" prop="resourceDesc" min-width="160"
                    :show-overflow-tooltip="true" />
                <el-table-column label="刊例价" align="center" prop="subscriptionPrice" width="130" />
                <el-table-column label="单位" align="center" prop="unit" width="100" />
                <el-table-column label="折扣" align="center" prop="discount" width="100" />
                <el-table-column label="首次制作安装费(元/次)" align="center" prop="firstMakeFee" min-width="230" />
                <el-table-column label="换画费(RMB)" align="center" prop="frameChangeFee" width="150" />
                <el-table-column label="备注" align="center" prop="remark" min-width="230" />
                <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" @click="handleUpdate(scope.row)"
                            v-hasPermi="['pitchPrice:edit']">修改</el-button>
                        <el-button link type="primary" @click="handleDelete(scope.row)"
                            v-hasPermi="['pitchPrice:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-table v-if="queryParams.type == 3 || queryParams.type == 4" v-loading="loading" :data="pitchPriceList"
                height="calc(100vh - 308px)">
                <el-table-column label="序号" align="center" width="80">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="供应商" align="left" prop="supplierName" width="180"
                    :show-overflow-tooltip="true" />
                <el-table-column label="城市" align="center" prop="city" width="130" />
                <el-table-column label="媒体名称" align="center" prop="mediaName" min-width="260"
                    :show-overflow-tooltip="true" />
                <el-table-column label="频次" align="center" prop="frequency" width="150" :show-overflow-tooltip="true" />
                <el-table-column label="刊例价" align="center" prop="subscriptionPrice" width="120" />
                <el-table-column label="单位" align="center" prop="unit" width="100" />
                <el-table-column label="折扣" align="center" prop="discount" width="100" />
                <el-table-column label="首次制作安装费(元/次)" align="center" prop="firstMakeFee" min-width="230" />
                <el-table-column label="换画费(RMB)" align="center" prop="frameChangeFee" width="150" />
                <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button link type="primary" @click="handleUpdate(scope.row)"
                            v-hasPermi="['pitchPrice:edit']">修改</el-button>
                        <el-button link type="primary" @click="handleDelete(scope.row)"
                            v-hasPermi="['pitchPrice:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination :total="total" v-model:page="queryParams.pageIndex" v-model:limit="queryParams.pageSize"
                @pagination="getPitchPricePage" />
        </el-card>

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" v-model="open" width="850px" class="my_dialog" align-center :destroy-on-close="true"
            :close-on-click-modal="false">
            <el-form ref="pitchPriceRef" :model="form" :rules="rules" label-width="140px" class="myInsertForm">
                <el-row :gutter="20">
                    <el-col :span="24">
                        <el-form-item label="类型" prop="type">
                            <el-select v-model="form.type" placeholder="请选择类型" style="min-width: 30px;" clearable>
                                <el-option v-for="item in types" :key="item.value" :label="item.label"
                                    :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="供应商" prop="busSupplierId">
                            <el-select class="filterSelect" v-model="form.busSupplierId" filterable remote
                                reserve-keyword :remote-method="getSupplierList" :loading="selectLoading"
                                placeholder="请输入" remote-show-suffix clearable>
                                <el-option v-for="item in supplierList" :key="item.supplierId"
                                    :label="item.supplierName" :value="item.supplierId" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="城市" prop="city">
                            <el-input v-model="form.city" placeholder="请输入城市" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.type == 1">
                        <el-form-item label="场站" prop="station">
                            <el-input v-model="form.station" placeholder="请输入场站" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.type == 1">
                        <el-form-item label="媒体位置" prop="mediaPlacement">
                            <el-input v-model="form.mediaPlacement" placeholder="请输入媒体位置" clearable />
                        </el-form-item>
                    </el-col>
                     <el-col :span="24" v-if="form.type == 3 || form.type == 4">
                        <el-form-item label="媒体名称" prop="mediaName">
                            <el-input v-model="form.mediaName" placeholder="请输入媒体名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.type == 2">
                        <el-form-item label="资源描述" prop="resourceDesc">
                            <el-input v-model="form.resourceDesc" placeholder="请输入资源描述" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.type == 1 || form.type == 3 || form.type == 4">
                        <el-form-item label="频次" prop="frequency">
                            <el-input v-model="form.frequency" placeholder="请输入频次" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="单位" prop="unit">
                            <el-input v-model="form.unit" placeholder="请输入单位" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="刊例价" prop="subscriptionPrice">
                            <el-input v-model="form.subscriptionPrice" placeholder="请输入刊例价" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="折扣" prop="discount">
                            <el-input v-model="form.discount" placeholder="请输入折扣" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="首次制作安装费" prop="firstMakeFee">
                            <el-input v-model="form.firstMakeFee" placeholder="请输入首次制作安装费" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="换画费" prop="frameChangeFee">
                            <el-input v-model="form.frameChangeFee" placeholder="请输入换画费" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" v-if="form.type == 2">
                        <el-form-item label="备注" prop="remark">
                            <el-input v-model="form.remark" :rows="5" type="textarea" placeholder="请输入备注" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button class="my-cancel-btn" @click="cancel">取 消</el-button>
                    <el-button class="my-confirm-btn" type="primary" @click="submitForm">确 定</el-button>
                </div>
            </template>
        </el-dialog>
        <!-- 导入对话框 -->
        <el-dialog title="导入" v-model="uploadOpen" width="650px" class="my_dialog" align-center :destroy-on-close="true"
            :close-on-click-modal="false">
            <el-upload ref="uploadRef" class="upload-demo" drag action="#" :http-request="requestDocUpload"
                :file-list="docUploadList" :before-upload="beforeDocUpload" :on-remove="removeDocUpload">
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="el-upload__text">
                    将文件拖曳至此区域，或 <em>点击上传</em>
                </div>
                <!-- <div class="el-upload__text">
                    支持扩展名：.xlsx, .xls
                </div> -->
                <template #tip>
                    <div class="el-upload__tip text-center">
                        <span>仅允许导入xls、xlsx格式文件。</span>
                        <el-dropdown placement="bottom-start" style="margin-left: 12px;margin-top: 4px;">
                            <el-link type="primary" underline="never"
                                style="font-size: 12px; vertical-align: baseline">下载示例模板</el-link>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item class="dropItem"
                                        @click="importTemplate('gt')">下载高铁比稿价模板</el-dropdown-item>
                                    <el-dropdown-item class="dropItem"
                                        @click="importTemplate('hct')">下载候车厅比稿价模板</el-dropdown-item>
                                    <el-dropdown-item class="dropItem"
                                        @click="importTemplate('mj')">下载门禁比稿价模板</el-dropdown-item>
                                    <el-dropdown-item class="dropItem"
                                        @click="importTemplate('dz')">下载道闸比稿价模板</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>

                    </div>
                </template>
            </el-upload>
            <template #footer>
                <div class="dialog-footer">
                    <el-button class="my-cancel-btn" @click="uploadOpen = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Post">
import { onMounted, ref } from 'vue';
import { listBusSupplier } from "@/api/supplier"
import { busSupplierComparePricePage, addBusSupplierComparePrice, updateBusSupplierComparePrice, deleteBusSupplierComparePrice, getBusSupplierComparePrice, importSupplierComparePrice } from "@/api/pitchPrice"
import { useBackgroundStore } from '@/store/modules/background'
import otherbg from '@/assets/images/otherbg.png'
const bgStore = useBackgroundStore()

const { proxy } = getCurrentInstance()

// 供应商数据
const supplierList = ref([])
// 1-高铁 2-候车亭 3-门禁道闸
const types = ref([
    { value: 1, label: '高铁' },
    { value: 2, label: '候车厅' },
    { value: 3, label: '门禁' },
    { value: 4, label: '道闸' },
])

const title = ref('新建')
const open = ref(false)
const uploadOpen = ref(false)
const docUploadList = ref([])
const loading = ref(true)
const selectLoading = ref(false)
const total = ref(0)
const pitchPriceList = ref([])
const data = reactive({
    form: {},
    queryParams: {
        pageIndex: 1,
        pageSize: 10,
        supplierId: undefined,
        type: 1 //1-高铁 2-候车亭 3-门禁道闸
    },
    rules: {
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        busSupplierId: [{ required: true, message: "供应商不能为空", trigger: "change" }],
        city: [{ required: true, message: "城市不能为空", trigger: "blur" }],
        station: [{ required: true, message: "场站不能为空", trigger: "blur" }],
        mediaPlacement: [{ required: true, message: "媒体位置不能为空", trigger: "blur" }],
        mediaName: [{ required: true, message: "媒体名称不能为空", trigger: "blur" }],
        frequency: [{ required: true, message: "频次不能为空", trigger: "blur" }],
        resourceDesc: [{ required: true, message: "资源描述不能为空", trigger: "blur" }],
        subscriptionPrice: [{ required: true, message: "刊例价不能为空", trigger: "blur" }],
        discount: [{ required: true, message: "折扣不能为空", trigger: "blur" }],
        unit: [{ required: true, message: "单位不能为空", trigger: "blur" }],
        firstMakeFee: [{ required: true, message: "首次制作安装费不能为空", trigger: "blur" }],
        frameChangeFee: [{ required: true, message: "换画费不能为空", trigger: "blur" }]
    }
})

const { queryParams, form, rules } = toRefs(data)

// 查询条件获取供应商
const getSupplierList = (queryText) => {
    if (queryText) {
        selectLoading.value = true
        listBusSupplier({ keyword: queryText }).then(res => {
            supplierList.value = res.data
            selectLoading.value = false
        })
    } else {
        selectLoading.value = false
        supplierList.value = []
    }
}

// 获取比稿价分页列表
const getPitchPricePage = () => {
    loading.value = true
    busSupplierComparePricePage(queryParams.value).then(res => {
        pitchPriceList.value = res.data.list
        total.value = res.data.total
        loading.value = false
    })
}

// 类型切换
const handleChangeType = (val) => {
    queryParams.value.type = val
    pitchPriceList.value = []
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageIndex = 1
    getPitchPricePage()
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryParams.value.pageIndex = 1
    handleQuery()
}

// 新建
const handleAdd = () => {
    title.value = '新建'
    form.value.type = 1
    open.value = true
}

// 修改问题
const handleUpdate = (row) => {
    getBusSupplierComparePrice(row.id).then(res => {
        form.value = res.data
        if (form.value.busSupplierId) {
            listBusSupplier({}).then(res => {
                supplierList.value = res.data
            })
        }
        open.value = true
    })
}

const cancel = () => {
    open.value = false
    form.value = {}
}

const submitForm = () => {
    proxy.$refs["pitchPriceRef"].validate(valid => {
        if (valid) {
            if (form.value.id != undefined) {
                updateBusSupplierComparePrice(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功")
                    cancel()
                    getPitchPricePage()
                })
            } else {
                addBusSupplierComparePrice(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功")
                    cancel()
                    getPitchPricePage()
                })
            }
        }
    })
}

/** 删除按钮操作 */
const handleDelete = (row) => {
    proxy.$modal.confirm('是否确认删除该比稿价格吗？').then(function () {
        return deleteBusSupplierComparePrice(row.id)
    }).then(() => {
        getPitchPricePage()
        proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
}

// 导入
const handleImport = () => {
    uploadOpen.value = true
}
// 自定义上传文件资料
const requestDocUpload = (options) => {
    proxy.$modal.loading('正在上传文件，请耐心等待...')
    const { file } = options
    var formData = new FormData();
    formData.append('file', file);
    importSupplierComparePrice(formData).then(res => {
        if (res.code == 200) {
            proxy.$modal.msgSuccess("导入成功")
            proxy.$modal.closeLoading()
            uploadOpen.value = false
            getPitchPricePage()
        } else {
            proxy.$modal.closeLoading()
            proxy.$modal.msgError(res.msg);
        }
    })
}
//自定义上传文件资料校验
const beforeDocUpload = (file) => {
    const type = [
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
        'application/vnd.ms-excel'
    ];
    const isXlsx = type.includes(file.type);
    // 检验文件格式
    if (!isXlsx) {
        proxy.$modal.msgError("文件格式错误，请上传.xls；.xlsx后缀的文件。");
        return false;
    }
}
// 移除已上传文件列表
const removeDocUpload = (file, fileList) => {
    docUploadList.value = docUploadList.value.filter(
        item => item.name != file.name
    );
}
// 下载导入示例模板操作
const importTemplate = (_type) => {
    const link = document.createElement('a')
    switch (_type) {
        case 'gt':
            link.href = '/高铁比稿价导入示例模板.xlsx'
            link.download = '高铁比稿价导入示例模板.xlsx' // 设置下载文件名 
            break;
        case 'hct':
            link.href = '/候车厅比稿价导入示例模板.xlsx'
            link.download = '候车厅比稿价导入示例模板.xlsx' // 设置下载文件名 
            break;
        case 'mj':
            link.href = '/门禁比稿价导入示例模板.xlsx'
            link.download = '门禁比稿价导入示例模板.xlsx' // 设置下载文件名 
            break;
        case 'dz':
            link.href = '/道闸比稿价导入示例模板.xlsx'
            link.download = '道闸比稿价导入示例模板.xlsx' // 设置下载文件名 
            break;
        default:
            break;
    }
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}
// 初始化
onMounted(() => {
    bgStore.setBgImage(otherbg)
    getPitchPricePage()
}); 
</script>
