import {
    listStationDepartment,
    getStationDepartment,
    delStationDepartment,
    addStationDepartment,
    updateStationDepartment,
} from "@/api/system/StationDepartment";
import { getListByUser } from "@/api/system/userlist.js";

export default {
    name: "StationDepartment",
    data() {
        // 验证手机号的规则
        var checkMobile = (rule, value, cb) => {
            // 验证手机号的正则表达式
            const regMobile =
                /^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57]|19[0-9])[0-9]{8}$/;
            if (regMobile.test(value)) {
                return cb();
            }
            cb(new Error("请输入正确的联系电话"));
        };
        return {
            nurseStationlist: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            total2: 0,
            // 护理站部门信息表格数据
            StationDepartmentList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            addopen: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                nurseStationId: null,
                departmentCode: null,
                departmentName: null,
                departmentPerson: null,
                phone: null,
                nurseStationName: null,
            },
            nurseStationqueryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                nurseStationDepartmentList: {
                    nurseStationId: [{
                        required: true,
                        message: "请选择所属护理站",
                        trigger: "blur",
                    }, ],
                    departmentName: [{
                        required: true,
                        message: "请选择科室名称",
                        trigger: "blur",
                    }, ],
                    departmentPerson: [{
                        required: true,
                        message: "请选择科室负责人",
                        trigger: "blur",
                    }, ],
                    phone: [{
                        required: true,
                        validator: checkMobile,
                        trigger: "blur",
                        message: "",
                    }, ],
                },
                nurseStationId: [{
                    required: true,
                    message: "请选择所属护理站",
                    trigger: "blur",
                }, ],
                departmentName: [{
                    required: true,
                    message: "请选择科室名称",
                    trigger: "blur",
                }, ],
                departmentPerson: [{
                    required: true,
                    message: "请选择科室负责人",
                    trigger: "blur",
                }, ],
                phone: [{
                    required: true,
                    validator: checkMobile,
                    trigger: "blur",
                    message: "",
                }, ],
            },
            index: "",
            stationid: null,
            innerVisible: false,
            homenumber: null,
        };
    },
    created() {
        this.getList();
    },
    methods: {
        ParamsStation(item) {
            this.info();
            this.innerVisible = true
            this.homenumber = item
            this.stationid = this.queryParams.nurseStationId;
        },
        // add点击护理站名称
        nurseclick(row) {
            if (this.homenumber) {
                this.stationid = this.queryParams.nurseStationId
                this.queryParams.nurseStationName = row.nurseStationName;
                this.queryParams.nurseStationId = row.id;
            } else {
                this.stationid = row.id;
                this.form.nurseStationDepartmentList[this.index].nurseStationName =
                    row.nurseStationName;
                this.form.nurseStationDepartmentList[this.index].nurseStationId = row.id;
            }
            this.innerVisible = false;
        },
        // 新增护理站按钮
        clickinnerVisible(item, index) {
            this.addresetQuery();
            this.stationid = item.nurseStationId;
            this.index = index;
            this.innerVisible = true;
        },
        //删除deldisease
        deldisease(index) {
            this.form.nurseStationDepartmentList.splice(index, 1);
        },
        adddisease() {
            var obj = {
                nurseStationId: "",
                departmentCode: "",
                departmentName: "",
                departmentPerson: "",
                phone: "",
                nurseStationName: "请选择所属护理站",
            };
            if (this.form.nurseStationDepartmentList.length == 5) {
                this.$message.error("最多只能5条");
            } else {
                this.form.nurseStationDepartmentList.push(obj);
            }
        },
        /** 查询护理站部门信息列表 */
        getList() {
            this.loading = true;
            listStationDepartment(this.queryParams).then((response) => {
                this.StationDepartmentList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.addopen = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                departmentCode: null,
                departmentName: null,
                departmentPerson: null,
                phone: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                nurseStationName: null,
                nurseStationId: null,
                nurseStationDepartmentList: [{
                    nurseStationId: "",
                    departmentCode: "",
                    departmentName: "",
                    departmentPerson: "",
                    phone: "",
                    nurseStationName: "请选择所属护理站",
                }, ],
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        innerVisiblecancel() {
            this.innerVisible = false;
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                nurseStationId: null,
                departmentCode: null,
                departmentName: null,
                departmentPerson: null,
                phone: null,
                nurseStationName: null,
            };
            this.handleQuery();
        },
        addresetQuery() {
            this.nurseStationqueryParams = {
                pageNum: 1,
                pageSize: 10,
            };
            this.info();
        },
        //护理站list
        info() {
            this.loading = true
            getListByUser(this.nurseStationqueryParams).then((res) => {
                this.nurseStationlist = res.rows;
                this.total2 = res.total;
                this.loading = false
            });
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.homenumber = null
            this.reset();
            this.addopen = true;
            // this.nurseStationName="",
            this.title = "添加护理站部门信息";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            const id = row.id || this.ids;
            getStationDepartment(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改护理站部门信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            if (this.form.id == null) {
                this.form.nurseStationId =
                    this.form.nurseStationDepartmentList[0].nurseStationId;
                this.form.departmentCode =
                    this.form.nurseStationDepartmentList[0].departmentCode;
                this.form.departmentName =
                    this.form.nurseStationDepartmentList[0].departmentName;
                this.form.departmentPerson =
                    this.form.nurseStationDepartmentList[0].departmentPerson;
                this.form.nurseStationName =
                    this.form.nurseStationDepartmentList[0].nurseStationName;
                this.form.phone = this.form.nurseStationDepartmentList[0].phone;
            }
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateStationDepartment(this.form).then((response) => {
                            if (response.code == 200) {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                            }
                        });
                    } else {
                        addStationDepartment(this.form).then((response) => {
                            if (response.code == 200) {
                            this.$modal.msgSuccess("新增成功");
                            this.addopen = false;
                            this.getList();
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm("是否确认删除护理站部门信息的数据项？")
                .then(function() {
                    return delStationDepartment(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "system/StationDepartment/export", {
                    ...this.queryParams,
                },
                `StationDepartment_${new Date().getTime()}.xlsx`
            );
        },
    },
};
