<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label-width="120px" prop="orgIndex" v-if="userInfoVO.userType == '00'">
                <el-select v-model="queryParams.orgIndex" filterable placeholder="请选择门店" clearable>
                    <template>
                        <el-option v-for="store in storeList" :key="store.id + store.sourceType" :label="store.name" :value="store.id + ',' + store.sourceType"> </el-option>
                    </template>
                </el-select>
            </el-form-item>
            <el-form-item prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
                    <el-option key="6" label="清洗中" value="6" />
                    <el-option key="7" label="已上挂" value="7" />
                    <el-option key="8" label="待送衣" value="8" />
                    <el-option key="9" label="送衣中" value="9" />
                    <el-option key="11" label="已完成" value="11" />
                </el-select>
            </el-form-item>
            <el-form-item prop="orderNo">
                <el-input v-model="queryParams.orderNo" placeholder="请输入订单条码" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-date-picker v-model="createTimeRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
            </el-form-item>
            <el-form-item prop="appUserPhoneNumber">
                <el-input v-model="queryParams.appUserPhoneNumber" placeholder="请输入会员手机" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <!-- <el-form-item prop="userId">
                <el-input v-model="queryParams.userId" placeholder="会员条码" clearable @keyup.enter.native="handleQuery" />
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" @click="handleExport" v-hasPermi="['order:cloth:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <Page uri="/mapi/order/cloth/list" :request-params="queryParams" ref="pagination">
            <el-table-column label="订单编号" align="center" prop="orderNo" min-width="120" fixed="left" />
            <el-table-column v-if="userInfoVO.userType == '00'" label="门店名称" align="center" prop="sysOrg.name" width="150" />
            <el-table-column label="衣服数量" align="center" prop="orderClothCount" width="60" />
            <el-table-column label="合计" align="center" prop="clothItemName" width="200">
                <template slot-scope="scope">
                    <span>合计：{{ scope.row.payAmount }}元</span>
                    <br />
                    <span v-if="scope.row.payType == '0'">支付方式：微信</span>
                    <span v-if="scope.row.payType == '1'">支付方式：支付宝</span>
                    <span v-if="scope.row.payType == '2'">支付方式：现金</span>
                    <span v-if="scope.row.payType == '3'">支付方式：余额</span>
                </template>
            </el-table-column>
            <el-table-column label="客户信息" align="center" width="140">
                <template slot-scope="scope">
                    <span>{{ scope.row.appUserName ? scope.row.appUserName : '--' }}</span>
                    <br />
                    <span>{{ scope.row.appUserPhoneNumber ? scope.row.appUserPhoneNumber : '' }}</span>
                </template>
            </el-table-column>
            <el-table-column label="订单时间" align="center" prop="createTime" min-width="200" />
            <el-table-column label="付款方式" align="center" prop="createTime" min-width="120">
                <template slot-scope="scope">
                    <span v-if="scope.row.payTimeType == '0'">收衣付款</span>
                    <span v-if="scope.row.payTimeType == '1'">取衣付款</span>
                </template>
            </el-table-column>
            <el-table-column label="预约配送状态" align="center" width="150">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.cloth_appointment_status" :value="scope.row.refundStatus" />
                </template>
            </el-table-column>
            <el-table-column label="订单状态" align="center" width="150">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.cloth_order_status" :value="scope.row.orderStatus" />
                </template>
            </el-table-column>
            <el-table-column label="退款状态" align="center" width="150">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.goods_order_refund_status" :value="scope.row.refundStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="250">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-s-order" @click="handleDetail(scope)">详情</el-button>
                    <el-button type="text" icon="el-icon-delete" v-if="scope.row.orderStatus != '10' && scope.row.orderStatus != '11' && scope.row.orderStatus != '4'" @click="handleClose(scope.row)">撤单 </el-button>
                    <el-button type="text" icon="el-icon-printer" v-if="userInfoVO.userType != '00'" @click="btn_lodop(scope.row)">打印</el-button>
                </template>
            </el-table-column>
        </Page>

        <!-- 添加或修改洗衣订单衣服明细对话框 -->
        <el-dialog :title="title" :visible.sync="open" size="70%" append-to-body>
            <el-row :gutter="15" style="margin-bottom: 80px; margin-left: 20px; width: calc(100% - 20px)">
                <el-col :span="24">
                    <el-descriptions class="margin-top" :column="2" border>
                        <el-descriptions-item>
                            <template slot="label"> 订单号 </template>
                            {{ orderDetail.orderNo }}
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 支付方式 </template>
                            <template v-if="orderDetail.payType == '0'">微信</template>
                            <template v-if="orderDetail.payType == '1'">支付宝</template>
                            <template v-if="orderDetail.payType == '2'">现金</template>
                            <template v-if="orderDetail.payType == '3'">余额</template>
                            （{{ orderDetail.payAmount }}元）
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 姓名 </template>
                            {{ orderDetail.appUserName }}
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 电话 </template>
                            {{ orderDetail.appUserPhoneNumber }}
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 总金额（元） </template>
                            {{ orderDetail.orderClothPrice }}
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 时间 </template>
                            {{ orderDetail.createTime }}
                        </el-descriptions-item>
                        <el-descriptions-item :span="2" v-if="userInfoVO.userType == '00'">
                            <template slot="label"> 门店名称 </template>
                            {{ orderDetail.sysOrg ? orderDetail.sysOrg.name : '' }}
                        </el-descriptions-item>
                        <el-descriptions-item>
                            <template slot="label"> 订单备注 </template>
                            {{ orderDetail.remark }}
                        </el-descriptions-item>
                    </el-descriptions>
                </el-col>
                <el-col :span="24" style="margin-top: 10px">
                    <el-table :data="orderClothDetails" fit highlight-current-row border stripe>
                        <el-table-column label="衣物条码" align="center" prop="washCode" width="180" fixed="left" />
                        <el-table-column label="衣服名称" align="center" prop="clothItemName" width="200">
                            <template slot-scope="scope">
                                <span> {{ calculateClothName(scope.row) }} </span>
                            </template>
                        </el-table-column>

                        <el-table-column label="颜色" align="center" width="150">
                            <template slot-scope="scope">
                                <span v-for="(item, index) in scope.row.orderClothColors">
                                    {{ index == 0 ? item.clothColorName : ',' + item.clothColorName }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="瑕疵" align="center" width="150">
                            <template slot-scope="scope">
                                <span v-for="(item, index) in scope.row.orderClothFlaws">
                                    {{ index == 0 ? item.clothFlawName : ',' + item.clothFlawName }}
                                </span>
                                <el-tag v-if="!scope.row.orderClothFlaws">暂无</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="特殊处理" align="center" width="150">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.orderClothCrafts" size="small" v-for="item in scope.row.orderClothCrafts" style="margin-right: 10px; margin-bottom: 2.5px; margin-top: 2.5px">
                                    {{ item.clothCraftName }}
                                </el-tag>
                                <el-tag v-if="!scope.row.orderClothCrafts">暂无</el-tag>
                            </template>
                        </el-table-column>

                        <el-table-column label="附件" align="center">
                            <template slot-scope="scope">
                                <el-tag size="small" v-for="item in scope.row.orderClothAdjuncts" style="margin-right: 10px; margin-bottom: 2.5px; margin-top: 2.5px">
                                    {{ item.adjunctName + '*' + item.num }}
                                </el-tag>
                                <el-tag v-if="!scope.row.orderClothAdjuncts">暂无</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="洗衣速度" align="center" prop="clothSpeedName" width="150" />
                        <el-table-column label="挂衣号" align="center">
                            <template slot-scope="scope">
                                <span v-if="scope.row.clothHanger">
                                    {{ scope.row.clothHanger.name + '#' + scope.row.clothHangerCode }}
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="状态" align="center" prop="flowStatus" width="80">
                            <template slot-scope="scope">
                                <dict-tag :options="dict.type.order_cloth_flow_status" :value="scope.row.flowStatus" />
                            </template>
                        </el-table-column>
                    </el-table>
                </el-col>
            </el-row>

            <div slot="footer" class="dialog-footer">
                <!--        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>-->
                <el-button @click="cancel" style="margin-left: 5%">关 闭</el-button>
            </div>
        </el-dialog>

        <el-dialog title="打印操作" :visible.sync="pointerType" width="30%" :before-close="handlePointerClose" class="pointer_content">
            <div class="pointer_view">
                <div class="pointer_icon">
                    <i class="el-icon-printer"></i>
                </div>
                <div class="btn_box">
                    <div class="btn" @click="setPointer(0)">打印小票</div>
                    <div class="btn" @click="setPointer(1)">打印条码</div>
                    <div class="btn" @click="pointerType = false">关闭</div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOrderCloth, refundOrderCloth } from '../../../api/order/cloth'
import { listOrderClothItem } from '../../../api/order/clothItem'
import { getLodop } from '@/utils/lodopUtils'
import { allOrg } from '@/api/system/store'

export default {
    name: 'ClothItem',
    dicts: ['sys_yes_no', 'cloth_appointment_status', 'order_cloth_flow_status', 'order_cloth_flow_action', 'cloth_order_status', 'goods_order_refund_status'],
    data() {
        return {
            orgList: [],
            storeList: [],
            factoryList: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 洗衣订单衣服明细表格数据
            clothOrderList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                orderStatus: null,
                orderSource: null,
                orderNo: null,
                orgIndex: null
            },
            createTimeRange: [],
            // 表单参数
            form: {},
            orderDetail: {},
            orderClothDetails: [],
            // 表单校验
            rules: {},
            userInfoVO: null,

            LODOP: null,
            number: 1,
            pointerList: [],
            pointerType: false,
            pointerInfo: null
        }
    },
    created() {
        this.userInfoVO = this.getUserInfo()
        allOrg().then((res) => {
            this.orgList = res.data
            this.orgList.forEach((org) => {
                if (org.sourceType == '01') {
                    this.factoryList.push(org)
                } else {
                    this.storeList.push(org)
                }
            })
        })
        this.getList()
    },
    mounted() {
        this.number = 1
        const printerTime = setInterval(() => {
            this.LODOP = getLodop()
            this.number++
            if (this.number == 25) {
                clearInterval(printerTime)
            }
            if (this.LODOP) {
                clearInterval(printerTime)
            }
        }, 200)

        this.pointerList = JSON.parse(this.$cache.local.get('printerSeting'))
    },
    methods: {
        /** 查询洗衣订单衣服明细列表 */
        getList() {
            this.loading = true
            if (null != this.createTimeRange && '' != this.createTimeRange) {
                this.queryParams['beginCreateTime'] = this.createTimeRange[0]
                this.queryParams['endCreateTime'] = this.createTimeRange[1]
            } else {
                delete this.queryParams.beginCreateTime
                delete this.queryParams.endCreateTime
            }
            if (this.queryParams.orgIndex != null) {
                this.queryParams.orgId = this.queryParams.orgIndex.split(',')[0]
                this.queryParams.sourceType = this.queryParams.orgIndex.split(',')[1]
            } else {
                delete this.queryParams.orgId
                delete this.queryParams.sourceType
            }
            this.$nextTick(() => {
                this.$refs.pagination.handleSearch(true)
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.resetForm('form')
        },
        calculateClothName(row) {
            var clothName = row.clothItemName
            if (row.orderClothTypes) {
                row.orderClothTypes.forEach((item) => {
                    clothName += ' 【' + item.clothTypeName + '】'
                })
            }
            return clothName
        },
        /** 搜索按钮操作 */
        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 = '添加洗衣订单衣服明细'
        },
        /** 详情按钮 */
        handleDetail(scope) {
            console.log('handleDetail')
            this.reset()
            this.orderClothDetails = []
            this.orderDetail = scope.row
            listOrderClothItem({ pageSize: 99999, pageNum: 1, orderNo: scope.row.orderNo }).then((res) => {
                this.orderClothDetails = res.rows
                this.title = '订单详情'
                this.open = true
            })
        },
        handleClose(row) {
            var _this = this
            var content = '退单金额如下：'
            if (row.payType == '0') {
                content += '微信 '
            } else if (row.payType == '1') {
                content += '支付宝 '
            } else if (row.payType == '2') {
                content += '现金 '
            } else if (row.payType == '3') {
                content += '余额 '
            }
            content += row.payAmount + '元！是否确定撤单？'
            this.$confirm(content).then(function () {
                refundOrderCloth({ orderId: row.id }).then((res) => {
                    _this.$message({
                        type: 'success',
                        message: '撤单成功'
                    })
                    _this.getList()
                })
            })
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        handlePointerClose(done) {
            this.$confirm('是否关闭打印操作？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        },
        btn_lodop(row) {
            if (this.userInfoVO.userType == '02') {
                const obj = {
                    name: this.userInfoVO.storeName,
                    contactPhone: this.userInfoVO.phonenumber,
                    user: this.userInfoVO.nickName
                }
                row.sysOrg = obj
            }

            this.pointerInfo = row

            listOrderClothItem({ pageSize: 99999, pageNum: 1, orderNo: row.orderNo }).then((res) => {
                this.pointerInfo.orderClothDetails = res.data.records
            })

            this.pointerType = true
        },
        setPointer(type) {
            if (!this.LODOP) {
                this.$modal.msgError('请安装打印机软件')
                return
            }

            let p_name = ''

            this.pointerList.forEach((pointer) => {
                if (pointer.printType == type) {
                    p_name = pointer.printName
                }
            })

            // console.log(this.pointerList, p_name)

            if (type == 0) {
                this.laundryPrinter(this.LODOP, this.pointerInfo, p_name)
            } else {
                this.barCodePrinter(this.LODOP, this.pointerInfo, p_name)
            }

            this.pointerType = false
        },
        /** 导出按钮操作 */
        handleExport() {
            if (null != this.createTimeRange && '' != this.createTimeRange) {
                this.queryParams['beginCreateTime'] = this.createTimeRange[0]
                this.queryParams['endCreateTime'] = this.createTimeRange[1]
            } else {
                delete this.queryParams.beginCreateTime
                delete this.queryParams.endCreateTime
            }
            if (this.queryParams.orgIndex != null) {
                this.queryParams.orgId = this.queryParams.orgIndex.split(',')[0]
                this.queryParams.sourceType = this.queryParams.orgIndex.split(',')[1]
            } else {
                delete this.queryParams.orgId
                delete this.queryParams.sourceType
            }
            this.download(
                'mapi/order/cloth/export',
                {
                    ...this.queryParams
                },
                `cloth_order_${new Date().getTime()}.xlsx`
            )
        }
    }
}
</script>

<style scoped lang="scss">
.pointer_content {
    ::v-deep .el-dialog__body {
        padding: 0;
    }
    .pointer_view {
        .pointer_icon {
            text-align: center;
            i {
                font-size: 100px;
                color: #ddd;
            }
            padding: 20px;
        }
        .btn_box {
            display: flex;
            align-items: center;
            border-top: 1px solid #e8e8e8;
            padding: 10px 0;
            background-color: #fafafa;
            .btn {
                flex: 1;
                text-align: center;
                padding: 10px;
                cursor: pointer;
                &:nth-child(2) {
                    border-left: 1px solid #e8e8e8;
                    border-right: 1px solid #e8e8e8;
                }
                &:hover {
                    color: #1890ff;
                }
            }
        }
    }
}
</style>
