index.vue 9.45 KB
<template>
  <VenusDialog v-model="showDialog"
               height="700px"
               title="选择产品"
               width="70%"
               destroy-on-close
               draggable
               :close-on-click-modal="false"
               :confirm-loading="dialog.confirmLoading"
               @confirm="handleSelectGoodsConfirm">
    <div v-if="showDialog"
         v-loading="loading"
         class="selected-product-wrapper">
      <d2-search class="show-search"
                 label-width="100px"
                 :form-data="table.formSchema"
                 :search-form="table.form">
        <template #commodityCode>
          <el-input v-model.trim="table.form.commodityCode"
                    placeholder="请输入商品ID"
                    :clearable="true"
                    :maxlength="50"
                    :style="{ width: '100%' }"
                    @input="event => searchInput(event, 'commodityCode')" />
        </template>

        <template #type>
          <el-input v-model.trim="table.form.type"
                    placeholder="请输入商品编码"
                    :clearable="true"
                    :maxlength="50"
                    :style="{ width: '100%' }" />
        </template>

        <template #thirdPartyCode>
          <el-input v-model.trim="table.form.thirdPartyCode"
                    placeholder="请输入第三方编码"
                    :clearable="true"
                    :maxlength="50"
                    :style="{ width: '100%' }"
                    @input="event => searchInput(event, 'thirdPartyCode')" />
        </template>

        <template #baseGoodsCommodityCode>
          <el-input v-model.trim="table.form.baseGoodsCommodityCode"
                    placeholder="请输入系列ID"
                    :clearable="true"
                    :maxlength="50"
                    :style="{ width: '100%' }"
                    @input="event => searchInput(event, 'baseGoodsCommodityCode')" />
        </template>

        <template #goodsCascader>
          <el-cascader v-model="table.form.tempGoodsType"
                       placeholder="请选择"
                       style="width: 100%;"
                       clearable
                       filterable
                       :options="options.productTypeList"
                       :props="{ checkStrictly: true, label: 'categoryName', value: 'categoryType' }"
                       @change="handleGoodsCascaderChange($event)">
          </el-cascader>
        </template>
      </d2-search>
      <div class="up-btns">
        <el-button :id="table.props.btnId"
                   size="small"
                   type="primary">
          查询
        </el-button>
      </div>
      <d2-table ref="tableRef"
                class="table-wrapper"
                :before-search-fn="beforeSearchFn"
                :get-list-api="table.api"
                :has-num="false"
                :search-form="table.form"
                :table-params="table.props"
                @setTableFinishFun="setTableFinishFun">
        <template #upgradeGoodsAreaList="{ scope }">
          <el-tooltip v-if="Array.isArray(scope.upgradeGoodsAreaList) && scope.upgradeGoodsAreaList.length" placement="top">
            <template #content>
              <div class="car-type-list">
                <div v-for="(item, index) in scope.upgradeGoodsAreaList" :key="index">
                  {{ item.provinceName === '全国' ? '全国' : `${item.provinceName}-${item.cityName}` }}
                </div>
              </div>
            </template>
            <div class="text-hide" v-text="getUpgradeGoodsAreaList(scope.upgradeGoodsAreaList)">
            </div>
          </el-tooltip>
          <div v-else>
            -
          </div>
        </template>
        <template #single-button="scope">
          <el-radio v-model="selectedId"
                    :label="scope.scope.commodityCode"
                    @change="handleSelectedRow(scope.scope)">
            {{ '' }}
          </el-radio>
        </template>
        <template #carName="scope">
          <el-tooltip v-if="scope.scope.carTypeConfigList && scope.scope.carTypeConfigList.length" placement="top">
            <template #content>
              <div class="car-type-list">
                <div v-for="(item, index) in scope.scope.carTypeConfigList" :key="index">
                  {{ item.seriesName }}
                </div>
              </div>
            </template>
            <div>{{ formatCar(scope.scope.carTypeConfigList) }}</div>
          </el-tooltip>
          <div v-else>
            -
          </div>
        </template>
      </d2-table>
    </div>
  </VenusDialog>
</template>

<script>
import { getAllCategory } from "@api/product";
import { getUpgradeProductList } from "@api/productManage";
import { isArray } from "lodash-es";
import { getOperationCost } from "@/api/grounding";
import VenusDialog from "@/components/venusDialog/index.vue";
import { setAllOptions } from "@/libs/select";
import { baseConfig } from "@/main";
import { formatCar } from "@/views/goods/shelfProduct/utils/format.js";
import { isQualityUpgrade } from "@/views/goods/shelfProduct/utils/status";
import { getSelectGoodsTableFilterSchema, selectGoodsTableSchema } from "./selectProductDialog.data.js";

