<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px"
            size="medium" class="ry_form">
            <el-form-item label="订单ID" prop="orderId">
                <el-input v-model="queryParams.orderId" placeholder="请输入订单ID" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="服务日期" prop="serviceDate">
                <el-date-picker v-model="queryParams.serviceDate" 
                type="date" 
                placeholder="请选择服务日期" 
                clearable size="small" 
                format="yyyy-MM-dd" 
                value-format="yyyy-MM-dd" />
            </el-form-item>
            <!-- <el-form-item label="省份" prop="receiverProvince">
                <el-input v-model="queryParams.receiverProvince" placeholder="请输入省份" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="城市" prop="receiverCity">
                <el-input v-model="queryParams.receiverCity" placeholder="请输入城市" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <!-- <el-form-item label="区县" prop="receiverDistrict">
        <el-input
          v-model="queryParams.receiverDistrict"
          placeholder="请输入区县"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
            <el-form-item label="伴宠师" prop="masterId">
                <el-input v-model="queryParams.masterId" placeholder="请输入伴宠师" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="下单用户" prop="userId">
                <el-input v-model="queryParams.userId" placeholder="请输入下单用户" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="完成状态" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择完成状态" clearable size="small">
                    <el-option v-for="dict in dict.type.order_check_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <!-- <template v-if="showMoreCondition">
      <el-form-item label="完成状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择完成状态" clearable size="small">
            <el-option
              v-for="dict in dict.type.order_check_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
        </el-select>
      </el-form-item>
    </template> -->
            <el-form-item class="flex_one tr">
                <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-button :icon="showMoreCondition ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" size="mini" @click="showMoreCondition = !showMoreCondition">{{showMoreCondition ? '收起条件' : '展开条件'}}</el-button> -->
            </el-form-item>
        </el-form>

        <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['model:appletOrderDateFrequency:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['model:appletOrderDateFrequency:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['model:appletOrderDateFrequency:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['model:appletOrderDateFrequency:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row> -->

        <el-table v-loading="loading" :data="appletOrderDateFrequencyList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="id" width="100px"/>
            <el-table-column label="订单ID" align="center" prop="orderId" v-if="columns[0].visible" width="120px"/>
            <el-table-column label="服务日期" align="center" prop="serviceDate" v-if="columns[1].visible" width="100px"/>
            <!-- <el-table-column label="省份" align="center" prop="receiverProvince" v-if="columns[2].visible"/> -->
            <el-table-column label="城市" align="center" prop="receiverCity" v-if="columns[3].visible">
                <template slot-scope="scope">
                    {{ scope.row.receiverProvince }}{{ scope.row.receiverCity }}{{ scope.row.receiverDistrict }}
                </template>
            </el-table-column>
            <!-- <el-table-column label="区县" align="center" prop="receiverDistrict" v-if="columns[4].visible"/> -->
            <el-table-column label="接单伴宠师" align="center" prop="masterId" v-if="columns[5].visible" width="180px">
                <template slot-scope="scope">
                    <div style="display: flex; align-items: center; justify-content: center;">
                        <!-- 左侧头像 -->
                        <div style="margin-right: 10px;">
                            <el-avatar :size="40" :src="scope.row.master.userImage" icon="el-icon-user-solid"
                                style="background-color: #409EFF;">
                            </el-avatar>
                        </div>
                        <!-- 右侧信息：上下布局 -->
                        <div style="display: flex; flex-direction: column; align-items: flex-start;">
                            <div style="font-weight: bold; margin-bottom: 2px;">
                                {{ scope.row.master.userName || '未分配' }}
                            </div>
                            <div style="font-size: 12px; color: #999;">
                                ID: {{ scope.row.masterId || '--' }}
                            </div>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="下单用户" align="center" prop="userId" v-if="columns[6].visible" width="140px">
                <template slot-scope="scope">
                    <div style="display: flex; align-items: center; justify-content: center;">
                        <div style="display: flex; flex-direction: column; align-items: flex-start;">
                            <div style="font-weight: bold; margin-bottom: 2px;">
                                {{ scope.row.user.nickname || '未分配' }}
                            </div>
                            <div style="font-size: 12px; color: #999;">
                                ID: {{ scope.row.userId || '--' }}
                            </div>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="宠物" align="center" prop="status" v-if="columns[7].visible" width="300px">
                <template slot-scope="scope">
                    <div style="display: flex; align-items: center; justify-content: center;"
                    v-for="(pet, index) in scope.row.pets"
                    :key="index">
                        <!-- 左侧头像 -->
                        <div style="margin-right: 10px;">
                            <el-avatar :size="40" :src="pet.photo" icon="el-icon-user-solid"
                                style="background-color: #409EFF;">
                            </el-avatar>
                        </div>
                        <!-- 右侧信息：上下布局 -->
                        <div style="display: flex; flex-direction: column; align-items: flex-start;">
                            <div style="margin-bottom: 2px;">
                                {{ pet.name }} | {{ pet.breed }} | {{ pet.bodyType }}
                            </div>
                            <div style="font-size: 12px; color: #999;">
                                {{ pet.orderItemList.map(n => n.productName).join(',') }}
                            </div>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="完成状态" align="center" prop="status" v-if="columns[7].visible" width="100px">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.order_check_status" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="完成时间" align="center" prop="completionTime" v-if="columns[8].visible" width="130px"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px">
                <template slot-scope="scope">
                    <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['model:appletOrderDateFrequency:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['model:appletOrderDateFrequency:remove']"
          >删除</el-button> -->
                    <el-button size="mini" type="text" icon="el-icon-full-screen"
                        @click="handleCheckin(scope.row)">打卡信息</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="title" :visible.sync="open" width="50%" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="108px" inline class="dialog-form-two">
                <el-form-item label="下单端订单ID" prop="orderId">
                    <el-input v-model="form.orderId" placeholder="请输入下单端订单ID" />
                </el-form-item>
                <el-form-item label="服务日期" prop="serviceDate">
                    <el-input v-model="form.serviceDate" placeholder="请输入服务日期" />
                </el-form-item>
                <el-form-item label="省份" prop="receiverProvince">
                    <el-input v-model="form.receiverProvince" placeholder="请输入省份" />
                </el-form-item>
                <el-form-item label="城市" prop="receiverCity">
                    <el-input v-model="form.receiverCity" placeholder="请输入城市" />
                </el-form-item>
                <el-form-item label="区县" prop="receiverDistrict">
                    <el-input v-model="form.receiverDistrict" placeholder="请输入区县" />
                </el-form-item>
                <el-form-item label="伴宠师" prop="masterId">
                    <el-input v-model="form.masterId" placeholder="请输入伴宠师" />
                </el-form-item>
                <el-form-item label="下单用户" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入下单用户" />
                </el-form-item>
                <el-form-item label="完成状态">
                    <el-radio-group v-model="form.status">
                        <el-radio v-for="dict in dict.type.order_check_status" :key="dict.value"
                            :label="parseInt(dict.value)">{{ dict.label }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 打卡弹窗 -->
        <checkin-dialog 
            v-model="checkinDialogVisible" 
            :order-data="currentOrderData">
        </checkin-dialog>
    </div>
</template>

<script>
import { listAppletOrderDateFrequency, 
    getAppletOrderDateFrequencyCheck,
    getAppletOrderDateFrequency, delAppletOrderDateFrequency, addAppletOrderDateFrequency, updateAppletOrderDateFrequency, exportAppletOrderDateFrequency } from "@/api/model/appletOrderDateFrequency";
import CheckinDialog from "@/components/CheckinDialog.vue";

export default {
    name: "AppletOrderDateFrequency",
    dicts: ['order_check_status'],
    components: {
        CheckinDialog
    },
    data() {
        return {
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 日订单表格数据
            appletOrderDateFrequencyList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 打卡弹窗显示状态
            checkinDialogVisible: false,
            // 当前选中的订单数据
            currentOrderData: {},
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderId: null,
                serviceDate: null,
                receiverProvince: null,
                receiverCity: null,
                receiverDistrict: null,
                masterId: null,
                userId: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                orderId: [
                    { required: true, message: "下单端订单ID不能为空", trigger: "blur" }
                ],
                serviceDate: [
                    { required: true, message: "服务日期不能为空", trigger: "blur" }
                ],
            },
            columns: [
                { key: 1, label: "下单端订单ID", visible: true },
                { key: 2, label: "服务日期", visible: true },
                { key: 5, label: "省份", visible: true },
                { key: 6, label: "城市", visible: true },
                { key: 7, label: "区县", visible: true },
                { key: 9, label: "伴宠师", visible: true },
                { key: 10, label: "下单用户", visible: true },
                { key: 11, label: "完成状态", visible: true },
                { key: 12, label: "完成时间", visible: true },
                { key: 13, label: "宠物信息", visible: true },
            ],
            showMoreCondition: false
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询日订单列表 */
        getList() {
            this.loading = true;
            listAppletOrderDateFrequency(this.queryParams).then(response => {
                this.appletOrderDateFrequencyList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                orderId: null,
                serviceDate: null,
                serviceExpected: null,
                petId: null,
                receiverProvince: null,
                receiverCity: null,
                receiverDistrict: null,
                receiverDetailAddress: null,
                masterId: null,
                userId: null,
                status: 0,
                completionTime: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        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 = "添加日订单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getAppletOrderDateFrequency(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改日订单";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateAppletOrderDateFrequency(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAppletOrderDateFrequency(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除日订单编号为"' + ids + '"的数据项？').then(function () {
                return delAppletOrderDateFrequency(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            const queryParams = this.queryParams;
            this.$modal.confirm('是否确认导出所有日订单数据项？').then(() => {
                this.exportLoading = true;
                return exportAppletOrderDateFrequency(queryParams);
            }).then(response => {
                this.download(response.msg);
                this.exportLoading = false;
            }).catch(() => { });
        },
        handleCheckin(row) {
            this.currentOrderData = row;
            this.checkinDialogVisible = true;
        }
    }
};
</script>
