<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="certificate_id">
            <template #default="scope">
                打印
            </template>
        </el-table-column>
        <el-table-column label="打印类型" prop="color">
            <template #default="scope">
                <el-tag type="danger" v-if="scope.row.color">{{ colorData[scope.row.color] }}</el-tag>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="单双面" prop="certificate_id">
            <template #default="scope">
                {{ duplexData[scope.row.duplex] }}
            </template>
        </el-table-column>
        <el-table-column label="数量" prop="rate">
            <template #default="scope">
                {{ scope.row.number }}张
            </template>
        </el-table-column>
        <el-table-column label="折扣" prop="rate">
            <template #default="scope">
                {{ scope.row.rate }}
            </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="right" fixed="right">
            <template #default="scope">
                <el-button-group>
                    <el-button text type="warning" size="small" @click="table_edit(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>
</template>

<script>
import formMain from "./form";
export default {
    components: {
        formMain
    },
    props: {
        type: { type: String, default: "1" }
    },
    data(){
        return {
            colorData:['','彩色','黑白'],
            duplexData:['','单面','双面'],
            list: {
                apiObj: this.$API.mDiscount.list
            },
            visible:false,
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{
                type:this.type
            }
        }
    },
    methods:{
        table_edit(data){
            this.$nextTick(() => {
                this.$refs.formMain.open("edit").setData(data)
            })
        },
        table_del(data){
            this.$confirm("确定要删除该数据吗？","删除确认",{type: 'warning'}).then(async ()=>{
                var resp = await this.$API.mDiscount.del.post({"id":data.id,"type":""});
                if (resp.code == 0) {
                    return this.$message.error(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);
        },
    }
}
</script>