<template>
    <div class="app-container">
        <div v-if="!showForm && !showDetail">
            <div class="searchPanel">
                <div class="more-search-pane">
                    <div class="search-where-container">
                        <el-form :model="queryParams" ref="queryRef" :inline="true" class="searchPanelForm">
                            <el-form-item label="发票类型:">
                                <el-select v-model="queryParams.invoice_type" placeholder="请选择" style="min-width: 30px;"
                                    clearable>
                                    <el-option v-for="dict in invoice_type" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="媒体归属:">
                                <el-select v-model="queryParams.media_ownership" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in media_ownership" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="人员规模:">
                                <el-select v-model="queryParams.personnel_size" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in personnel_size" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="主营媒体类型:">
                                <el-select v-model="queryParams.main_media_types" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in main_media_types" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="媒体质量:">
                                <el-select v-model="queryParams.media_quality" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in media_quality" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="有无合作历史:">
                                <el-select v-model="queryParams.history_cooperation" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in history_cooperation" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                            <el-form-item label="供应商级别:">
                                <el-select v-model="queryParams.supplier_level" placeholder="请选择"
                                    style="min-width: 30px;" clearable>
                                    <el-option v-for="dict in supplier_level" :key="dict.value" :label="dict.label"
                                        :value="dict.value" />
                                </el-select>
                            </el-form-item>
                        </el-form>
                    </div>
                    <div class="search-more-button">
                        <el-button v-if="!unfoldFlag" text class="foladText" @click="handleFlod">展开
                            <svg-icon icon-class="unfold" class="ml10" />
                        </el-button>
                        <el-button v-else text class="foladText" @click="handleFlod">收起
                            <svg-icon icon-class="packUp" class="ml10" />
                        </el-button>
                    </div>
                </div>
            </div>
            <div class="searchSmallPanel" v-show="unfoldFlag">
                <el-form :model="queryParams" ref="queryRef" :inline="true" class="searchSmallPanelForm">
                    <el-form-item label="供应商配合度:">
                        <el-select v-model="queryParams.supplier_cooperation_degree" placeholder="请选择"
                            style="min-width: 30px;" clearable>
                            <el-option v-for="dict in supplier_cooperation_degree" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="主营业务:">
                        <el-select v-model="queryParams.main_business" placeholder="请选择" clearable
                            style="min-width: 30px;">
                            <el-option v-for="dict in main_business" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="业务部门:">
                        <el-select v-model="queryParams.business_department" placeholder="请选择" clearable
                            style="min-width: 30px;">
                            <el-option v-for="dict in business_department" :key="dict.value" :label="dict.label"
                                :value="dict.value" />
                        </el-select>
                    </el-form-item>
                    <el-form-item label="城市:">

                    </el-form-item>
                    <el-form-item label="">

                    </el-form-item>
                    <el-form-item label="">

                    </el-form-item>
                </el-form>
            </div>

            <el-card class="mt20">
                <el-row :gutter="10" class="my_row"><el-col :span="12">
                        <el-form :model="queryParams" ref="queryRef" :inline="true" class="searchInputForm">
                            <el-form-item label="">
                                <el-input v-model="form.postCode" placeholder="请输入供应商名称/集团公司名称/联系人/联系人电话"
                                    :prefix-icon="Search" style="width: 500px;" />
                            </el-form-item>
                        </el-form>
                    </el-col>
                    <el-col :span="12" style="text-align: right;">
                        <el-button type="primary" class="primaryBtn" @click="handleQuery">查询</el-button>
                        <el-button type="primary" class="primaryBtn" @click="resetQuery">重置</el-button>
                        <el-button type="primary" class="primaryBtn" @click="handleOpenAddForm">新增</el-button>
                        <el-button type="primary" class="primaryBtn" @click="handleImport">导入</el-button>
                        <el-button type="primary" class="primaryBtn" @click="handleExport">导出</el-button>
                        <el-button type="primary" class="primaryBtn">比稿价管理</el-button>
                    </el-col>
                </el-row>

                <el-table v-loading="loading" :data="supplierList"
                    :height="unfoldFlag ? 'calc(100vh - 342px)' : '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="center" prop="supplierName" width="230">
                        <template #default="scope">
                            <span class="supplierNameLabel" @click="handleOpenDetail">{{ scope.row.supplierName }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column label="城市" align="center" prop="postCode" width="150" />
                    <el-table-column label="主营业务" align="center" prop="postCode" width="150" />
                    <el-table-column label="主营媒体类型" align="center" prop="postCode" width="150" />
                    <el-table-column label="媒体质量" align="center" prop="postCode" width="150" />
                    <el-table-column label="供应商配合度" align="center" prop="postCode" width="150" />
                    <el-table-column label="人员规模" align="center" prop="postCode" width="150" />
                    <el-table-column label="公司类型" align="center" prop="postCode" width="150" />
                    <el-table-column label="年营收金额" align="center" prop="postCode" width="150" />
                    <el-table-column label="媒体归属" align="center" prop="postCode" width="150" />
                    <el-table-column label="媒体年限" align="center" prop="postCode" width="150" />
                    <el-table-column label="有无合作历史" align="center" prop="postCode" width="150" />
                    <el-table-column label="合作折扣力度" align="center" prop="postCode" width="150" />
                    <el-table-column label="供应商联系人" align="center" prop="postCode" width="150" />
                    <el-table-column label="供应商联系电话" align="center" prop="postCode" width="150" />
                    <!-- <el-table-column label="反馈时间" align="center" prop="createTime" width="210">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column> -->
                    <el-table-column label="操作" width="180" align="center" fixed="right">
                        <template #default="scope">
                            <el-button link type="primary" @click="handleUpdate(scope.row)"
                                v-hasPermi="['problemFeedback:edit']">修改</el-button>
                            <el-button link type="primary" @click="handleDelete(scope.row)"
                                v-hasPermi="['problemFeedback:remove']">删除</el-button>
                            <el-button link type="primary" @click="handleDelete(scope.row)"
                                v-hasPermi="['problemFeedback:remove']">日志</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
                    @pagination="getList" />
            </el-card>
        </div>
        <supplier-form ref="supplierFormRef" v-if="showForm" @handleShowList="handleShowList" />
        <supplier-detail ref="supplierDetailRef" v-if="showDetail" @handleShowList="handleShowList" />
        <!-- 导入对话框 -->
        <el-dialog :title="upload.title" v-model="upload.open" width="650px" class="my_dialog" align-center
            :destroy-on-close="true" :close-on-click-modal="false">
            <el-upload ref="uploadRef" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
                <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
                <template #tip>
                    <div class="el-upload__tip text-center">
                        <!-- <div class="el-upload__tip">
                            <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据
                        </div> -->
                        <span>仅允许导入xls、xlsx格式文件。</span>
                        <!-- <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline"
                            @click="importTemplate">下载模板</el-link> -->
                    </div>
                </template>
            </el-upload>
            <template #footer>
                <div class="dialog-footer">
                    <el-button class="my-cancel-btn" @click="upload.open = false">取 消</el-button>
                    <el-button class="my-confirm-btn" type="primary" @click="submitFileForm">确 定</el-button>
                </div>
            </template>
        </el-dialog>

    </div>
</template>
<script setup name="Post">
import { getToken } from "@/utils/auth"
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import { useBackgroundStore } from '@/store/modules/background'
import otherbg from '@/assets/images/otherbg.png'

import supplierForm from "./supplierForm.vue";
import supplierDetail from "./supplierDetail.vue";

const bgStore = useBackgroundStore()
const router = useRouter()
const { proxy } = getCurrentInstance()
const { invoice_type, media_ownership, personnel_size, main_media_types, media_quality, history_cooperation, supplier_level, supplier_cooperation_degree, main_business, business_department } = proxy.useDict("invoice_type", "media_ownership", "personnel_size", "main_media_types", "media_quality", "history_cooperation", "supplier_level", "supplier_cooperation_degree", "main_business", "business_department")

const showForm = ref(false) 
const supplierFormRef = ref(null)
const showDetail = ref(false) 
const supplierDetailRef = ref(null)
/*** 导入参数 */
const upload = reactive({
    // 是否显示弹出层（用户导入）
    open: false,
    // 弹出层标题（用户导入）
    title: "",
    // 是否禁用上传
    isUploading: false,
    // 是否更新已经存在的用户数据
    updateSupport: 0,
    // 设置上传的请求头部
    headers: { Authorization: "Bearer " + getToken() },
    // 上传的地址
    url: import.meta.env.VITE_APP_BASE_API + "/system/user/importData"
})

const supplierList = ref([
    { supplierName: 'xxxx供应商' }
])
const loading = ref(true)
const total = ref(0)
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
    },
    rules: {
        postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
        postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
        postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
    }
})

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

