import {
    getGoodsInfo,
    delGoodsInfo,
    addGoodsInfo,
    updateGoodsInfo,
    updatePicture,
    goodsInfoList,
    goodsCategory,
    editGoodsWhetherShelf,
    stationList,
} from "@/api/system/OperateGoodsInfo";
import {
    getStationCategoryList,
} from "@/api/system/goodsCategory";
import editor from "@/components/Editor";
import stationAcatar from "../stationAvatar/index.vue";
let id = 0;

export default {
    components: { stationAcatar, editor },
    name: "OperateGoodsInfo",
    data() {
        return {
            imgsurl: { pictureUrlList: [] },
            imgone: "",
            imageUrl: "", //商品图片
            value: "",
            datas: null,
            // 遮罩层
            loading: true,
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            total2: 0,
            // 商品基本信息表格数据
            goodsInfoList: [],
            // 护理站列表
            nurseStationlist: [],
            // 查询商品分类列表
            goodsCategorylist: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // open2: false,
            innerVisible: false,
            StationName: "请选择所属护理站",
            nursetotal: 0,
            nurseStationId: null,
            goods: [{
                    value: "BUSINESS",
                    label: "买卖",
                },
                // {
                //     value: "LEASE",
                //     label: "租赁",
                // },
            ],
            options: [{
                    value: 0,
                    label: "否",
                },
                {
                    value: 1,
                    label: "是",
                },
            ],
            // goodAttributeDetailsLists:[],
            goodDetailsLists: [],
            idd: 9999999,
            ids: [],
            integraloption: [{
                    value: 0,
                    label: "否",
                },
                {
                    value: 1,
                    label: "是",
                },
            ],
            // 查询参数
            shangjia: {
                id: null,
                whetherShelf: null,
            },
            queryParams: {
                id: null,
                pageNum: 1,
                pageSize: 10,
                storeInfoId: null,
                nurseStationId: null,
                goodsCategoryId: null,
                goodsName: null,
                // goodsCode: null,
                whetherShelf: null,
                shelfTime: null,
                goodsPictureUrl: null,
                goodsRemark: null,
                goodsUnit: null,
                goodsPurpose: null,
                sort: null,
            },
            //权限查询
            getListByUserquery: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {
                nurseStationId: null,
            },
            // 表单校验
            rules: {
                goodsName: [
                    { required: true, message: "请输入商品名称", trigger: "blur" },
                ],
                goodsCategoryId: [
                    { required: true, message: "请选择商品分类名称", trigger: "blur" },
                ],
                goodsPurpose: [
                    { required: true, message: "请选择商品用途", trigger: "blur" },
                ],
                goodsUnit: [
                    { required: true, message: "请输入商品度量单位", trigger: "blur" },
                ],
                goodsPictureUrl: [
                    { required: true, message: "请选择图片", trigger: "blur" },
                ],
                sort: [
                    { required: true, message: "请输入商品顺序", trigger: "blur" },

                ],
            },
            homestation: null,
            treeOption: {
                lazy: true,
                checkStrictly: true, // 是否可选择任意一级
                lazyLoad(node, resolve) {
                    const id = (node.level === 0) ? 0 : node.data.id;
                    setTimeout(() => {
                        getStationCategoryList(id).then(res => { // 接口请求
                            res.data.forEach(e => {
                                e.value = e.id
                                e.label = e.goodsCategoryName
                            })
                            resolve(res.data.map(item => {
                                return {
                                    ...item
                                }
                            }))
                        });
                    }, 300);
                },
            },
            isResouceShow: 1,
        };
    },
    created() {
        this.getList();
        // this.goodsCategoryinfo();
    },
    methods: {
        changeExchangeFlag(item) {
            if (item.integralExchangeFlag == 0) {
                item.integralExchangeCount = ''
                item.integralExchangeSill = ''
            }
        },
        imgs(item) {
            this.imgsurl.pictureUrlList.push(item);
        },
        upwhetherShelf(row) {
            if (row.whetherShelf == false) {
                var obj = {
                    id: row.goodsInfoId,
                    whetherShelf: 0,
                };
            } else if (row.whetherShelf == true) {
                var obj = {
                    id: row.goodsInfoId,
                    whetherShelf: 1,
                };
            }
            console.log(obj)
            editGoodsWhetherShelf(obj).then((res) => {
                if (obj.whetherShelf == 0) {
                    this.$modal.msgSuccess("已修改上架状态为未上架");
                } else {
                    this.$modal.msgSuccess("已修改上架状态为上架");
                }
                this.getList();
            });
        },
        /** 查询商品基本信息列表 */
        getList() {
            this.loading = true;
            goodsInfoList(this.queryParams).then((response) => {
                response.rows.forEach((e) => {
                    if (e.whetherShelf == 1) {
                        e.whetherShelf = true;
                    } else if (e.whetherShelf == 0) {
                        e.whetherShelf = false;
                    }
                });
                this.goodsInfoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 添加
        addgoodAttributeDetail() {
            // if (this.goodDetailsLists.length == 5) {
            // this.$message.error("最多只能5条");
            // } else {
            this.idd++;
            var obj = {
                attributePitureUrl: "",
                goodsPrice: "",
                goodsStock: "",
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                attributeDetailsSort: null,
                idd: this.idd,
            };
            this.goodDetailsLists.push(obj);
            // }
        },
        // 删除
        delgoodAttributeDetail(item) {
            if (item.idd && !item.attributeDetailsId) {
                if (this.goodDetailsLists.length == 1) {
                    this.$message.error("最后一条不可删除");
                } else {
                    this.goodDetailsLists = this.goodDetailsLists.filter(
                        (e) => e.idd != item.idd
                    );
                }
            } else if (!item.idd && item.attributeDetailsId) {
                if (this.goodDetailsLists.length == 1) {
                    this.$message.error("最后一条不可删除");
                } else {
                    this.goodDetailsLists = this.goodDetailsLists.filter(
                        (e) => e.attributeDetailsId != item.attributeDetailsId
                    );
                }
            }
        },
        // 取消按钮
        cancel() {
            var obj = { pictureUrlList: [] };
            if (this.imgone != this.form.goodsPictureUrl) {
                obj.pictureUrlList.push(this.form.goodsPictureUrl);
            }
            if (obj.pictureUrlList.length > 0) {
                updatePicture(obj).then((res) => {});
            }
            if (this.imgsurl.pictureUrlList.length > 0) {
                updatePicture(this.imgsurl).then((res) => {});
            }
            this.imgsurl = { pictureUrlList: [] };
            this.open = false;
            this.reset();
            this.StationName = "请选择所属护理站";
            this.goodDetailsLists = [{
                attributeName: "",
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                goodAttributeDetailsLists: [{
                    attributePitureUrl: "",
                    goodsPrice: "",
                    goodsStock: "",
                    attributeDetailsSort: null,
                }, ],
            }, ];
            ++this.isResouceShow
        },
        //页面所属护理站
        ParamsStation(item) {
            this.info();
            this.homestation = item
            this.innerVisible = true
            this.form.nurseStationId = this.queryParams.nurseStationId
        },
        //选择护理站
        clickinnerVisible() {
            this.info();
            this.innerVisible = true;
        },
        nurseclick(row) {
            if (this.homestation) {
                this.queryParams.nurseStationName = row.nurseStationName;
                this.queryParams.nurseStationId = row.id;
            } else {
                this.form.nurseStationId = row.id;
                this.StationName = row.nurseStationName;
            }
            this.innerVisible = false;
        },
        handleQuery2() {
            this.info();
        },
        resetQuery2() {
            this.resetForm("queryForm");
            this.getListByUserquery = {
                pageNum: 1,
                pageSize: 10,
            }
            this.handleQuery2();
        },
        addcancel() {
            this.innerVisible = false;
            this.resetQuery2();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                storeInfoId: null,
                nurseStationId: null,
                goodsCategoryId: null,
                goodsName: null,
                // goodsCode: null,
                whetherShelf: null,
                shelfTime: null,
                goodsPictureUrl: null,
                goodsRemark: null,
                goodsUnit: null,
                goodsPurpose: null,
                sort: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
            };
            this.goodDetailsLists = [{
                attributePitureUrl: "",
                goodsPrice: "",
                goodsStock: "",
                attributeDetailsSort: null,
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                idd: 9999999,
            }, ];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                goodsName: null,
                goodsCategoryName: null,
                goodsPurpose: null,
            };

            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.goodsInfoId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.homestation = null;
            this.nurseStationId = "";
            this.title = "商品基本信息";
            // this.nurseName = "请选择护理员";
            this.StationName = "请选择所属护理站";
            this.goodDetailsLists = [{
                attributePitureUrl: "",
                goodsPrice: "",
                goodsStock: "",
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                sort: null,
                idd: 9999999,
            }, ];
            this.open = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.homestation = null;
            const id = row.goodsInfoId || this.ids;
            this.StationName = row.nurseStationName;
            if (this.StationName == null) {
                this.StationName = "请选择所属护理站";
            }
            getGoodsInfo(id).then((response) => {
                // response.data.parentIdList = response.data.parentIdList.split(',')
                this.form = response.data;
                this.imgone = this.form.goodsPictureUrl;
                if (response.data.goodAttributeDetailsLists) {
                    this.goodDetailsLists = response.data.goodAttributeDetailsLists;
                }
                // this.form.goodsCategoryId = this.form.goodsCategoryName
                // this.form.value = this.form.goodsCategoryId
                // this.form.label = this.form.goodsCategoryName
                this.open = true;
                this.title = "修改商品基本信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.form.goodAttributeDetailsLists = [];
            this.form.goodDetailsLists = this.goodDetailsLists;
            if (!this.form.goodDetailsLists.integralExchangeFlag) {
                this.form.goodDetailsLists.integralExchangeFlag = null
            }
            if (this.form.goodDetailsLists.integralExchangeSill == "") {
                this.form.goodDetailsLists.integralExchangeSill = null

            }
            if (this.form.goodDetailsLists.integralExchangeCount == "") {
                this.form.goodDetailsLists.integralExchangeCount = null
            }

            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.goodsInfoId != null) {
                        this.form.id = this.form.goodsInfoId;
                        this.form.goodDetailsLists.forEach(e => {
                            e.goodsPrice = Number(e.goodsPrice)
                            if (e.attributeDetailsSort == '') {
                                e.attributeDetailsSort = null
                            }
                            // e.sort = e.attributeDetailsSort
                        })
                        updateGoodsInfo(this.form).then((response) => {
                            var obj = { pictureUrlList: [] };
                            if (this.imgone != this.form.goodsPictureUrl) {
                                obj.pictureUrlList.push(this.imgone);
                            }
                            this.imgsurl = { pictureUrlList: [] };
                            if (obj.pictureUrlList.length > 0) {
                                updatePicture(obj).then((res) => {});
                            }
                            if (response.code == 200) {
                                this.$modal.msgSuccess("修改成功");
                                ++this.isResouceShow
                                this.open = false;
                                this.getList();
                            }
                        });
                    } else {
                        this.form.goodDetailsLists.forEach(e => {
                                e.goodsPrice = Number(e.goodsPrice)
                            })
                            // this.nurseStationId=this.form.nurseStationId
                        addGoodsInfo(this.form).then((response) => {
                            if (response.code == 200) {
                                this.$modal.msgSuccess("新增成功");
                                this.StationName = "请选择所属护理站";
                                ++this.isResouceShow
                                this.open = false;
                                this.getList();
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$confirm("是否确认删除订单信息的数据项？", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(() => {
                    delGoodsInfo(row.goodsInfoId).then((res) => {
                        var obj = { pictureUrlList: [] };
                        obj.pictureUrlList.push(row.goodsPictureUrl);
                        if (obj.pictureUrlList.length > 0) {
                            updatePicture(obj).then((res) => {});
                        }
                        this.$message.success("删除成功");
                        this.getList();
                    });
                })
                .catch(() => {});
        },
        imgUrl(imgUrl) {
            this.form.goodsPictureUrl = imgUrl;
        },
        attributePitureUrl(imgUrl) {
            // this.form.attributePitureUrl = imgUrl;
        },
        attributePitureitem(item) {
            let items = JSON.parse(item);
            if (items.idd && !items.attributeDetailsId) {
                this.goodDetailsLists.forEach((e) => {
                    if (e.idd == items.idd) {
                        e.attributePitureUrl = items.attributePitureUrl;
                    }
                });
            } else if (!items.idd && items.attributeDetailsId) {
                this.goodDetailsLists.forEach((e) => {
                    if (e.attributeDetailsId == items.attributeDetailsId) {
                        e.attributePitureUrl = items.attributePitureUrl;
                    }
                });
            }
        },
        //商品分类
        goodsCategoryinfo() {
            getStationCategoryList(0).then((res) => {
                res.data.forEach(e => {
                    e.value = e.id
                    e.label = e.goodsCategoryName
                })
                this.goodsCategorylist = res.data;
            });
        },
        change(e) {
            this.form.goodsCategoryId = e[e.length - 1]
        },
        //护理站
        info() {
            stationList(this.getListByUserquery).then((res) => {
                this.nurseStationlist = res.rows;
                this.total2 = res.total;
            });
        },
    },
};
