<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" fixed="left">
            <template #default="scope">
                <span v-if="scope.row.name">{{ scope.row.name }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="机器码" prop="open_id" width="150">
            <template #default="scope">
                <span v-if="scope.row.code">{{ scope.row.code }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="打印类型" prop="out_id">
            <template #default="scope">
                <div v-if="scope.row.rule" class="flex-tag">
                    <el-tag type="success" v-for="item in scope.row.rule.type">{{ typeData[item] }}</el-tag>
                </div>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="支持纸张" prop="certificate_id">
            <template #default="scope">
                <div v-if="scope.row.rule" class="flex-tag">
                    <el-tag type="danger" v-for="item in scope.row.rule.paper_size">{{ item }}</el-tag>
                </div>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="支持颜色" prop="color">
            <template #default="scope">
                <div v-if="scope.row.rule" class="flex-tag">
                    <el-tag type="warning" v-for="item in scope.row.rule.color">{{ colorData[item] }}</el-tag>
                </div>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="支持单双面" prop="count">
            <template #default="scope">
                <div v-if="scope.row.rule" class="flex-tag">
                    <el-tag v-for="item in scope.row.rule.duplex">{{ duplexData[item] }}</el-tag>
                </div>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="取件方式" prop="order_amount">
            <template #default="scope">
                <div v-if="scope.row.rule" class="flex-tag">
                    <el-tag v-for="item in scope.row.rule.package">{{ packageData[item] }}</el-tag>
                </div>
                <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.print_status">{{ scope.row.print_status }}</span>
                <span class="status-danger" v-else>未设置</span>
            </template>
        </el-table-column>
        <el-table-column label="开启打印" prop="status" width="180">
            <template #default="scope">
                <el-switch
                    v-model="scope.row.status"
                    size="large"
                    inline-prompt
                    :active-value="1"
                    :inactive-value="2"
                    active-text="启用"
                    inactive-text="禁用"
                    @change="table_state(scope.row)"
                    style="--el-switch-on-color: #13ce66;"
                />
            </template>
        </el-table-column>
        <el-table-column label="操作" width="200" 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="success" size="small" @click="table_qrcode(scope.row)">二维码</el-button>
                    <el-button text type="danger" size="small" @click="table_price(scope.row)" v-if="scope.row.is_price==2">额外收费</el-button>
                </el-button-group>
            </template>
        </el-table-column>
    </scTable>
    <formMain ref="formMain" @success="handleSuccess"></formMain>
    <priceMain ref="priceMain" @success="handleSuccess"></priceMain>
    <el-dialog :title="qrcodeTitle" v-model="visible" :width="465" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false" :close-on-press-escape="false">
        <div class="qrcode-img">
            <img :src="qrcodeImg" />
        </div>
		<template #footer>
			<el-button @click="visible=false">关 闭</el-button>
		</template>
    </el-dialog>
</template>

<script>
import formMain from "./form";
import priceMain from "./price";
export default {
    components: {
        formMain,priceMain
    },
    data(){
        return {
            list: {
                apiObj: this.$API.mPrint.list
            },
            qrcodeImg:"",
            visible:false,
            qrcodeTitle:"",
            typeData:['','文档','照片'],
            colorData:['','彩色','黑白'],
            duplexData:['','单面','双面'],
            packageData:['','店内打印','远程自取','商家配送'],
            dataSelect:[],
            dataSelectFull:[],
            searchKey:{}
        }
    },
    methods: {
        async table_price(data){
            var loading = this.$loading();
            var resp = await this.$API.mPrint.price.get();
            loading.close()
            if (resp.code == 0) {
                return this.$message.error(resp.msg);
            }
            this.$nextTick(() => {
                this.$refs.priceMain.open("edit").setData({"id":data.id,"name":data.name,"price":resp.data})
            })
        },
        table_edit(data){
            this.$nextTick(() => {
                this.$refs.formMain.open("edit").setData(data)
            })
        },
        async table_state(data){
            var status = 1;
            if (data.status == 1) {
                status = 2;
            }
            var resp = await this.$API.mPrint.single.post({"id":data.id,"status":status});
            if (resp.code !== 1) {
                return this.$message.error(resp.msg);
            }
            this.$refs.table.refresh()
        },
        async table_qrcode(data){
            var loading = this.$loading();
            var resp = await this.$API.mPrint.qrcode.post({"id":data.id});
            loading.close()
            if (resp.code !== 1) {
                return this.$message.error(resp.msg);
            }
            this.visible = true;
            this.qrcodeTitle = "【"+data.name+"】打印二维码";
            this.qrcodeImg = resp.data.img;
        },
        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>
.flex-tag{display: flex;align-items: center;gap: 10px;}
</style>