<template>
    <scTable ref="table" :apiObj="list.apiObj" :params="searchKey" @selectionChange="selectionChange" row-key="id" border>
        <el-table-column type="selection" width="50" fixed="left"></el-table-column>
        <el-table-column label="代理名称" width="150" fixed="left" prop="truename"></el-table-column>
        <el-table-column label="联系人" width="150" prop="contact"></el-table-column>
        <el-table-column label="代理ID" width="150" prop="agent_id"></el-table-column>
        <el-table-column label="主账号登录账号" prop="truename" width="200">
            <template #default="scope">
                <span v-if="scope.row.user">{{ scope.row.user.username?scope.row.user.username:'-' }}</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.mobile">{{ scope.row.mobile }}</span>
                <span class="status-danger" v-else>-</span>
            </template>
        </el-table-column>
        <el-table-column label="到期时间" prop="vip_at" width="220"></el-table-column>
        <el-table-column label="创建时间" prop="create_at" width="180"></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-if="scope.row.status==2"><sc-status-indicator type="danger"></sc-status-indicator> 已冻结</div>
                <div class="status-danger" v-if="scope.row.status==3"><sc-status-indicator type="danger"></sc-status-indicator> 已到期</div>
            </template>
        </el-table-column>
        <el-table-column label="操作" width="200" align="right" fixed="right">
            <template #default="scope">
                <el-button-group>
                    <el-button text size="small" @click="table_user(scope.row)">用户管理</el-button>
                    <el-button text type="warning" size="small" @click="table_passwd(scope.row)">编辑</el-button>
                    <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                        <template #reference>
                            <el-button text type="danger" size="small">删除</el-button>
                        </template>
                    </el-popconfirm>                    
                </el-button-group>
            </template>
        </el-table-column>
    </scTable>
    <passwd ref="userPasswd"></passwd>
    <formMain ref="formMain" @success="handleSuccess"></formMain>
    <userMain ref="userMain" @success="handleSuccess"></userMain>
</template>

<script>
import passwd from "@/views/manage/components/password";
import formMain from './form';
import userMain from '../user/list';
export default {
    components: {
        passwd,formMain,userMain
    },
    props: {
        type: { type: String, default: "1" }
    },
    data(){
        return {
            list: {
                apiObj: this.$API.agent.list
            },
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{}
        }
    },
    methods: {
        async table_del(data){
            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()
        },
        table_user(row){
            this.$nextTick(() => {
                this.$refs.userMain.open("edit").setData(row)
            })
        },
        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.formMain.open("edit").setData(row)
            })
        },
    }
}
</script>