import {
    getGoodsInfo,
    delGoodsInfo,
    addGoodsInfo,
    updateGoodsInfo,
    updatePicture,
    goodsInfoList,
    goodsCategory,
    editGoodsWhetherShelf,
} from "@/api/system/goodsInfo";
import editor from "@/components/Editor";
import { getListByUser } from "@/api/system/userlist.js";
import stationAcatar from "../stationAvatar/index.vue";
import baseurl from "@/api/baseurl.js";

export default {
    components: { stationAcatar, editor },
    name: "OperateGoodsInfo",
    data() {
        return {
            imgone: "",
            imageUrl: "", //商品图片
            value: "",
            datas: null,
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            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: "是",
                },
            ],
            goodDetailsLists: [],
            idd: 9999999,
            // 查询参数
            shangjia: {
                id: null,
                whetherShelf: null,
            },
            getListByUserquery: {
                pageNum: 1,
                pageSize: 10,
            },
            queryParams: {
                id: null,
                pageNum: 1,
                pageSize: 10,
                storeInfoId: null,
                nurseStationId: null,
                goodsCategoryId: null,
                goodsName: null,
                whetherShelf: null,
                shelfTime: null,
                goodsPictureUrl: null,
                goodsRemark: null,
                goodsUnit: null,
                goodsPurpose: null,
                sort: null,
                // mame:null,
            },
            //权限查询
            getListByUserquery: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {
                nurseStationName: "",
                nurseStationId: "",
            },
            // 表单校验
            rules: {
                goodsName: [
                    { required: true, message: "请输入商品名称", trigger: "blur" },
                ],
                goodsCategoryId: [
                    { required: true, message: "请选择商品分类名称", trigger: "blur" },
                ],
                nurseStationId: [
                    { required: true, message: "请选择所属护理站", trigger: "blur" },
                ],
                goodsPurpose: [
                    { required: true, message: "请选择商品用途", trigger: "blur" },
                ],
                goodsUnit: [
                    { required: true, message: "请输入商品度量单位", trigger: "blur" },
                ],
                goodsPictureUrl: [
                    { required: true, message: "请输入选择图片", trigger: "blur" },
                ],
            },
            stationid: null,
        };
    },
    created() {
        this.getList();
        this.info();
    },
    methods: {
        addcancel() {
            this.innerVisible = false
        },
        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,
                };
            }
            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: "",
                    sort: "",
                    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) => {});
            }
            this.open = false;
            this.reset();
            this.StationName = "请选择所属护理站";
            this.goodDetailsLists = [{
                attributeName: "",
                goodAttributeDetailsLists: [{
                    attributePitureUrl: "",
                    goodsPrice: "",
                    goodsStock: "",
                    sort: "",
                }, ],
            }, ];
        },
        //护理站页面选择护理站
        nurseclick(item) {
            this.queryParams.nurseStationName = item.nurseStationName;
            this.queryParams.nurseStationId = item.id;
            this.stationid = item.id
            this.innerVisible = false;
        },
        //页面所属护理站
        ParamsStation(item) {
            this.title = '选择护理站'
            this.innerVisible = true
            this.stationid = this.queryParams.nurseStationId;
        },
        //护理站列表
        info() {
            goodsCategory().then((res) => {
                this.goodsCategorylist = res.rows;
            });
            getListByUser(this.getListByUserquery).then((res) => {
                this.nurseStationlist = res.rows;
                this.total2 = res.total;
            });
        },
        handleQuery2() {
            this.info();
        },
        resetQuery2() {
            this.resetForm("queryForm");
            this.getListByUserquery = {
                pageNum: 1,
                pageSize: 10,
            }
            this.handleQuery2();
        },
        // 表单重置
        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: "",
                sort: "",
                idd: 9999999,
            }, ];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
            };
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.goodsInfoId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.form.nurseStationName = this.nurseStationlist[0].nurseStationName;
            this.form.nurseStationId = this.nurseStationlist[0].id;
            this.title = "商品基本信息";
            this.StationName = "请选择所属护理站";
            this.goodDetailsLists = [{
                attributePitureUrl: "",
                goodsPrice: "",
                goodsStock: "",
                sort: "",
                idd: 9999999,
            }, ];
            this.open = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.goodsInfoId || this.ids;
            getGoodsInfo(id).then((response) => {
                this.form = response.data;
                this.form.nurseStationName = row.nurseStationName;
                this.imgone = this.form.goodsPictureUrl;
                if (response.data.goodAttributeDetailsLists) {
                    this.goodDetailsLists = response.data.goodAttributeDetailsLists;
                }
                this.open = true;
                this.title = "修改商品基本信息";
            });
        },

        /** 提交按钮 */
        submitForm() {
            this.form.goodAttributeDetailsLists = [];
            this.form.goodDetailsLists = this.goodDetailsLists;
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.goodsInfoId != null) {
                        this.form.id = this.form.goodsInfoId;
                        updateGoodsInfo(this.form).then((response) => {
                            var obj = { pictureUrlList: [] };
                            if (this.imgone != this.form.goodsPictureUrl) {
                                obj.pictureUrlList.push(this.imgone);
                            }
                            if (obj.pictureUrlList.length > 0) {
                                updatePicture(obj).then((res) => {});
                            }
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        // this.nurseStationId=this.form.nurseStationId
                        addGoodsInfo(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$confirm("是否确认删除订单信息的数据项？", "提示", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                })
                .then(() => {
                    var obj = { pictureUrlList: [] };
                    obj.pictureUrlList.push(row.goodsPictureUrl);
                    updatePicture(obj).then((res) => {});
                    delGoodsInfo(row.goodsInfoId).then((res) => {
                        this.$message.success("删除成功");
                        this.getList();
                    });
                })
                .catch(() => {});
        },
        imgUrl(imgUrl) {
            this.form.goodsPictureUrl = imgUrl;
        },
        attributePitureUrl(imgUrl) {},
        attributePitureitem(item) {
            let items = JSON.parse(item);
            if (items.idd && !items.id) {
                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;
                    }
                });
            }
        },
    },
};