<template>
    <div class="app-container calendar-list-container exchangeView">

        <!-- 查询和其他操作 -->
        <div class="filter-container">
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="员工名称"
                v-model="listQuery.purchaser"></el-input>
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="订单编号"
                v-model="listQuery.orderSeq"></el-input>
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="商品名称"
                v-model="listQuery.name"></el-input>
            <el-input clearable class="filter-item" style="width: 200px;" placeholder="活动标题"
                v-model="listQuery.title"></el-input>
                <el-select v-model="listQuery.productAttribute" clearable placeholder="商品属性" class="filter-item" style="width: 200px">
                        <el-option :key="item.dictValue" v-for="item in productAttributeList" :label="item.dictLabel"
                            :value="item.dictValue">
                        </el-option>
                    </el-select>
                    <el-select v-model="listQuery.deliveryType" clearable placeholder="兑换方式" class="filter-item" style="width: 200px">
                        <el-option :key="item.dictValue" v-for="item in deliveryTypeList" :label="item.dictLabel" :value="item.dictValue">
                        </el-option>
            </el-select>
             <el-input clearable class="filter-item" style="width: 200px;" placeholder="联系人"
                                       v-model="listQuery.contact"></el-input>
                        <el-input clearable class="filter-item" style="width: 200px;" placeholder="联系电话"
                                       v-model="listQuery.contactPhone"></el-input>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleFilter">查找</el-button>
            <el-button class="filter-item" type="primary" v-waves icon="el-icon-download" @click="handleDownLoad">导出</el-button>

        </div>

        <!-- 查询结果 -->
        <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit
            highlight-current-row>
            <el-table-column type="index" label="序号" header-align="center" align="center">
            </el-table-column>
            <el-table-column align="center" min-width="200px" label="部门名称" prop="deptName">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="员工名称" prop="purchaser">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="员工号" prop="employeNo">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="手机号" prop="userPhone">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="工作地点" prop="workplace">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="订单编号" prop="orderSeq">
            </el-table-column>
            <el-table-column align="center" min-width="120px" label="订单状态" prop="orderTypeName">
            </el-table-column>
            <el-table-column align="center" min-width="200px" label="商品名称" prop="name">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="商品编号" prop="skuSeq">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="商品类型" prop="skuTypeName">
            </el-table-column>
            <el-table-column align="center" min-width="120px" label="商品属性" prop="productAttributeName">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="兑换方式" prop="deliveryTypeName">
            </el-table-column>
            <el-table-column align="center" min-width="150px" label="领取地点" prop="reclaimAddrName">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="兑换数" prop="skuCount">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="积分" prop="price">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="总积分" prop="total">
            </el-table-column>
            <el-table-column align="center" min-width="80px" label="活动标题" prop="title">
            </el-table-column>
            <el-table-column align="center" label="操作" width="80px" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="success" size="small"
                        @click="handleClick(scope.row.orderId, 'complete')">处理</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <div class="pagination-container">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="listQuery.page" :page-sizes="[10, 20, 30, 50]" :page-size="listQuery.limit"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <!-- 添加或修改对话框 -->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogVisible" :close-on-click-modal="false" width="70%">

            <h3>订单信息</h3>

            <el-form status-icon label-position="center" label-width="100px" style="width:100%;">
                <div style="display: flex;">

                    <el-form-item label="订单编号">
                        <el-input disabled v-model="detailData.orderSeq"></el-input>
                    </el-form-item>
                    <el-form-item label="员工姓名">
                        <el-input disabled v-model="detailData.purchaser"></el-input>
                    </el-form-item>
                    <el-form-item label="部门名称">
                        <el-input disabled v-model="detailData.deptName"></el-input>
                    </el-form-item>
                </div>

                <div style="display: flex;">
                    <el-form-item label="员工号">
                        <el-input disabled v-model="detailData.employeNo"></el-input>
                    </el-form-item>
                    <el-form-item label="手机号">
                        <el-input disabled v-model="detailData.userPhone"></el-input>
                    </el-form-item>
                    <el-form-item label="兑换时间">
                        <el-input disabled v-model="detailData.createTime"></el-input>
                    </el-form-item>
                </div>
                <div style="display: flex;">
                    <el-form-item label="数量">
                        <el-input disabled v-model="detailData.skuCount"></el-input>
                    </el-form-item>
                    <el-form-item label="单价(积分)">
                        <el-input disabled v-model="detailData.price"></el-input>
                    </el-form-item>
                    <el-form-item label="订单状态">
                        <el-input disabled v-model="detailData.orderTypeName"></el-input>
                    </el-form-item>
                </div>
                <div v-if="detailData.deliveryType == 1" style="display: flex;">
                    <el-form-item label="联系人">
                        <el-input disabled v-model="detailData.contact"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话">
                        <el-input disabled v-model="detailData.contactPhone"></el-input>
                    </el-form-item>
                    <el-form-item label="联系地址">
                        <el-input type="textarea" :rows="2" disabled v-model="addressStr"></el-input>
                    </el-form-item>
                </div>
                <div style="display: flex;">
                    <el-form-item label="支付类型">
                        <el-input disabled v-model="detailData.payTypeName"></el-input>
                    </el-form-item>
                </div>
            </el-form>
            <h3>商品信息</h3>
            <el-form status-icon label-position="center" label-width="100px" style="width:100%;">
                <div style="display: flex;">
                    <el-form-item label="商品编号">
                        <el-input disabled v-model="detailData.skuSeq"></el-input>
                    </el-form-item>
                    <el-form-item label="商品名称">
                        <el-input disabled v-model="detailData.name"></el-input>
                    </el-form-item>
                    <el-form-item label="商品类型">
                        <el-input disabled v-model="detailData.skuTypeName"></el-input>
                    </el-form-item>
                </div>
                <div style="display: flex;">
                    <el-form-item label="商品属性">
                        <el-input disabled v-model="detailData.productAttributeName"></el-input>
                    </el-form-item>
                    <el-form-item label="参考价格">
                        <el-input disabled v-model="detailData.hisPrice"></el-input>
                    </el-form-item>
                    <el-form-item label="兑换方式">
                        <el-input disabled v-model="detailData.deliveryTypeName"></el-input>
                    </el-form-item>
                </div>






                <div style="display: flex;" v-if="detailData.deliveryType == 4">
                        <el-form-item label="充值账号">
                            <el-input disabled v-model="detailData.contactPhone"></el-input>
                        </el-form-item>
                </div>
                <div v-if="detailData.deliveryType == 1">
                        <el-form-item label="快递单号">
                            <el-input v-model="detailData.trackingNumber"></el-input>
                        </el-form-item>
                </div>
                <div v-if="detailData.deliveryType == 2">
                    <el-form-item label="兑换码">
                            <el-input style="width: 100%"  type="textarea" :rows="2" v-model="detailData.trackingNumber"></el-input>
                    </el-form-item>
                </div>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="comment" style="width: 100%"></el-input>
                </el-form-item>





            </el-form>

            <el-table :data="tableData" border style="width: 100%">
                <el-table-column align="center" prop="orderStatus" label="订单流程" width="180">
                </el-table-column>
                <el-table-column align="center" prop="auditor" label="审核人" width="180">
                </el-table-column>
                <el-table-column align="center" prop="createTime" label="创建时间" width="180">
                </el-table-column>
                <el-table-column align="center" prop="comment" label="备注">
                </el-table-column>
            </el-table>


            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="complete(0)">同意</el-button>
                <el-button type="danger" @click="complete(1)">驳回</el-button>
            </div>
        </el-dialog>

    </div>
