<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="商品ID" width="180" fixed="left" prop="goods_id">
            <template #default="scope">
                {{ scope.row.goods_id?scope.row.goods_id:'审核中' }}
            </template>
        </el-table-column>
        <el-table-column label="商品类型" prop="types" width="100"></el-table-column>
        <el-table-column label="商品信息" width="340" prop="name">
            <template #default="scope">
                <div class="goods-img">
                    <div class="img">
                        <el-image 
                        style="width: 54px; height: 54px"
                        :src="scope.row.image_list[0].url"
                        :zoom-rate="1.2"
                        :max-scale="7"
                        :min-scale="0.2"
                        :preview-src-list="[scope.row.image_list[0].url]"
                        preview-teleported
                        z-index="999"
                        fit="cover"></el-image>
                    </div>
                    <div class="name">
                        <span>{{ scope.row.product_name }}</span>
                        <span class="dec">商品编码：{{ scope.row.product_id }}</span>
                    </div>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="价格" width="200" prop="id">
            <template #default="scope">
                <div class="price">{{ $TOOL.moneyFormat(scope.row.price) }}</div>
                <div class="goods-price">
                    <div class="del">{{ $TOOL.moneyFormat(scope.row.line_price) }}</div>
                    <span>{{ $TOOL.disFormat(scope.row.price,scope.row.line_price) }}折</span></div>
            </template>
        </el-table-column>
        <el-table-column label="剩余库存" prop="category_id" width="120">
            <template #default="scope">
                <span v-if="scope.row.limit_use_rule==1">{{ scope.row.use_num_per_consume }}</span>
                <span v-else>不限</span>
            </template>
        </el-table-column>
        <el-table-column label="状态" prop="enable" width="120" align="center">
            <template #default="scope">
                <div class="status-success" v-if="scope.row.status==1"><sc-status-indicator type="success"></sc-status-indicator> 售卖中</div>
                <div class="status-danger" v-if="scope.row.status==2"><sc-status-indicator type="danger"></sc-status-indicator> 已下架</div>
                <div class="status-info" v-if="scope.row.status==3"><sc-status-indicator type="danger"></sc-status-indicator> 已删除</div>
                <div class="status-danger" v-if="scope.row.status==0"><sc-status-indicator type="danger"></sc-status-indicator> 审核中</div>
                <div v-if="scope.row.status==4">
                    <el-popover :width="300" trigger="hover" placement="top-start" title="驳回原因">
                        <template #reference>
                            <div class="status-danger" v-if="scope.row.status==4"><sc-status-indicator type="danger"></sc-status-indicator> 审核驳回 <el-icon><el-icon-question-filled /></el-icon></div>
                        </template>
                        <div v-html="scope.row.reason"></div>
                    </el-popover>
                </div>
            </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_at" width="180"></el-table-column>
        <el-table-column label="操作" width="180" align="left" fixed="right">
            <template #default="scope">
                <el-button-group>
                    <el-button size="small" type="warning" text @click="table_life(scope.row)" v-if="scope.row.status == 1">下架</el-button>
                    <el-button size="small" type="warning" text @click="table_life(scope.row)" v-if="scope.row.status == 2">上架</el-button>
                </el-button-group>
                <el-button-group>
                    <el-button size="small" text @click="table_view(scope.row)" v-if="scope.row.status == 1 || scope.row.status == 4">编辑</el-button>
                    <el-button type="danger" text size="small" @click="table_del(scope.row)" v-if="scope.row.status == 2 || scope.row.status == 4">删除</el-button>
                    <el-button type="danger" text size="small" v-if="scope.row.status == 0">-</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </scTable>
    <formMain ref="formMain" @success="handleSuccess"></formMain>
</template>

<script>
import formMain from './form';
export default {
    components: {
        formMain
    },
    data(){
        return {
            list: {
                apiObj: this.$API.merGoods.list
            },
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{}
        }
    },
    
    methods: {
        table_auth(data){
            this.$nextTick(() => {
                this.$refs.comboMain.open("edit").setData(data)
            })
        },
        table_life(data){
            this.$confirm(`来客下架成功后，在小程序中将不展示该商品，同时也将无法二次上架`, '提示', {
                type: 'warning'
            }).then(async ()=>{
                var resp = await this.$API.merGoods.off.post({"id":data.id});
                if (resp.code == 0) {
                    return this.$message.error(resp.msg);
                }
                this.$message.success(resp.msg);
                this.$emit("success");
            }).catch(()=>{})
        },
        table_view(data){
            this.$router.push({path:"/merchant/goods/edit",query:{"product_id":data.product_id,"id":data.id,"spm":(new Date()).getTime()}})
        },
        table_del(data){
            this.$confirm(`删除商品后，如有对应订单也将一并会删除，数据删除后无法恢复，确定要执行吗？`, '提示', {
                type: 'warning'
            }).then(async ()=>{
                var resp = await this.$API.merGoods.del.post({"id":data.id});
                if (resp.code == 0) {
                    return this.$message.error(resp.msg);
                }
                this.$message.success(resp.msg);
                this.$emit("success");
            }).catch(()=>{})
        },
        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);
        },
        table_passwd(row){
            this.$nextTick(() => {
                this.$refs.userPasswd.open("edit").setData(row)
            })
        }
    }
}
</script>

<style>
.goods-img{display: flex;gap: 5px;}
.goods-img .name span{display: block;}
.goods-img .name span.dec{color: #999;font-size: 12px;}
.goods-price,.price{display: flex;align-items: center;gap: 5px;font-size: 14px;}
.goods-price .del{text-decoration: line-through;}
.goods-price span{background-color: #fff !important;
    border: 1px solid #d3d9e0 !important;
    border-radius: 4px;
    box-sizing: border-box;
    color: #6c737a !important;
    display: inline-flex;
    flex: none;
    font-size: 12px;
    line-height: 18px !important;
    padding: 1px 6px !important;text-decoration: none;}
</style>