export default {
	name: "SelectGoodsDialog",
	components: {
		VenusDialog,
	},
	props: {
		title: {
			type: String,
			default: "选择商品",
		},
		// 已选的activeId
		activeId: {
			type: [Number, String],
			default: undefined,
		},
		//  服务商列表
		merchantIdList: {
			type: Array,
			default: () => ([]),
		},
		//   业务场景
		baseSceneCode: {
			type: String,
			required: true,
		},
		show: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			table: {
				formSchema: [],
				form: {
					upgradeGoodsName: "",
					commodityCode: "",
					specificationData: "",
					type: "",
					thirdPartyCode: "",
					carTypeSearch: "",
					goodsName: "",
					baseGoodsCommodityCode: "",
					tempGoodsType: [],
				},
				props: selectGoodsTableSchema,
				api: getUpgradeProductList,

			},

			loading: false,
			dialog: {
				confirmLoading: false,
			},
			selectedRow: undefined,
			selectedId: undefined,
			options: {
				productTypeList: [],
			},
		};
	},
	computed: {
		showDialog: {
			get() {
				return this.show;
			},
			set(val) {
				this.$emit("update:show", val);
			},

		},
	},
	async mounted() {
		this.loading = true;
		try {
			this.selectedId = this.activeId;
			await this.getSelectedRow();
			// this.keyDowmGetList 不知道在哪里设置的全局变量.  原有组件中需要传入
			this.table.formSchema = getSelectGoodsTableFilterSchema(this.keyDowmGetList);
			await this.getOptionsList();
		} finally {
			this.loading = false;
		}
	},
	methods: {
		formatCar,
		getUpgradeGoodsAreaList(areaList) {
			if (!isArray(areaList)) {
				return "-";
			}

			if (areaList.length === 0) {
				return "-";
			}
			const _areaList = areaList.map((item) => {
				if (item.provinceName === "全国") {
					return item.provinceName;
				}
				return `${item.provinceName}-${item.cityName}`;
			});
			return _areaList.join(",");
		},
		handleSelectedRow(row) {
			this.$set(this, "selectedRow", row);
			this.selectedId = row.commodityCode;
		},
		// 初始化 通过id 获取详情
		async getSelectedRow() {
			if (!this.activeId) {
				return;
			}
			const { list } = await getUpgradeProductList({
				commodityCode: this.activeId,
				pageSize: 1,
				currentPage: 1,
			});
			if (list && list.length) {
				this.$set(this, "selectedRow", list[0]);
			}
		},
		searchInput(e, key) {
			if (!/^[a-z0-9]*$/i.test(e)) {
				this.table.form[key] = e.replace(/[^a-z0-9]/gi, "");
			}
		},
		async getOptionsList() {
			// 获取全部类目 屏蔽服务商自建类目
			const res = await getAllCategory({ categoryTypeList: baseConfig.selfBuildGoodsType, pageSize: 9999, currentPage: 1 });
			this.options.productTypeList = res?.list || [];
			setAllOptions(this.table.formSchema);
		},
		setTableFinishFun(data) {
			this.tableData = data;
		},
		handleGoodsCascaderChange(value) {
			this.table.form.goodsType = value?.[0];
			this.table.form.secondaryGoodsType = value?.[1];
			this.table.form.thirdLevelGoodsType = value?.[2];
		},
		async beforeSearchFn() {
			const filterParams = baseConfig.equitySceneType.concat(baseConfig.extendedPosiType) || [];
			// 精品升级 excludeGoodsTypeList
			// 会员权益 goodsTypeList
			const searchKey = isQualityUpgrade(this.baseSceneCode) ? "excludeGoodsTypeList" : "goodsTypeList";
			this.table.form[searchKey] = filterParams;
			this.table.form.merchantIds = this.merchantIdList;
		},
		async handleSelectGoodsConfirm() {
			if (!this.selectedId) {
				return this.$message.warning("请选择商品");
			}
			const { operationCost } = await getOperationCost({
				commodityCode: this.selectedRow.commodityCode,
				merchantIdList: this.merchantIdList,
			});
			this.$emit("confirm", {
				...this.selectedRow,
				operationCost,
			});
		},
		handleReset() {
			this.table.form = {
				upgradeGoodsName: "",
				commodityCode: "",
				specificationData: "",
				type: "",
				thirdPartyCode: "",
				carTypeSearch: "",
				goodsName: "",
				baseGoodsCommodityCode: "",
				tempGoodsType: [],
			};
			// 重新查询
			this.$refs.tableRef.refreshTable(true, true);
		},
	},
};
</script>

<style lang="scss" scoped>
.car-type-list {
	max-height: 300px;
	overflow-y: scroll;
}

.show-search {
	::v-deep .d2-search-block {
		padding: 0;
	}
}
.table-wrapper {
	::v-deep .el-table .cell {
		padding: 6px 8px;
	}
}
</style>