<template>
    <Container>
        <div style="margin: 0 auto">
            <el-form
                :model="queryParams"
                ref="queryForm"
                :inline="true"
                v-show="showSearch"
            >
                <el-form-item label="手机号" prop="phone">
                    <el-input
                        v-model="queryParams.phone"
                        placeholder="请输入手机号"
                        clearable
                        size="small"
                        maxlength="11"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item label="姓名" prop="realName" style="margin-left: 15px;">
                    <el-input
                        v-model="queryParams.realName"
                        placeholder="请输入姓名"
                        clearable
                        size="small"
                        maxlength="10"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item
                    label="工龄"
                    prop="serverAge"
                    label-width="40px"
                    style="margin-left: 15px"
                >
                    <el-input-number
                        v-model="queryParams.serverAgeStart"
                        style="width: 90px"
                        clearable
                        size="mini"
                        :controls="false"
                        :min="0"
                        :max="999"
                        :precision="0"
                        @keyup.enter.native="handleQuery"
                    />
                    <span style="margin: 0 4px">-</span>
                    <el-input-number
                        v-model="queryParams.serverAgeEnd"
                        style="width: 90px"
                        clearable
                        size="mini"
                        :controls="false"
                        :min="0"
                        :max="999"
                        :precision="0"
                        @keyup.enter.native="handleQuery"
                    />
                </el-form-item>

                <el-form-item label="等级" prop="serverLevel" style="margin-left: 15px;">
                    <el-select
                        v-model="queryParams.serverLevel"
                        placeholder="请选择"
                    >
                        <el-option
                            v-for="item in serverLevelOptions"
                            :key="item.decodeValue"
                            :label="item.decodeValue"
                            :value="item.decodeId"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="状态" prop="status" style="margin-left: 15px;">
                    <el-select
                        v-model="queryParams.stopOrderFor"
                        placeholder="请选择"
                    >
                        <el-option
                            v-for="item in statusOptions"
                            :key="item.value"
                            :label="item.value"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item>
                    <el-button
                        type="primary"
                        icon="el-icon-search"
                        size="mini"
                        @click="handleQuery"
                        >搜索</el-button
                    >
                    <el-button
                        icon="el-icon-refresh"
                        size="mini"
                        @click="resetQuery"
                        >重置</el-button
                    >
                </el-form-item>
            </el-form>

            <el-row :gutter="10">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['system:user:add']"
                        style="margin-bottom: 10px;"
                        >添加人员</el-button
                    >
                </el-col>

                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                v-loading="loading"
                :data="userList"
                @selection-change="handleSelectionChange"
            >
                <!-- <el-table-column type="selection" width="55" align="center" /> -->
                <el-table-column type="index" width="50" label="序号">
                </el-table-column>
                <el-table-column
                    label="手机号"
                    align="center"
                    prop="phone"
                    width="150"
                />
                <el-table-column
                    label="姓名"
                    align="center"
                    prop="realName"
                    width="160"
                />
                <el-table-column label="工龄" align="center" prop="serverAge" />
                <el-table-column
                    label="等级"
                    align="center"
                    prop="serverLevel"
                />
                <el-table-column label="服务项目数" align="center">
                    <template slot-scope="scope">
                        <a
                            style="color: #1c84c6"
                            v-text="scope.row.goodsCount"
                            @click="showGoodsList(scope.row)"
                        ></a>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center" prop="stopOrder">
                    <template slot-scope="scope">
                        <el-tag
                            v-if="scope.row.stopOrderFor == '0'"
                            type="success"
                        >
                            正常</el-tag
                        >
                        <el-tag v-if="scope.row.stopOrderFor == '1'" type="info">
                            停止接单</el-tag
                        >
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-alarm-clock"
                            @click="toSeviceTime(scope.row)"
                            v-hasPermi="['system:user:edit']"
                            >设置服务时间</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-view"
                            @click="selfInfoSEE(scope.row)"
                            v-hasPermi="['system:user:edit']"
                            >查看</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="toEdit(scope.row)"
                            v-hasPermi="['system:user:edit']"
                            >编辑</el-button
                        >
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['system:user:remove']"
                            >删除</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />

            <!-- 服务项目 -->
            <el-dialog
                title="服务项目"
                :visible.sync="serviceOpen"
                width="500px"
                append-to-body
            >
                <el-table :data="serviceProgramme" v-loading="goodsLoading">
                    <el-table-column
                        label="服务主题"
                        align="center"
                        prop="goods.name"
                    >
                    </el-table-column>
                    <el-table-column
                        label="服务类型"
                        align="center"
                        prop="categoryName"
                    >
                    </el-table-column>
                    <el-table-column
                        label="单价"
                        align="center"
                        prop="goodsServerPrice"
                    >
                        <template slot-scope="scope">
                            <span>{{ parsePrice(scope.row) }}</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="大概服务时长"
                        align="center"
                        prop="goods.singleTime"
                    >
                    </el-table-column>
                </el-table>

                <pagination
                    v-show="goodsTotal > 0"
                    :total="goodsTotal"
                    :page.sync="goodsQueryParams.pageNum"
                    :limit.sync="goodsQueryParams.pageSize"
                    @pagination="getGoodsList"
                />
            </el-dialog>

            <!-- 添加或修改服务人员 -->
            <el-dialog
                title="服务人员"
                :visible.sync="serviceUserOpen"
                width="500px"
                append-to-body
            >
                <el-form
                    :model="userParams"
                    label-width="80px"
                    ref="userForm"
                    :inline="true"
                    label-position="left"
                    :rules="UserParamsRules"
                >
                    <el-row style="margin-left: 50px">
                        <el-col :span="24">
                            <el-form-item label="手机号" prop="phone">
                                <el-input
                                    v-model="userParams.phone"
                                    placeholder="请输入手机号"
                                    clearable
                                    size="small"
                                    maxlength="11"
                                    style="width: 300px"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="姓名" prop="realName">
                                <el-input
                                    v-model="userParams.realName"
                                    placeholder="请输入姓名"
                                    clearable
                                    size="small"
                                    style="width: 300px"
                                    maxlength="10"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="工龄" prop="serverAge">
                                <el-input-number
                                    v-model="userParams.serverAge"
                                    clearable
                                    :controls="false"
                                    :min="0"
                                    :max="999"
                                    :precision="0"
                                    size="small"
                                    style="width: 300px"
                                    oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input-number> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="等级" prop="serverLevel">
                                <el-select
                                    v-model="userParams.serverLevel"
                                    placeholder="请选择 "
                                    clearable
                                    size="small"
                                    style="width: 300px"
                                >
                                    <el-option
                                        v-for="item in serverLevelOptions"
                                        :key="item.decodeValue"
                                        :label="item.decodeValue"
                                        :value="item.decodeId"
                                    >
                                    </el-option>
                                </el-select> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="个人简介" prop="desc">
                                <el-input
                                    type="textarea"
                                    :rows="6"
                                    maxlength="200"
                                    style="width: 300px"
                                    placeholder="请输入内容"
                                    show-word-limit
                                    v-model="userParams.desc"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col style="margin-left: 53%">
                            <el-button @click="add()" type="primary"
                                >保存</el-button
                            >
                            <el-button @click="cancelUser()">取消</el-button>
                        </el-col>
                    </el-row>
                </el-form>
            </el-dialog>

            <el-dialog
                title="服务人员信息"
                :visible.sync="open"
                width="500px"
                append-to-body=""
            >
                <el-form
                    :model="userParams"
                    label-width="80px"
                    ref="userForm"
                    :inline="true"
                    label-position="left"
                    :rules="UserParamsRules"
                    disabled
                >
                    <el-row style="margin-left: 50px">
                        <el-col :span="24">
                            <el-form-item label="手机号" prop="phone">
                                <el-input
                                    v-model="form.phone"
                                    placeholder="请输入手机号"
                                    clearable
                                    size="small"
                                    maxlength="11"
                                    style="width: 300px"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="姓名" prop="realName">
                                <el-input
                                    v-model="form.realName"
                                    placeholder="请输入姓名"
                                    clearable
                                    size="small"
                                    style="width: 300px"
                                    maxlength="10"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="工龄" prop="serverAge">
                                <el-input-number
                                    v-model="form.serverAge"
                                    clearable
                                    :controls="false"
                                    :min="0"
                                    :max="999"
                                    :precision="0"
                                    size="small"
                                    style="width: 300px"
                                    oninput="value=value.replace(/^\.+|[^\d.]/g,'')"
                                    @keyup.enter.native="handleQuery"
                                >
                                </el-input-number> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="等级" prop="serverLevel">
                                <el-select
                                    v-model="form.serverLevel"
                                    placeholder="请选择 "
                                    clearable
                                    size="small"
                                    style="width: 300px"
                                >
                                    <el-option
                                        v-for="item in serverLevelOptions"
                                        :key="item.decodeValue"
                                        :label="item.decodeValue"
                                        :value="item.decodeId"
                                    >
                                    </el-option>
                                </el-select> </el-form-item
                        ></el-col>
                        <el-col :span="24">
                            <el-form-item label="个人简介" prop="desc">
                                <el-input
                                    type="textarea"
                                    :rows="6"
                                    maxlength="200"
                                    style="width: 300px"
                                    placeholder="请输入内容"
                                    show-word-limit
                                    v-model="form.desc"
                                >
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </el-dialog>
        </div>
    </Container>
