import {
    getGoodsInfo,
    delGoodsInfo,
    addGoodsInfo,
    updateGoodsInfo,
    updatePicture,
    goodsInfoList,
    goodsCategory,
    editGoodsWhetherShelf,
} from "@/api/system/goodsInfo";
import editor from "@/components/Editor";
import {
    getStationCategoryList,
} from "@/api/system/goodsCategory";
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 {
            imgsurl: { pictureUrlList: [] },
            imgone: "",
            imageUrl: "", //商品图片
            value: "",
            datas: null,
            // 遮罩层
            loading: true,
            // 选中数组
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            total2: 0,

            // 商品基本信息表格数据
            goodsInfoList: [],
            // 护理站列表
            nurseStationlist: [],
            handstationlist: [], //页面搜索list
            // 查询商品分类列表
            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: "是",
                },
            ],
            integraloption: [{
                    value: 0,
                    label: "否",
                },
                {
                    value: 1,
                    label: "是",
                },
            ],
            value: "",
            // goodAttributeDetailsLists:[],
            goodDetailsLists: [],
            ids: [],
            idd: 9999999,
            // 查询参数
            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,
                // 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" },
                ],
                sort: [
                    { required: true, message: "请输入商品顺序", trigger: "blur" },
                ],
            },
            isResouceShow: 1,
            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);
                },
            },
        };
    },
    created() {
        this.info();
        this.infos();
    },
    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,
                };
            }
            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: null,
                    integralExchangeCount: null,
                    integralExchangeFlag: null,
                    integralExchangeSill: 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() {
            ++this.isResouceShow;
            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: "",
                goodAttributeDetailsLists: [{
                    attributePitureUrl: "",
                    goodsPrice: "",
                    goodsStock: "",
                    sort: null,
                }, ],
            }, ];
        },
        handleQuery2() {
            // this.getListByUserquery.pageNum = 1;
            this.getList2();
        },
        getList2() {
            getListByUser(this.getListByUserquery).then((res) => {
                this.total2 = res.total;
                this.nurseStationlist = res.rows;
            });
        },
        resetQuery2() {
            this.resetForm("queryForm");
            (this.getListByUserquery = {
                pageNum: 1,
                pageSize: 10,
            }),
            this.handleQuery2();
        },
        addcancel() {
            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: "",
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                sort: null,
                idd: 9999999,
            }, ];
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            if (this.nurseStationlist[0]) {
                this.queryParams.pageNum = 1;
                this.queryParams.pageSize = 10;
                this.queryParams.goodsName = null;
                this.queryParams.goodsCategoryName = null;
                this.queryParams.goodsPurpose = null;
                this.queryParams.whetherShelf = null;
                // this.resetForm("queryForm");


            }
            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: "",
                integralExchangeCount: null,
                integralExchangeFlag: null,
                integralExchangeSill: null,
                sort: null,
                idd: 9999999,
            }, ];
            this.open = true;
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            // this.reset();
            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;
            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.goodDetailsLists.forEach((e) => {
                            e.goodsPrice = Number(e.goodsPrice);
                        });
                        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.imgsurl = { pictureUrlList: [] };
                            ++this.isResouceShow;
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        this.form.goodDetailsLists.forEach((e) => {
                            e.goodsPrice = Number(e.goodsPrice);
                        });
                        addGoodsInfo(this.form).then((response) => {
                            ++this.isResouceShow;
                            this.$modal.msgSuccess("新增成功");
                            // this.StationName = "请选择所属护理站";
                            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(() => {});
        },
        // handleDelete(row) {
        //   const ids = row.id || this.ids;
        //   this.$modal
        //     .confirm("是否确认删除？")
        //     .then(function () {
        //       return delGoodsInfo(ids);
        //     })
        //     .then(() => {
        //       this.getList();
        //       this.$modal.msgSuccess("删除成功");
        //     })
        //     .catch(() => {});
        // },
        // /** 导出按钮操作 */
        // handleExport() {
        //   this.download(
        //     "system/goodsInfo/export",
        //     {
        //       ...this.queryParams,
        //     },
        //     `goodsInfo_${new Date().getTime()}.xlsx`
        //   );
        // },
        imgUrl(imgUrl) {
            this.form.goodsPictureUrl = imgUrl;
        },
        attributePitureUrl(imgUrl) {
            // this.form.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;
                    }
                });
            }
        },
        //权限列表
        info() {
            goodsCategory().then((res) => {
                this.goodsCategorylist = res.rows;
            });
            getListByUser(this.getListByUserquery).then((res) => {
                this.total2 = res.total;
                this.nurseStationlist = res.rows;
                this.queryParams.nurseStationId = res.rows[0].id;
                // this.handleQuery();
                this.getList()

            });
        },
        change(e) {
            this.form.goodsCategoryId = e[e.length - 1]
        },
        infos() {
            var queryFor = {
                pageNum: 1,
                pageSize: 9999,
            };
            getListByUser(queryFor).then((res) => {
                this.handstationlist = res.rows;
            });
        },
    },
};