// 折叠展开
const unfoldFlag = ref(false)
const handleFlod = () => {
    unfoldFlag.value = !unfoldFlag.value
}

/** 查询岗位列表 */
const getList = () => {
    loading.value = false
    // listPost(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    //     postList.value = response.rows
    //     total.value = response.total
    //     loading.value = false
    // })
}
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1
    getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
    proxy.resetForm("queryRef")
    handleQuery()
}
/** 删除按钮操作 */
const handleDelete = (row) => {
    const postIds = row.postId
    const supplierName = row.supplierName
    proxy.$modal.confirm('是否确认删除名称为"' + supplierName + '"的供应商数据？').then(function () {
        return delPost(postIds)
    }).then(() => {
        getList()
        proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
}
/** 导入按钮操作 */
const handleImport = () => {
    upload.title = "导入"
    upload.open = true
}
/**文件上传中处理 */
const handleFileUploadProgress = (event, file, fileList) => {
    upload.isUploading = true
}

/** 文件上传成功处理 */
const handleFileSuccess = (response, file, fileList) => {
    upload.open = false
    upload.isUploading = false
    proxy.$refs["uploadRef"].handleRemove(file)
    proxy.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true })
}
/** 提交上传文件 */
const submitFileForm = () => {
    proxy.$refs["uploadRef"].submit()
}
/** 导出按钮操作 */
const handleExport = () => {

}
// 新增
const handleOpenAddForm = () => {
    showForm.value = true
    nextTick(() => {
        supplierFormRef.value.formTitle = '新建供应商'
    })
}
// 修改
const handleUpdate = () => {
    showForm.value = true
    nextTick(() => {
        supplierFormRef.value.formTitle = '编辑供应商'
    })
}
// 详情
const handleOpenDetail = () => {
    showDetail.value = true
    nextTick(() => {
        // supplierFormRef.value.formTitle = '编辑供应商'
    })
}
const handleShowList = () => {
    console.log('触发')
    showForm.value = false
    showDetail.value = false
}
// 初始化
onMounted(() => {
    bgStore.setBgImage(otherbg)
    getList()
}); 
</script>
<style lang="scss">
.supplierNameLabel {
    font-family: Microsoft YaHei;
    font-weight: 400;
    font-size: 16px;
    color: #1A75E6;
    cursor: pointer;
}
</style>