<template>
    <scTable ref="table" :apiObj="list.apiObj" :params="searchKey" @selectionChange="selectionChange" row-key="id">
        <el-table-column type="selection" width="50" fixed="left"></el-table-column>
        <el-table-column label="订单编号" prop="order_id" width="220" fixed="left">
            <template #default="scope">
                <span v-if="scope.row.order_id">{{ scope.row.order_id }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="所属代理" prop="status" width="160">
            <template #default="scope">
                <span>{{ scope.row.agent?scope.row.agent.name:'-' }}</span>
            </template>
        </el-table-column>
        <el-table-column label="所属店铺" prop="status" width="160">
            <template #default="scope">
                <span>{{ scope.row.store?scope.row.store.store_name:'-' }}</span>
            </template>
        </el-table-column>
        <el-table-column label="订单类型" prop="status" width="120">
            <template #default="scope">
                <div class="status-danger" v-if="scope.row.service_type==1"><sc-status-indicator type="danger"></sc-status-indicator> 预约</div>
                <div class="status-success" v-if="scope.row.service_type==2"><sc-status-indicator type="success"></sc-status-indicator> 快递</div>
            </template>
        </el-table-column>
        <el-table-column label="收货地址" prop="company_id" width="120">
            <template #default="scope">
                <el-popover :width="200" placement="top-start">
                    <template #reference>
                        <div class="el-text">
                            {{scope.row.username?scope.row.username:'-'}}
                            <div class="desc" v-copy="'联系人:'+scope.row.username+'联系电话:'+scope.row.mobile+'联系地址:'+scope.row.region+scope.row.address"><span>一键复制</span></div>
                        </div>
                    </template>
                    <div class="flex-tags-size">
                        <span>联系人</span>{{scope.row.username?scope.row.username:'-'}}<br><br>
                        <span>联系电话</span>{{scope.row.mobile?scope.row.mobile:'-'}}<br><br>
                        <span>联系地址</span>{{scope.row.region?scope.row.region:'-'}}{{scope.row.address?scope.row.address:'-'}}
                    </div>
                </el-popover>
            </template>
        </el-table-column>
        <el-table-column label="商品ID" prop="out_id" width="150">
            <template #default="scope">
                <span v-if="scope.row.product_id">{{ scope.row.product_id }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="商品信息" prop="product_name" width="220">
            <template #default="scope">
                <div class="el-text" v-if="scope.row.product_name">
                    {{ scope.row.product_name }}
                    <div class="desc"><span>{{ scope.row.sku }}</span></div>
                </div>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="数量" prop="count" width="100">
            <template #default="scope">
                <span>{{ scope.row.parent.count }}件</span>
            </template>
        </el-table-column>
        <el-table-column label="订单金额" prop="order_amount" width="150">
            <template #default="scope">
                <span v-if="scope.row.parent.order_amount">￥{{ scope.row.parent.order_amount }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="支付金额" prop="pay_amount" width="150">
            <template #default="scope">
                <span v-if="scope.row.parent.pay_amount">￥{{ scope.row.parent.pay_amount }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" width="180">
            <template #default="scope">
                <div class="status-danger" v-if="scope.row.express_status==0"><sc-status-indicator type="danger"></sc-status-indicator> 待发货</div>
                <div class="status-success" v-if="scope.row.express_status==1"><sc-status-indicator type="success"></sc-status-indicator> 待核销</div>
                <div class="status-danger" v-if="scope.row.express_status==2"><sc-status-indicator type="danger"></sc-status-indicator> 已完成</div>
                <div class="status-info" v-if="scope.row.express_status==3"><sc-status-indicator type="info"></sc-status-indicator> 已取消</div>
                <div class="status-info" v-if="scope.row.express_status==4"><sc-status-indicator type="info"></sc-status-indicator> 退款审核</div>
                <div class="status-info" v-if="scope.row.express_status==5"><sc-status-indicator type="info"></sc-status-indicator> 拒绝退款</div>
                <div v-if="scope.row.status==5 && scope.row.remark">{{ scope.row.remark }}</div>
            </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_at" width="180"></el-table-column>
        <el-table-column label="操作" width="200" align="right" fixed="right">
            <template #default="scope">
                <el-button-group v-if="scope.row.express_status == 4">
                    <el-button text type="warning" size="small" @click="table_log(scope.row)">订单记录</el-button>
                    <el-button text type="success" size="small" @click="table_send(scope.row)">通过</el-button>
                    <el-button text type="warning" size="small" @click="table_pass(scope.row)">拒绝</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </scTable>
    <addMain ref="addMain" @success="handleSuccess"></addMain>
    <logMain ref="logMain" @success="handleSuccess"></logMain>
</template>

<script>
import addMain from "@/views/manage/components/send";
import logMain from '@/views/manage/order/components/log'
export default {
    components: {
        addMain,logMain
    },
    data(){
        return {
            list: {
                apiObj: this.$API.merOrder.list
            },
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{
                ex_status:5
            }
        }
    },
    methods: {
        table_pass(data){
            this.$confirm(`拒绝申请后，该笔订单状态变为原来的服务中，如已开通短信通知能力则系统自动发送短信至用户`, '提示', {
                type: 'warning'
            }).then(async () => {
                var loading = this.$loading();
                var resp = await this.$API.merOrder.refuse.post({"order":data.order_sn});
                loading.close();
                if (resp.code == 0) {
                    return this.$message.error(resp.msg)
                }
                this.$message.success(resp.msg)
                this.$refs.table.refresh()
            }).catch(() => {

            })
        },
        table_send(data){
            this.$confirm(`通过审核后，系统将自动对该订单进行解锁，用户可在抖音团购订单中申请退款`, '提示', {
                type: 'warning'
            }).then(async () => {
                var loading = this.$loading();
                var resp = await this.$API.merOrder.agree.post({"order":data.order_sn});
                loading.close();
                if (resp.code == 0) {
                    return this.$message.error(resp.msg)
                }
                this.$message.success(resp.msg)
                this.$refs.table.refresh()
            }).catch(() => {

            })
        },
        table_log(data){
            this.$nextTick(() => {
                this.$refs.logMain.open("edit").setData(data)
            })
        },
        refresh(){
            this.$refs.table.refresh()
        },
        upData(data){
            this.$refs.table.upData(data)
        },
        handleSuccess(){
            this.$refs.table.refresh()
        },
        sortChange(event){
            if (event.order) {
                var data = {
                    "field":event.prop,
                    "order":event.order
                }
                this.$refs.table.upData(data)
            } else {
                this.$refs.table.reload(this.searchKey)
            }
            return ;
        },
        selectionChange(event){
            this.dataSelect = [];
            var arr = [];
            var arrCompany = [];
            event.forEach(function(val,index){
                arr[index] = val.id;
                arrCompany[index] = val;
            });
            this.dataSelectFull = arrCompany;
            this.dataSelect = arr;
            this.$emit("success",this.dataSelect);
            this.$emit("successFull",this.dataSelectFull);
        },
    }
}
</script>

<style>
.desc{
    color: #999;
    font-size: 12px;
}
.flex-btn{
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
}
.flex-tags {
    display: flex;
    gap: 2px;
    flex-wrap: wrap;
}
.flex-btn .el-button.is-text{
    margin-left: 0;
}
.flex-tags-size{
    font-size: 12px;
}
.flex-tags-size span{
    display: block;
    margin-bottom: 5px;
    color: #999;
}
</style>