<template>
    <vxe-modal v-model="value" id="addQuest" width="400" v-loading="load" @show="show" @hide="hide" height="200"
        show-footer title="智能审核设置">
        <by-form :propConfig="config" ref="form"></by-form>
        <template #footer>
            <div class="btn">
                <el-button plain size="small" @click="value = false">取消</el-button>
                <el-button type="primary" size="small" @click="btn">确定</el-button>
            </div>
        </template>
    </vxe-modal>
</template>

<script lang="ts">
import { Component, Prop, Vue, Watch } from "vue-property-decorator";
import { save } from '@/api/question'
import { autoAuditData } from "@/api/omsOrder";
@Component({ components: {} })
export default class AutoAudit extends Vue {
    value = false;
    load = false;
    config = {
        attr: { size: 'mini' },
        columns: [
            [{
                span: 16,
                label: '当前状态',
                prop: 'jobState',
                component: 'by-switch',
                compConfig: {
                    attr: {
                        activeText: '开启',
                        inactiveText: '关闭'
                    }
                }
            }, {
                span: 16,
                label: '时间',
                prop: 'scheduleConf',
                component: 'by-select',
                compConfig: {
                    attr: {
                        clearable: false,
                        defaultIndex: 0,
                        data: [{
                            label: '10分钟',
                            value: '0 */10 * * * ?'
                        }, {
                            label: '30分钟',
                            value: '0 */30 * * * ?'
                        }, {
                            label: '1小时',
                            value: '0 0 * * * ?'
                        }]
                    }
                }
            }]
        ]
    }
    show() {

    }
    hide() {
        (this.$refs.form as any).clearValidate();
        (this.$refs.form as any).clearValue();
    }
    setShow(v: boolean) {
        this.value = v;
        setTimeout(() => {
            this.autoAuditData();
        }, 200);
    }
    autoAuditData() {
        autoAuditData().then((res: any) => {
            if (res.code != 200) return;
            const resV = res.rows[0]
            const v = {
                jobState: resV.jobState === '1' ? true : false,
                scheduleConf: resV.time == '1' ? '0 */10 * * * ?' : resV.time == '2' ? '0 */30 * * * ?' : '0 0 * * * ?'
            };
            (this.$refs.form as any).setValue(v);
        })
    }

    btn() {
        let v = (this.$refs.form as any).getValue();
        if (v.scheduleConf === '0 */10 * * * ?') {
            v.time = '1'
        }
        if (v.scheduleConf === '0 */30 * * * ?') {
            v.time = '2'
        }
        if (v.scheduleConf === '0 0 * * * ?') {
            v.time = '3'
        }
        if (v.jobState === undefined || v.jobState === false) {
            v.jobState = 2
        } else {
            v.jobState = 1
        }
        this.$emit('handleAutoAudit', v);
        this.value = false;

        // (this.$refs.form as any).validate().then(() => {
        //     let data = (this.$refs.form as any).getValue();
        //     this.load = true
        //     save(data).then(() => {
        //         this.load = false;
        //         this.$emit('handleSuccess');
        //         this.value = false;
        //     }).catch(() => {
        //         this.load = false;
        //     })
        // })
    }
}
</script>

<style lang="scss" scoped></style>