import {
    listStationConsumable,
    getStationConsumable,
    delStationConsumable,
    addStationConsumable,
    updateStationConsumable,
    list,
} from "@/api/system/stationConsumable";
import { getListByUser } from "@/api/system/userlist.js";
import { getToken } from "@/utils/auth";
export default {
    name: "StationConsumable",
    data() {
        return {
            upload: {
                // 是否显示弹出层（用户导入）
                open: false,
                // 弹出层标题（用户导入）
                title: "",
                // 是否禁用上传
                isUploading: false,
                // 设置上传的请求头部
                headers: { Authorization: "Bearer " + getToken() },
                // 上传的地址
                url: process.env.VUE_APP_BASE_API + "/system/stationConsumable/importData",
            },
            value: "",
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            total2: 0,
            total3: 0,
            // 护理站耗材信息表格数据
            stationConsumableList: [],
            nurseStationlist: [],
            nurseStationlist2: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            open2: false,
            open3: false,
            xgopen: false,
            index: "",
            //权限查询
            getListByUserquery: {
                pageNum: 1,
                pageSize: 10,
            },
            // 查询参数
            nurseStationqueryParams: {
                pageNum: 1,
                pageSize: 10,
                areaCode: null,
                userId: null,
                nurseStationCode: null,
                nurseStationName: null,
                nurseStationType: null,
                agencyIntroduce: null,
                nurseStationDescription: null,
                longitude: null,
                latitude: null,
                phone: null,
                address: null,
                dutyPerson: null,
                dutyPhone: null,
                stationPictureUrl: null,
                sort: null,
            },
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                nurseStationId: null,
                nurseStationName: null,
                userId: null,
                consumableCode: null,
                consumableDetail: null,
                consumableUnit: null,
                consumablePrice: null,
                sort: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                nurseStationId: [{
                    required: true,
                    message: "请选择所属护理站",
                    trigger: "blur",
                },],
                consumableDetail: [{
                    required: true,
                    message: "请输入耗材包名称",
                    trigger: "blur",
                },],
                consumableUnit: [{
                    required: true,
                    message: "请输入耗材包单位",
                    trigger: "blur",
                },],
                consumablePrice: [{
                    required: true,
                    message: "请输入耗材包价格",
                    trigger: "blur",
                },],
                sort: [{
                    required: true,
                    message: "请输入排序",
                    trigger: "blur",
                },],
                nurseStationConsumables: {
                    consumableDetail: [{
                        required: true,
                        message: "请输入耗材包名称",
                        trigger: "blur",
                    },],
                    nurseStationId: [{
                        required: true,
                        message: "请选择所属护理站",
                        trigger: "blur",
                    },],
                    consumableUnit: [{
                        required: true,
                        message: "请输入耗材包单位",
                        trigger: "blur",
                    },],
                    consumablePrice: [{
                        required: true,
                        message: "请输入耗材包价格",
                        trigger: "blur",
                    },],
                    sort: [{
                        required: true,
                        message: "请输入排序",
                        trigger: "blur",
                    },],
                },
            },
            homenumber: null,
            stationid: null,
        };
    },
    created() {
        this.getList();
        // this.listinfo();
    },
    methods: {
        /** 下载模板操作 */
        importTemplate() {
            this.download(
                "/system/stationItem/downloadTemplate?fileType=2", {},
                `护理站耗材信息导入模板.xlsx`
            );
        },
        // 提交上传文件
        submitFileForm() {
            this.$refs.upload.submit();
        },
        // 文件上传成功处理
        handleFileSuccess(response, file, fileList) {
            this.upload.open = false;
            this.upload.isUploading = false;
            this.$refs.upload.clearFiles();
            this.$alert(
                "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
                response.msg +
                "</div>",
                "导入结果", { dangerouslyUseHTMLString: true }
            );
            this.getList();
        },
        // 文件上传中处理
        handleFileUploadProgress(event, file, fileList) {
            this.upload.isUploading = true;
        },
        delnurseStationConsumables(index) {
            this.form.nurseStationConsumables.splice(index, 1);
        },
        addnurseStationConsumables() {
            var obj = {
                nurseStationId: "",
                consumableUnit: null,
                sort: "",
                consumablePrice: "",
                consumableDetail: null,
                nurseStationName: "请选择所属护理站",
            };
            if (this.form.nurseStationConsumables.length == 5) {
                this.$message.error("最多只能5条");
            } else {
                this.form.nurseStationConsumables.push(obj);
            }
        },
        choicenurseStation(item, index) {
            this.index = index;
            this.open2 = true;
            this.info();
        },
        //页面所属护理站
        ParamsStation(item) {
            console.log(this.queryParams)
            this.open3 = true
            this.homenumber = item
            this.stationid = this.queryParams.nurseStationId;
        },
        //点击护理站左侧按钮
        clicknurseStation2(item) {
            this.queryParams.nurseStationName = item.nurseStationName
            this.queryParams.nurseStationId = item.id
            this.form.nurseStationId = item.id;
            this.form.nurseStationName = item.nurseStationName;
            this.open3 = false;
            this.reset2();
        },
        clicknurseStation(item) {
            this.form.nurseStationConsumables[this.index].nurseStationId = item.id;
            this.form.nurseStationConsumables[this.index].nurseStationName =
                item.nurseStationName;
            this.open2 = false;
            this.reset2();
        },
        /** 查询护理站耗材信息列表 */
        getList() {
            this.loading = true;
            this.listStationConsumableinfo();
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.xgopen = false;
            this.nurseStationName = "请选择所属护理站";
            this.reset();
        },
        cancel2() {
            this.open2 = false;
            this.open3 = false;
            this.reset2();
        },
        // 表单重置
        reset2() {
            this.getListByUserquery = {
                pageNum: 1,
                pageSize: 10,
                areaCode: null,
                userId: null,
                nurseStationCode: null,
                nurseStationName: null,
                nurseStationType: null,
                agencyIntroduce: null,
                nurseStationDescription: null,
                longitude: null,
                latitude: null,
                phone: null,
                address: null,
                dutyPerson: null,
                dutyPhone: null,
                stationPictureUrl: null,
                sort: null,
            };
        },
        reset() {
            this.form = {
                id: null,
                consumablePrice: null,
                sort: null,
                nurseStationId: "",
                nurseStationConsumables: [{
                    nurseStationId: "",
                    consumableUnit: null,
                    sort: "",
                    consumablePrice: "",
                    consumableDetail: null,
                    nurseStationName: "请选择所属护理站",
                },],
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        listStationConsumableinfo() {
            listStationConsumable(this.queryParams).then((response) => {
                this.stationConsumableList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        handleQuery() {
            this.loading = true;
            this.queryParams.pageNum = 1;
            this.listStationConsumableinfo();
        },
        //护理站搜索
        handleQuery2() {
            this.getListByUserquery.pageNum = 1;
            getListByUser(this.getListByUserquery).then((res) => {
                this.nurseStationlist = res.rows;
                this.total3 = res.total;
            });
        },
        /** 重置按钮操作 */
        sresetQuery() {
            this.resetForm("queryForm");
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                nurseStationId: null,
                userId: null,
                consumableCode: null,
                consumableDetail: null,
                consumableUnit: null,
                consumablePrice: null,
                sort: null,
            };
            this.handleQuery();
        },
        //护理站重置
        resetQuery() {
            this.reset2();
            this.handleQuery2();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加护理站耗材信息";
            this.cancel2();
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.loading = true;
            this.reset();
            const id = row.id || this.ids;
            getStationConsumable(id).then((response) => {
                this.form = response.data;
                this.xgopen = true;
                this.title = "修改护理站耗材信息";
                this.loading = false;
            });
        },
        /** 提交按钮 */
        submitForm() {
            if (this.form.id == null) {
                this.form.consumableDetail =
                    this.form.nurseStationConsumables[0].consumableDetail;
                this.form.nurseStationId =
                    this.form.nurseStationConsumables[0].nurseStationId;
                this.form.consumableUnit =
                    this.form.nurseStationConsumables[0].consumableUnit;
                this.form.consumablePrice =
                    this.form.nurseStationConsumables[0].consumablePrice;
                this.form.sort = this.form.nurseStationConsumables[0].sort;
            } else {
                this.form.consumablePrice = Number(this.form.consumablePrice);
            }
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateStationConsumable(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.xgopen = false;
                            this.getList();
                        });
                    } else {
                        this.form.nurseStationConsumables.forEach((e) => {
                            e.consumablePrice = Number(e.consumablePrice);
                        });
                        addStationConsumable(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.nurseStationName = "请选择所属护理站";
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否确认删除？")
                .then(function () {
                    return delStationConsumable(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/stationConsumable/export", {
                ...this.queryParams,
            },
                `stationConsumable_${new Date().getTime()}.xlsx`
            );
        },
        /** 导入按钮操作 */
        handleport() {
            this.upload.title = "护理站耗材导入";
            this.upload.open = true;
        },
        //护理站列表
        info() {
            getListByUser(this.getListByUserquery).then((res) => {
                this.nurseStationlist = res.rows;
                this.total3 = res.total;
            });
        },
    },
};