</template>

<style>
.demo-table-expand {
    font-size: 0;
}

.demo-table-expand label {
    width: 200px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
}
</style>

<script>
import { complete, list, detail } from "@/api/exchangeManage";
import { dataTypeList } from "@/api/public";
import waves from "@/directive/waves"; // 水波纹指令
import Tinymce from '@/components/Tinymce'

export default {
    name: 'exchangeList',
    components: { Tinymce },
    directives: { waves },
    data() {
        return {
            productAttributeList: [],
            deliveryTypeList: [

            ],
            trackingNumber: '',
            comment: '',
            tableData: [],
            list: [],
            total: 0,
            listLoading: false,
            listQuery: {
                page: 1,
                limit: 10,
                purchaser: '',
                orderSeq: '',
                title: '',
                productAttribute:'',
                deliveryType:'',
                orderType:1,
                name: '',
            },
            dialogVisible: false,
            dialogStatus: '',
            textMap: {
                complete: "审批",
                view: "详情",
            },
            detailData: { },
            addressStr:'',
        }
    },
    created() {
        this.getProductAttributeList();
        this.getDeliveryTypeList();
        this.getList();
    },
    methods: {
        getDeliveryTypeList(){
            dataTypeList({dictType:'exchange_methond'}).then(response => {
                this.deliveryTypeList = response.data.data;
            }).catch(() => {});
        },
        getProductAttributeList(){
            dataTypeList({dictType:'mall_sku_attribute'}).then(response => {
                this.productAttributeList = response.data.data;
            }).catch(() => {});
        },
        handleDownLoad(){
            window.location.href = process.env.BASE_API + '/mall-order/exportOrderLog?purchaser=' + this.listQuery.purchaser + '&orderSeq=' + this.listQuery.orderSeq + '&orderType=' + this.listQuery.orderType + '&title=' + this.listQuery.title + '&productAttribute=' + this.listQuery.productAttribute + '&deliveryType=' + this.listQuery.deliveryType + '&name=' + this.listQuery.name;
        },
        checked(val){
            if(val.flag == 0 && val.deliveryType==1){
                if(!val.trackingNumber){
                    this.$alert("请填写快递单号", "提示", {
                        confirmButtonText: "确定",
                    });
                    return false;
                }

            };
            if(val.flag == 0 && val.deliveryType==2){
                if(!val.trackingNumber){
                    this.$alert("请填写兑换码", "提示", {
                        confirmButtonText: "确定",
                    });
                    return false;
                }

            };
            if(val.flag == 1){
                if(!val.comment){
                    this.$alert("请填备注", "提示", {
                        confirmButtonText: "确定",
                    });
                    return false;
                }
            }
            return true;
        },
        complete(flag) {
            const parms = {
                orderId:this.detailData.orderId,
                orderType:this.detailData.orderType,
                flag:flag,
                deliveryType:this.detailData.deliveryType,
                trackingNumber: this.detailData.trackingNumber,
                nowPrice:this.detailData.nowPrice,
                comment: this.comment,
            }
            const isChecked = this.checked(parms);
            if(isChecked){
                complete(parms).then((response) => {
                    this.$notify({
                        title: "成功",
                        message: "操作成功",
                        type: "success",
                        duration: 2000,
                    });
                    this.getList();
                    this.dialogVisible = false;
                })
                .catch(() => { });
            }

        },
        getDetail(id) {
            detail({ orderId: id }).then(response => {
                this.detailData = response.data.data;
                this.tableData = response.data.data.logs;
                this.addressStr = response.data.data.addressStr + response.data.data.street + response.data.data.contactAddr;
            }).catch(() => {})
        },
        handleClick(id, tag) {
            this.comment = '';
            this.dialogStatus = tag;
            this.getDetail(id);
            this.dialogVisible = true;
        },
        getList() {
            this.listLoading = true
            list(this.listQuery).then(response => {
                this.list = response.data.data.items
                this.total = response.data.data.total
                this.listLoading = false
            }).catch(() => {})
        },
        handleFilter() {
            this.listQuery.page = 1
            this.getList()
        },
        handleSizeChange(val) {
            this.listQuery.limit = val
            this.getList()
        },
        handleCurrentChange(val) {
            this.listQuery.page = val
            this.getList()
        },
    }
}
</script>
<style>
.ad-avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.ad-avatar-uploader .el-upload:hover {
    border-color: #409EFF;
}

.ad-avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.ad-avatar {
    display: block;
}
.exchangeView .el-dialog__body {
    padding-top: 0px;
}
</style>