</template>

<script>
import {
    listUser,
    getUser,
    delUser,
    addUser,
    updateUser,
    serverGoodsList,
} from "@/api/system/ServiceUserService";
import { getCode, listCode } from "../../api/system/dict/code";
import { getInfo } from "@/api/login";
import { list } from "@/api/merchant/merchantService";
export default {
    name: "service",
    data() {
        return {
            //家政等级
            serverLevelOptions: [],
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 服务人员表格数据
            userList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            //服务项目弹出层
            serviceOpen: false,
            //服务项目数据
            serviceProgramme: [],
            goodsLoading: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                merchantId: null,
                serverAgeStart: undefined,
                serverAgeEnd: undefined,
                orderCount: null,
                serverLevel: null,
                phone: null,
                openId: null,
                appleId: null,
                nikeName: null,
                realName: null,
                age: null,
                sex: null,
                avatar: null,
                closeTime: null,
                closeReason: null,
                password: null,
                status: null,
                loginIp: null,
                loginDate: null,
                status: null,
                stopOrderFor: null,
            },
            // 表单参数
            form: {},
            serviceUserOpen: false,
            userParams: {
                phone: null,
                realName: null,
                serverAge: undefined,
                serverLevel: null,
                desc: null,
                serverId: null,
                merchantId: null,
                stopOrder: 0,
            },
            // 表单校验
            UserParamsRules: {
                phone: [
                    {
                        required: true,
                        message: "服务人员手机号不能为空",
                        trigger: "blur",
                    },
                    {
                        min: 11,
                        max: 11,
                        message: "请输入11位电话号码",
                        trigger: "blur",
                    },
                    {
                        pattern:
                            /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
                        message: "请输入正确的电话号码",
                    },
                ],
                realName: [
                    {
                        required: true,
                        message: "姓名不能为空",
                        trigger: "blur",
                    },
                ],
                serverAge: [
                    {
                        required: true,
                        message: "工龄不能为空",
                        trigger: "blur",
                    },
                ],
                serverLevel: [
                    {
                        required: true,
                        message: "等级不能为空",
                        trigger: "blur",
                    },
                ],
                desc: [
                    {
                        required: true,
                        message: "个人简介不能为空",
                        trigger: "blur",
                    },
                ],
            },
            goodsTotal: 0,
            goodsQueryParams: {
                pageNum: 1,
                pageSize: 10,
                serverId: "",
            },
            statusOptions: [
                 {
                    id: "",
                    value: "全部",
                },
                {
                    id: 0,
                    value: "正常",
                },
                {
                    id: 1,
                    value: "停止接单",
                },
               
            ],
            advice: false,
        };
    },
    created() {
        // this.getList();
        this.getServiceLever();
    },
    mounted() {
        this.getList();
    },
    methods: {
        seeAdvice() {
            this.advice = true;
        },
        noAdvice() {
            this.advice = false;
        },
        showGoodsList(row) {
            this.serviceProgramme = [];
            this.goodsTotal = 0;
            this.title = row.realName + " 服务项目";
            this.serviceOpen = true;
            this.goodsQueryParams = {
                pageNum: 1,
                pageSize: 10,
                serverId: "",
            };
            this.goodsQueryParams.serverId = row.serverId;
            this.getGoodsList();
        },
        getGoodsList() {
            serverGoodsList(this.goodsQueryParams)
                .then((res) => {
                    if (res.code == 200) {
                        this.serviceProgramme = res.rows;
                        this.goodsTotal = res.total;
                    } else {
                        this.$message.error(res.msg);
                    }
                })
                .catch((err) => {
                    console.log(err);
                    this.$message.error("请求失败");
                })
                .finally(() => {
                    this.goodsLoading = false;
                });
        },
        parsePrice(row) {
            if (row.goods == null) {
                return "--";
            }
            let price = row.goodsServerPrice + "/";
            if (row.goods.goodsUnit == "099") {
                price += row.goods.otherUnit;
            } else {
                price += row.unitName;
            }
            return price;
        },

        toSeviceTime(row) {
            this.$router.push({
                path: "/merchant/serviceTime",
                query: {
                    serverId: row.serverId,
                    serverName: row.realName,
                },
            });
        },
        infoSee(row) {
            let params = {
                pageNum: 1,
                pageSize: 10,
            };
            list().then((res) => {
                console.log(res);
            });
            this.serviceOpen = true;
        },
        /** 查询家政等级*/
        getServiceLever() {
            let params = {
                codeValue: "服务等级",
            };
            listCode(params).then((res) => {
                this.serverLevelOptions = res.rows;
            });
        },

        /** 查询服务人员列表 */
        getList() {
            getInfo().then((r) => {
                if (r.roles[0] == "merchant") {
                    if (r.merchant.merchantId != null) {
                        this.queryParams.merchantId = r.merchant.merchantId;
                    }
                }
                this.loading = true;
                listUser(this.queryParams).then((response) => {
                    this.userList = response.rows;
                    for (let i in this.userList) {
                        for (let j in this.serverLevelOptions) {
                            if (
                                this.userList[i].serverLevel ==
                                this.serverLevelOptions[j].decodeId
                            ) {
                                this.userList[i].serverLevel =
                                    this.serverLevelOptions[j].decodeValue;
                            }
                        }
                    }
                    this.total = response.total;
                    this.loading = false;
                });
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                merchantId: null,
                serverAgeStart: undefined,
                serverAgeEnd: undefined,
                orderCount: null,
                serverLevel: null,
                phone: null,
                openId: null,
                appleId: null,
                nikeName: null,
                realName: null,
                age: null,
                sex: null,
                avatar: null,
                closeTime: null,
                closeReason: null,
                password: null,
                status: null,
                loginIp: null,
                loginDate: null,
                merchantType: 0,
                status: null,
                stopOrderFor: null,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.reset();
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.serverId);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        //服务人员参数重置
        resetUserParams() {
            this.userParams = {
                phone: null,
                realName: null,
                serverAge: undefined,
                serverLevel: null,
                desc: null,
                serverId: null,
                merchantId: null,
                stopOrder: 0,
            };
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.resetUserParams();
            this.userParams.merchantId = this.queryParams.merchantId;
            this.serviceUserOpen = true;
        },

        cancelUser() {
            this.serviceUserOpen = false;
        },
        /**编辑按钮操作*/
        toEdit(row) {
            console.log("toEdit》》》》》>>>>", row);
            this.userParams.serverId = row.serverId;
            this.userParams.phone = row.phone;
            this.userParams.realName = row.realName;
            this.userParams.serverAge = row.serverAge;
            this.userParams.serverLevel = row.serverLevel;
            this.userParams.desc = row.desc;
            this.userParams.merchantId = row.merchantId;
            this.serviceUserOpen = true;
        },
        add() {
            for (let j in this.serverLevelOptions) {
                if (
                    this.userParams.serverLevel ==
                    this.serverLevelOptions[j].decodeValue
                ) {
                    this.userParams.serverLevel =
                        this.serverLevelOptions[j].decodeId;
                }
            }

            this.$refs["userForm"].validate((valid) => {
                if (valid) {
                    if (this.userParams.serverId !== null) {
                        updateUser(this.userParams).then((res) => {
                            this.$modal.msgSuccess("修改成功");
                            this.serviceUserOpen = false;
                            this.getList();
                        });
                    } else {
                        addUser(this.userParams).then((res) => {
                            this.$modal.msgSuccess("新增成功");
                            this.serviceUserOpen = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        selfInfoSEE(row) {
            this.reset();
            const serverId = row.serverId || this.ids;
            getUser(serverId).then((response) => {
                this.form = response.data;
                for (let j in this.serverLevelOptions) {
                    if (
                        this.form.serverLevel ==
                        this.serverLevelOptions[j].decodeId
                    ) {
                        this.form.serverLevel =
                            this.serverLevelOptions[j].decodeValue;
                    }
                }
                this.open = true;
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const serverIds = row.serverId || this.ids;
            this.$modal
                .confirm(
                    '是否确认删除服务人员姓名为"' + row.realName + '"的数据项？'
                )
                .then(function () {
                    return delUser(serverIds);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
    },
};
</script>

<style scoped></style>
