<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="代理名称" width="150" fixed="left" prop="name"></el-table-column>
        <el-table-column label="代理ID" prop="agent_id"></el-table-column>
        <el-table-column label="到期时间" prop="vip_end"></el-table-column>
        <el-table-column label="联系人" prop="truename">
            <template #default="scope">
                <span v-if="scope.row.contact_name">{{ scope.row.contact_name }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="联系电话" prop="mobile" width="150">
            <template #default="scope">
                <span v-if="scope.row.contact_mobile">{{ scope.row.contact_mobile }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="可创建店铺" prop="status" width="120" align="center">
            <template #default="scope">
                <span>{{ scope.row.open_num }} 个</span>
            </template>
        </el-table-column>
        <el-table-column label="已创建店铺" prop="status" width="120" align="center">
            <template #default="scope">
                <span>{{ scope.row.store }} 个</span>
            </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" 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-else><sc-status-indicator type="danger"></sc-status-indicator> 禁用</div>
            </template>
        </el-table-column>
        <el-table-column label="注册时间" prop="create_at" width="180"></el-table-column>
        <el-table-column label="操作" width="200" align="center" fixed="right">
            <template #default="scope">
                <el-button-group>
                    <el-button text type="danger" size="small" @click="table_express(scope.row)">顺丰开平</el-button>
                    <el-button text type="primary" size="small" @click="table_view(scope.row)">编辑</el-button>
                    <el-button text type="danger" size="small" @click="table_del(scope.row)">删除</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </scTable>
    <formMain ref="formMain" @success="handleSuccess"></formMain>
    <expressMain ref="expressMain" @success="handleSuccess"></expressMain>
</template>

<script>
import formMain from './form';
import expressMain from './express';
export default {
    components: {
        formMain,expressMain
    },
    data(){
        return {
            list: {
                apiObj: this.$API.agent.list
            },
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{}
        }
    },
    
    methods: {
        table_express(data){
            this.$nextTick(() => {
                this.$refs.expressMain.open("edit").setData(data)
            })
        },
        table_view(data){
            this.$nextTick(() => {
                this.$refs.formMain.open("edit").setData(data)
            })
        },
        table_del(data){
            this.$confirm(`删除代理后，所有有关该代理的店铺、门店、授权、订单等信息都将删除，不可恢复，确定要执行删除吗`, '提示', {
                type: 'warning'
            }).then(async ()=>{
                var resp = await this.$API.agent.del.post({"id":data.id});
                if (resp.code == 0) {
                    return this.$message.warning(resp.msg);
                }
                this.$message.success(resp.msg);
                this.$refs.table.refresh()
            }).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>