<template>
    <v-layout>
        <v-flex xs12>
            <v-card class="mb-2" color="white">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>QC [{{xinstrument.name}}]</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <!--   <v-btn v-if="xinstrument.name != 'Belum Pilih Control'" @click="openFormQc(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>  -->
                    <v-icon v-if="xinstrument.name != 'Belum Pilih Control'" style="color:white" @click="simpanAll()">save</v-icon>
                </v-toolbar>
                <v-layout row style="background:#bbdefb;padding-top:5px;" justify-left>
                    <v-list-tile>
                        <input type="text" @click="selectMe(0)" v-model="xsearch" class="textinput" label="Nama Station" placeholder="Cari ..." />
                        <!--  <input type="text" readonly v-model="filterComputedDateFormattedStart" @blur="date = deFormatedDate(filterComputedDateFormattedStart)"
                            class="textinput" /> -->
                        <v-menu v-model="menufilterdatestart" :close-on-content-click="false" :nudge-right="40" lazy transition="scale-transition"
                            offset-y full-width max-width="290px" min-width="290px">
                            <template v-slot:activator="{ on }">
                           
                                    <input type="text" v-model="filterComputedDateFormattedStart" v-on="on" @blur="date = deFormatedDate(filterComputedDateFormattedStart)"
                            class="textinput" />
                            </template>
                            <v-date-picker v-model="xstartdate" no-title @input="menufilterdatestart = false"></v-date-picker>
                        </v-menu>
                        </v-list-tile-content>
                    </v-list-tile>
                </v-layout>
                <v-divider></v-divider>
                <v-layout row wrap class="scroll-container" style="max-height:600px;overflow: auto;">
                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                        <v-data-table :headers="headers" :items="qcs" :loading="isLoading" hide-actions class="elevation-1">
                            <template slot="items" slot-scope="props">
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">{{ props.item.Nat_TestName}}
                                    <p style="color:#800000" class="mb-0 font-weight-bold caption">{{props.item.Nat_InstrumentName}} | {{props.item.Nat_QcControlName}}</p>
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <div>
                                        <v-btn :disabled="xinstrument.name == 'Belum Pilih Control'" v-if="props.item.M_QcResultIsInstrument == 'N' || props.item.M_QcResultID == '0'"
                                            small color="error">
                                            Tidak</v-btn>
                                        <v-btn @click="nonactiveFormQc(props.item)" :disabled="xinstrument.name == 'Belum Pilih Control'" v-if="props.item.M_QcResultID != '0' && props.item.M_QcResultIsInstrument == 'Y'"
                                            small color="success">
                                            Ya</v-btn>
                                    </div>
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <p style="color:black" class="mb-0 font-weight-bold caption">{{props.item.Nat_QcMean}}</p>
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <p style="color:black" class="mb-0 font-weight-bold caption">{{props.item.Nat_QcSd}}</p>
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <p style="color:black" class="mb-0 font-weight-bold caption">{{props.item.Nat_QcTea}}</p>
                                </td>
                                <td class="text-xs-left pa-2" v-bind:class="{'amber lighten-4':isSelected(props.item)}" @click="selectMe(props.item)">
                                    <p v-if="xinstrument.name !== 'Belum Pilih Control' && props.item.haveResult === 'Y'" style="color:black" class="mb-0 font-weight-bold caption">{{props.item.M_QcResultValue}}<v-icon v-if="xinstrument.name !== 'Belum Pilih Control' && props.item.haveResult === 'Y'" style="color:green" title="histori result" class="text-sm-right" @click="historyResult(props.item)">more_vert</v-icon></p>
                                    <p v-if="xinstrument.name == 'Belum Pilih Control'" style="color:black" class="mb-0 font-weight-bold caption">{{props.item.M_QcResultValue}}</p>
                                    <v-text-field v-on:change="changeQc" v-if="xinstrument.name != 'Belum Pilih Control' && props.item.haveResult === 'N'" style="margin-top:0;padding-top:0"
                                        v-model="props.item.M_QcResultValue" hide-details></v-text-field>
                                        
                                </td>
                                <td class="text-xs-left pa-2"
                                    v-bind:class="{ 'amber lighten-4': isSelected(props.item) }"
                                    v-if="props.item.haveResult === 'Y'"
                                    @click="selectMe(props.item)">
                                    <v-btn @click="processQc(props.item)" color="blue en-3">QC Ulang</v-btn>
                                </td>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>
                <v-divider></v-divider>
                <v-pagination style="margin-top:10px;margin-bottom:10px" v-model="curr_page" :length="xtotal_page"></v-pagination>

                <template>
                    <v-layout row justify-center>
                        <v-dialog v-model="dialogqc" persistent max-width="750px">
                            <v-card>
                                <v-card-title>
                                    <span class="headline">QC ULANG</span>
                                </v-card-title>
                                <v-card-text class="pt-0 pb-0">
                                    <v-form ref="formcontrolqc" v-model="validqc" lazy-validation>
                                        <v-layout wrap>
                                            <v-flex xs12>
                                                <v-layout wrap>
                                            <v-flex xs6 pa-2>
                                                <v-text-field readonly v-model="xvalueold" label="Value Lama"></v-text-field>
                                            </v-flex>
                                            <v-flex xs6 pa-2>    
                                                <v-text-field v-model="xvalue" label="Value Baru*"></v-text-field>
                                            </v-flex>
                                                </v-layout>
                                            </v-flex>
                                            <v-flex xs12>
                                                <v-textarea v-model="xreason" label="Alasan QC Ulang*"></v-textarea>
                                            </v-flex>
                                            <v-flex xs12>
                                                <v-textarea v-model="xsolusi" label="Solusi Perbaikan*"></v-textarea>
                                            </v-flex>
                                            <v-flex>
                                                <p v-for="(xerror,idx) in xerrors" class="error pl-2 pr-2" style="color:#fff">{{xerror.msg}}</p>
                                            </v-flex>
                                        </v-layout>
                                </v-card-text>
                                <v-card-actions>
                                    <v-spacer></v-spacer>
                                    <v-btn color="blue darken-1" flat @click="updateDialogFormQc()">Tutup</v-btn>
                                    <v-btn color="blue darken-1" flat @click="saveFormQc()">Simpan</v-btn>
                                </v-card-actions>
                                </v-form>
                            </v-card>
                        </v-dialog>
                    </v-layout>
                </template>

            </v-card>
        </v-flex>

        <one-dialog-print :title="printtitle" :width="printwidth" :height="500" :status="openprint" :urlprint="urlprint" @close-dialog-print="closePrint"></one-dialog-print>
        <template>

            <v-dialog v-model="dialogdeletealertqc" max-width="30%">
                <v-card>
                    <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                        Konfirmasi
                    </v-card-title>
                    <v-card-text class="pt-2 pb-2">
                        <v-layout row>
                            <v-flex xs12 d-flex>
                                <v-layout row>
                                    <v-flex pb-1 xs12>
                                        <v-layout row>
                                            <v-flex pt-2 pr-2 xs12>
                                                {{msgalertqc}}
                                            </v-flex>
                                        </v-layout>
                                    </v-flex>
                                </v-layout>
                            </v-flex>
                        </v-layout>
                    </v-card-text>
                    <v-divider></v-divider>
                    <v-card-actions>
                        <v-spacer></v-spacer>
                        <v-btn color="primary" flat @click="dialogdeletealertqc = false">
                            Tutup
                        </v-btn>
                        <v-btn color="primary" flat @click="closeActiveAlertQc()">
                            Yakin lah
                        </v-btn>
                    </v-card-actions>
                </v-card>
            </v-dialog>

        </template>
        <template>
            <v-layout row justify-center>
                <v-dialog v-model="dialoghistory" persistent max-width="1050px">
                    <v-card>
                        <v-card-title>
                            <span class="headline">HISTORI RESULT</span><span> &nbsp;[ <span style="color:#2196F3;font-weight: 900;"></span>&nbsp;]
                        </v-card-title>
                        <v-layout row wrap>
                            <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                                <v-data-table :headers="headerhistory" :items="histories" hide-actions class="elevation-1">
                                    <template slot="items" slot-scope="props">
                                        <td class="text-xs-left pa-2" v-html="props.item.tanggal">
                                        </td>
                                        <td class="text-xs-left pa-2" v-html="props.item.M_QcResultLogValue">
                                        </td>
                                        <td class="text-xs-left pa-2" v-html="props.item.M_QcResultLogReason">
                                        </td>
                                        <td class="text-xs-left pa-2" v-html="props.item.M_QcResultLogSolution">
                                        </td>
                                        <td class="text-xs-left pa-2">
                                            <v-btn v-if="props.item.M_QcResultLogIsGraphic === 'Y'" color="green en-3">Tampilkan di Grafik</v-btn>
                                            <v-btn v-if="props.item.M_QcResultLogIsGraphic === 'N'" @click="processGraphic(props.item)" color="grey en-3">Tampilkan di Grafik</v-btn>
                                            <v-btn v-if="props.item.M_QcResultLogIsGraphic === 'Y'" @click="processGraphic(props.item)" color="red en-3">Batalkan</v-btn>
                                        </td>
                                    </template>
                                </v-data-table>
                            </v-flex>
                        </v-layout>
                        <v-card-actions>
                            <v-spacer></v-spacer>
                            <v-btn color="primary" flat @click="closeViewHistory()">
                                Tutup
                            </v-btn>
                        </v-card-actions>
                    </v-card>
                </v-dialog>
            </v-layout>
        </template>
        <one-dialog-alert :status="openalertconfirmation" :msg="msgalertconfirmation" @forget-dialog-alert="forgetAlertConfirmation()"
            @close-dialog-alert="closeAlertConfirmation()"></one-dialog-alert>
    </v-layout>
</template>

<style scoped>
    .searchbox .v-input.v-text-field .v-input__slot {
        min-height: 60px;
    }

    .searchbox .v-btn {
        min-height: 60px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }

    .textinput {
        -webkit-transition: width 0.4s ease-in-out;
        transition: width 0.4s ease-in-out;
        background-color: white;
        background-position: 10px 10px;
        background-repeat: no-repeat;
        padding-left: 40px;
        width: 100%;
        padding: 8px 10px;
        margin-bottom: 5px;
        box-sizing: border-box;
        border: 1px solid #607d8b;

    }

    .textinput:focus {
        width: 100%;
    }

    .textinput:focus::-webkit-input-placeholder {
        color: transparent;
    }

    .textinput:focus::-moz-placeholder {
        color: transparent;
    }

    .textinput:-moz-placeholder {
        color: transparent;
    }

    .boxoutline {
        color: red;
        border: 1px solid red;
        justify-content: left;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #ffffff;
        font-size: 10px;
        font-weight: 50;
        word-wrap: break-word;
        border-radius: 1px
    }

    .boxoutline:hover {
        background: rgba(0, 0, 0, 0.07) !important;
        font-size: 15px;
        font-weight: 700;
    }

    .boxsolid {
        color: #ffffff;
        border: 1px solid #ffffff;
        justify-content: center;
        height: 45px;
        line-height: 45px;
        padding-left: 10px;
        background: #f44336;
        font-size: 14px;
        font-weight: 500;
        border-radius: 1px
    }

    .boxsolid:hover {
        background: #f44336de;
        font-size: 15px;
        font-weight: 700;
    }

    .scroll-container {
        scroll-padding: 50px 0 0 50px;
    }

    ::-webkit-scrollbar {
        width: 7px;
    }

    /* this targets the default scrollbar (compulsory) */

    ::-webkit-scrollbar-track {
        background-color: #73baf3;
    }

    /* the new scrollbar will have a flat appearance with the set background color */

    ::-webkit-scrollbar-thumb {
        background-color: #2196f3;
    }

    /* this will style the thumb, ignoring the track */

    ::-webkit-scrollbar-button {
        background-color: #0079da;
    }

    /* optionally, you can style the top and the bottom buttons (left and right for horizontal bars) */

    ::-webkit-scrollbar-corner {
        background-color: black;
    }

    /* if both the vertical and the horizontal bars appear, then perhaps the right bottom corner also needs to be styled */
</style>

<script>
    module.exports = {
        components: {
            'one-date-picker': httpVueLoader('../../common/oneDatePicker.vue'),
            'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue'),
            'one-dialog-alert': httpVueLoader('../../common/oneDialogAlert.vue')
        },
        data() {
            return {
                query: "",
                items: [],
                openprint: false,
                urlprint: '',
                printtitle: '',
                printwidth: 600,
                formatreport: 'pdf',
                readonlydefault: false,
                page: 1,

                xvalueold:'',
                xreason:'',
                xsolusi:'',
                xvalue:'',
                isview: false,
                xname: '',
                xphone: '',
                xcode: '',
                xdescription: '',
                xipdescription: '',
                xbasename: '',
                isdefault: false,
                issatellite: false,
                search_control: '',
                search_city: '',
                search_district: '',
                search_kelurahan: '',
                search_company: '',
                search_mou: '',
                search_doctor: '',
                search_test: '',
                menufilterdatestart: false,
                date: new Date().toISOString().substr(0, 10),
                xnameRules: [
                    v => !!v || 'Nama harus diisi'
                ],
                xcodeRules: [
                    v => !!v || 'Kode harus diisi'
                ],
                descriptionRules: [
                    v => !!v || 'Alamat harus diisi'
                ],
                xipdescriptionRules: [
                    v => !!v || 'Alamat IP harus diisi'
                ],
                controlRules: [
                    v => !!v || 'Control harus dipilih'
                ],
                cityRules: [
                    v => !!v || 'Kota harus dipilih'
                ],
                districtRules: [
                    v => !!v || 'Kecamatan harus dipilih'
                ],
                kelurahanRules: [
                    v => !!v || 'Kelurahan harus dipilih'
                ],
                headers: [{
                        text: "PEMERIKSAAN",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "30%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "INSTRUMENT",
                        align: "left",
                        sortable: false,
                        value: "lab",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "MEAN",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "SD",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "TEA(%)",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "VALUE",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "left",
                        sortable: false,
                        value: "name",
                        width: "10%",
                        class: "blue lighten-3 white--text"
                    }
                ],
                headerhistory: [{
                        text: "WAKTU QC",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "7%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "VALUE",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "ALASAN QC ULANG",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "SOLUSI PERBAIKAN",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "15%",
                        class: "pa-2 blue lighten-3 white--text"
                    },
                    {
                        text: "AKSI",
                        align: "center",
                        sortable: false,
                        value: "mr",
                        width: "10%",
                        class: "pa-2 blue lighten-3 white--text"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 100,
                    sortBy: 'Nat_QcName ASC',
                    totalItems: this.$store.state.qc.total_transactions
                },
                statuss: [{
                        waktu: '16-07-2019 10:00',
                        status: 'Order Dibuat',
                        qc: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 11:00',
                        status: 'Order Dikirim',
                        qc: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 12:00',
                        status: 'Order dikonfirmasi',
                        qc: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 13:00',
                        status: 'Telah dibuat Surat Jalan',
                        qc: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 14:00',
                        status: 'Kurir akan mengambil sampel',
                        qc: 'Satria Subiantoro'
                    }
                ],
                color: "success",
                validqc: false,
                xid: 0,
                isinput: "N",
                countprice: 0,
                qcid: 0,
                qcxname: "",
                qclang: "",
                qctext: "",
                isadvance: "N",
                qcformatadvance: "",
                issenttocontrol: "N",
                menustartdate: false,
                menuenddate: false,
                date: new Date().toISOString().substr(0, 10),
                msgalertconfirmation: "Perubahan yang telah dilakukan belum disimpan dong !",
                dialogdeletealertqc: false,
                dialogverifyalertqc: false,
                dialogreleasealertqc: false,
                alertverify: false,
                dialogunreleasealertqc: false,
                dialogunverifyalertqc: false,
                msgalertqc: ""
            };
        },
        mounted() {},
        computed: {
            qcs() {
                return this.$store.state.qc.qcs
            },
            histories() {
                return this.$store.state.qc.histories
            },
            xcontrol() {
                return this.$store.state.control.selected_control
            },
            xinstrument() {
                return this.$store.state.instrument.selected_instrument
            },
            dialogqc() {
                return this.$store.state.qc.dialog_form_qc
            },
            xerrors() {
                return this.$store.state.qc.errors
            },
            isLoading() {
                return this.$store.state.qc.search_status == 1
            },
            filterComputedDateFormattedStart() {
                return this.formatDate(this.xstartdate)
            },
            dialoghistory() {
                return this.$store.state.qc.dialog_history
            },
            xstartdate: {
                get() {
                    return this.$store.state.qc.startdate
                },
                set(val) {
                    this.$store.commit("qc/update_startdate", val)
                }
            },
            endComputedDateFormatted() {
                return this.formatDate(this.xenddate)
            },
            xenddate: {
                get() {
                    return this.$store.state.qc.enddate
                },
                set(val) {
                    this.$store.commit("qc/update_enddate", val)
                }
            },
            xorderautotests() {
                return this.$store.state.qc.orderautotests
            },
            xorderautotest: {
                get() {
                    return this.$store.state.qc.orderautotest
                },
                set(val) {
                    this.$store.commit("qc/update_orderautotest", val)
                }
            },
            xordertests() {
                return this.$store.state.qc.ordertests
            },
            xautocitys() {
                return this.$store.state.qc.autocitys
            },
            xautocity: {
                get() {
                    return this.$store.state.qc.autocity
                },
                set(val) {
                    this.$store.commit("qc/update_autocity", val)
                    this.$store.dispatch("qc/searchdistrict", {
                        search: this.search_district,
                        id: val.M_CityID
                    })
                }
            },
            xautodistricts() {
                return this.$store.state.qc.autodistricts
            },
            xautodistrict: {
                get() {
                    return this.$store.state.qc.autodistrict
                },
                set(val) {
                    this.$store.commit("qc/update_autodistrict", val)
                    this.$store.dispatch("qc/searchkelurahan", {
                        search: this.search_kelurahan,
                        id: val.M_DistrictID
                    })
                }
            },
            xautokelurahans() {
                return this.$store.state.qc.autokelurahans
            },
            xautokelurahan: {
                get() {
                    return this.$store.state.qc.autokelurahan
                },
                set(val) {
                    this.$store.commit("qc/update_autokelurahan", val)
                }
            },
            xautocompanys() {
                return this.$store.state.qc.autocompanys
            },
            xautocompany: {
                get() {
                    return this.$store.state.qc.autocompany
                },
                set(val) {
                    this.$store.commit("qc/update_autocompany", val)
                    this.$store.dispatch("qc/searchmou", {
                        search: this.search_mou,
                        id: val.M_CompanyID
                    })
                }
            },
            xautomous() {
                return this.$store.state.qc.automous
            },
            xautomou: {
                get() {
                    return this.$store.state.qc.automou
                },
                set(val) {
                    this.$store.commit("qc/update_automou", val)
                }
            },
            xautodoctors() {
                return this.$store.state.qc.autodoctors
            },
            xautodoctor: {
                get() {
                    return this.$store.state.qc.autodoctor
                },
                set(val) {
                    this.$store.commit("qc/update_autodoctor", val)
                    this.$store.dispatch("qc/selectdescriptiondoctor", {
                        id: val.M_DoctorID
                    })
                }
            },
            xdescriptiondoctors() {
                return this.$store.state.qc.descriptiondoctors
            },
            xdescriptiondoctor: {
                get() {
                    return this.$store.state.qc.descriptiondoctor
                },
                set(val) {
                    this.$store.commit("qc/update_descriptiondoctor", val)
                }
            },
            xstatuss() {
                return this.$store.state.qc.statuss
            },
            dialogstatusorder() {
                return this.$store.state.qc.dialog_status_order
            },
            xautocontrols() {
                return this.$store.state.qc.autocontrols
            },
            xautocontrol: {
                get() {
                    return this.$store.state.qc.autocontrol
                },
                set(val) {
                    this.$store.commit("qc/update_autocontrol", val)
                }
            },
            xsearch: {
                get() {
                    return this.$store.state.qc.x_search
                },
                set(val) {
                    this.$store.commit("qc/update_x_search", val)
                }
            },
            no_save: {
                get() {
                    return this.$store.state.qc.no_save
                },
                set(val) {
                    this.$store.commit("qc/update_no_save", val)
                }
            },
            openalertconfirmation: {
                get() {
                    return this.$store.state.qc.open_alert_confirmation
                },
                set(val) {
                    this.$store.commit("qc/update_open_alert_confirmation", val)
                }
            },
            curr_page: {
                get() {
                    return this.$store.state.qc.current_page
                },
                set(val) {
                    if (this.$store.state.qc.no_save == 0) {
                        if (this.xinstrument.name == 'Belum Pilih Control') {
                            this.$store.commit("qc/update_current_page", val)
                            this.$store.dispatch("qc/lookupbyname", {
                                control: this.$store.state.control.x_search,
                                instrument: this.$store.state.instrument.x_search,
                                sdate: this.xstartdate,
                                qc: this.xsearch,
                                current_page: val,
                                lastid: -1
                            })
                        } else {
                            this.$store.commit("qc/update_current_page", val)
                            this.$store.dispatch("qc/lookupbyid", {
                                instrumentid: this.$store.state.instrument.selected_instrument.Nat_QcNat_InstrumentID,
                                controlid: this.$store.state.instrument.selected_instrument.Nat_QcNat_QcControlID,
                                px: this.xsearch,
                                sdate: this.xstartdate,
                                current_page: val,
                                lastid: -1
                            })
                        }
                    } else {
                        this.$store.commit("qc/update_open_alert_confirmation", true)
                    }


                }
            },
            xtotal_page: {
                get() {
                    return this.$store.state.qc.total_data
                },
                set(val) {
                    this.$store.commit("qc/update_total_data", val)
                }
            }
        },
        methods: {
            isSelected(p) {
                return p.id == this.$store.state.qc.selected_qc.id
            },
            historyResult(data) {
                this.$store.dispatch("qc/history", {
                    id: data.M_QcResultID
                })
                this.$store.commit("qc/update_dialog_history", true)
            },
            closeViewHistory() {
                this.$store.commit("qc/update_dialog_history", false)
            },
            processQc(val) {
            this.in_saving = false;
            this.xid = val.M_QcResultID
            this.qcid = val.M_QcID
            this.xvalueold = val.M_QcResultValue
            this.xvalue = ""
            this.xreason = ""
            this.xsolusi = ""
            this.isview = false
            this.$store.commit("qc/update_dialog_form_qc", true)
        },
            selectMe(sc) {
                if (sc == '0' && this.xinstrument.name == 'Belum Pilih Control') {
                    this.$store.commit("qc/update_selected_qc", {
                        name: ""
                    })
                } else {
                    this.$store.commit("qc/update_selected_qc", sc)
                }


            },
            updateDialogFormQc() {
                this.$store.commit("qc/update_dialog_form_qc", false)
            },
            updateDialogStatusOrder() {
                this.$store.commit("qc/update_dialog_status_order", false)
            },
            setStatusOrder(val) {
                this.$store.commit("qc/update_qcs", {})
                this.$store.commit("qc/update_dialog_status_order", true)
                this.$store.commit("qc/update_statuss", val.statuss)
            },
            addTest() {
                var oldArr = this.$store.state.qc.ordertests
                oldArr.push(this.$store.state.qc.orderautotest)
                this.searchtest = ''
                this.$store.commit("qc/update_orderautotests", [])
                this.$store.commit("qc/update_orderautotest", {})
            },
            deleteDataPx(idx) {
                var inx = parseInt(idx);
                var xrow = this.$store.state.qc.ordertests[inx]
                if (parseInt(xrow.xid) !== 0) {
                    var xdeltest = this.$store.state.qc.deleted_ordertests
                    xdeltest.push(xrow)
                    this.$store.commit("qc/update_deleted_ordertests", xdeltest)
                }
                var oldArr = this.$store.state.qc.ordertests
                oldArr.splice(inx, 1)
                this.$store.commit("qc/update_ordertests", oldArr)
            },
            doPriceList(val) {
                console.log(location)
                var id = val.id
                location.replace("/one-ui/control/vuex/one-md-price/" + "?id=" + id)
            },
            doPrice() {
                console.log(location)
                var id = this.xid
                location.replace("/one-ui/control/vuex/one-md-price/" + "?id=" + id)
            },
            thr_search_control: _.debounce(function () {
                this.$store.dispatch("qc/searchcontrol", {
                    tes: this.search_control
                })
            }, 200),
            thr_search_test: _.debounce(function () {
                this.$store.dispatch("qc/searchtest", {
                    search: this.search_test,
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_QcNat_InstrumentID,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcNat_QcControlID,
                    tests: this.xordertests
                })
            }, 200),
            thr_search_city: _.debounce(function () {
                this.$store.dispatch("qc/searchcity", {
                    search: this.search_city
                })
            }, 200),
            thr_search_district: _.debounce(function () {
                this.$store.dispatch("qc/searchdistrict", {
                    search: this.search_district,
                    id: this.xautocity.M_CityID
                })
            }, 200),
            thr_search_kelurahan: _.debounce(function () {
                this.$store.dispatch("qc/searchkelurahan", {
                    search: this.search_kelurahan,
                    id: this.xautodistrict.M_DistrictID
                })
            }, 200),
            thr_search: _.debounce(function () {
                if (this.xcontrol.name == 'Belum Pilih Instrument' && this.xinstrument.name === "Belum Pilih Control") {
                    this.$store.dispatch("control/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.dispatch("instrument/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.dispatch("qc/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    sdate: this.xstartdate,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.commit("instrument/update_selected_instrument", {
                    name: "Belum Pilih Control"
                })
            } else if (this.xinstrument.name == 'Belum Pilih Control' && this.xcontrol.name !== "Belum Pilih Instrument") {

                this.$store.dispatch("qc/lookupbyidname", {
                    control: this.xcontrol.id,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    sdate: this.xstartdate,
                    current_page: 1,
                    lastid: -1
                })
            } else {
                this.$store.dispatch("qc/lookupbyid", {
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_QcNat_InstrumentID,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcNat_QcControlID,
                    px: this.xsearch,
                    sdate: this.xstartdate,
                    current_page: 1,
                    lastid: -1
                })
            }



                
            }, 200),
            thr_sdate: _.debounce(function () {
                if (this.xcontrol.name == 'Belum Pilih Instrument' && this.xinstrument.name === "Belum Pilih Control") {
                    this.$store.dispatch("control/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.dispatch("instrument/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.dispatch("qc/lookupbyname", {
                    control: this.$store.state.control.x_search,
                    instrument: this.$store.state.instrument.x_search,
                    sdate: this.xstartdate,
                    qc: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
                this.$store.commit("instrument/update_selected_instrument", {
                    name: "Belum Pilih Control"
                })
            } else if (this.xinstrument.name == 'Belum Pilih Control' && this.xcontrol.name !== "Belum Pilih Instrument") {

                this.$store.dispatch("qc/lookupbyidname", {
                    control: this.xcontrol.id,
                    instrument: this.$store.state.instrument.x_search,
                    qc: this.xsearch,
                    sdate: this.xstartdate,
                    current_page: 1,
                    lastid: -1
                })
            } else {
                this.$store.dispatch("qc/lookupbyid", {
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_QcNat_InstrumentID,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcNat_QcControlID,
                    px: this.xsearch,
                    sdate: this.xstartdate,
                    current_page: 1,
                    lastid: -1
                })
            }
            }, 200),
            thr_search_company: _.debounce(function () {
                this.$store.dispatch("qc/searchcompany", {
                    search: this.search_company
                })
            }, 200),
            thr_search_mou: _.debounce(function () {
                this.$store.dispatch("qc/searchmou", {
                    search: this.search_mou,
                    id: this.xautocompany.M_CompanyID
                })
            }, 200),
            thr_search_doctor: _.debounce(function () {
                this.$store.dispatch("qc/searchdoctor", {
                    search: this.search_doctor
                })
            }, 200),
            searchControl() {
                this.$store.dispatch("qc/lookup", {
                    id: this.xcontrol.Nat_InstrumentName === "Belum Pilih Control" ? "0" : this.$store.state
                        .control.selected_control
                        .id,
                    sdate: this.xstartdate,
                    search: this.xsearch,
                    current_page: 1,
                    lastid: -1
                })
            },
            doPrint() {
                console.log('doprint')
                this.printwidth = 1028
                this.printtitle = ""
                let qc = one_qc()
                var id = this.xid
                var d = new Date();
                var n = d.getTime()
                var rptname = 'control_price'
                var formatrpt = this.formatreport

                this.urlprint = "/birt/run?__report=report/one/md/" + rptname +
                    ".rptdesign&__format=" +
                    formatrpt + "&PQcID=" + id + "&qcxname=" + qc.Nat_QcQcname +
                    "&tm=" + n

                this.openprint = true
            },
            closePrint() {
                this.openprint = false
            },
            openFormQc(val) {
                this.xid = val
                this.$store.commit("qc/update_ordertests", [])
                this.$store.commit("qc/update_dialog_form_qc", true)
            },
            editFormQc(val) {
                this.xid = val.id
                this.xname = val.Nat_QcName
                this.xdescription = val.Nat_QcDescription
                this.$store.commit("qc/update_autocontrols", [{
                    Nat_QcControlID: val.Nat_QcControlID,
                    Nat_QcControlName: val.Nat_QcControlName
                }])
                this.$store.commit("qc/update_autocontrol", {
                    Nat_QcControlID: val.Nat_QcControlID,
                    Nat_QcControlName: val.Nat_QcControlName
                })
                this.$store.commit("qc/update_dialog_form_qc", true)
            },
            checkError(value) {
                var errors = this.$store.state.qc.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            forgetAlertConfirmation() {
                this.$store.commit("qc/update_no_save", 0)
                this.$store.commit("qc/update_open_alert_confirmation", false)
            },
            closeAlertConfirmation() {
                this.$store.commit("qc/update_open_alert_confirmation", false)
            },
            changeQc() {
                this.$store.commit("qc/update_no_save", 1)
            },
            saveFormQc() {
                if (this.$refs.formcontrolqc.validate()) {
                    this.$store.dispatch("qc/savereqc", {
                        id: this.xid,
                        mqcid: this.qcid,
                        controlid: this.$store.state.instrument.selected_instrument.Nat_QcControlID,
                    controlname: this.$store.state.instrument.selected_instrument.Nat_QcControlName,
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_InstrumentID,
                    instrumentname: this.$store.state.instrument.selected_instrument.Nat_InstrumentName,
                    isview: this.isview === true ? "Y" : "N",
                    tests: this.$store.state.qc.qcs,
                    sdate: this.xstartdate,
                        qcvalue : this.xvalue,
                        reason: this.xreason,
                        solution: this.xsolusi,
                        scontrol: this.$store.state.control.x_search,
                    sinstrument: this.xsearch
                    })
                }

            },
            simpanAll() {
                this.$store.dispatch("qc/saveall", {
                    xid: this.xid,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcControlID,
                    controlname: this.$store.state.instrument.selected_instrument.Nat_QcControlName,
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_InstrumentID,
                    instrumentname: this.$store.state.instrument.selected_instrument.Nat_InstrumentName,
                    tests: this.$store.state.qc.qcs,
                    sdate: this.xstartdate,
                    scontrol: this.$store.state.control.x_search,
                    sinstrument: this.xsearch
                })

            },
            processGraphic(data) {
                this.$store.dispatch("qc/savegraphic", {
                    id: data.M_QcResultLogID,
                    resultid: data.M_QcResultLogM_QcResultID,
                    qcvalue: data.M_QcResultLogValue,
                    reason : data.M_QcResultLogReason,	
                    solution : data.M_QcResultLogSolution,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcControlID,
                    controlname: this.$store.state.instrument.selected_instrument.Nat_QcControlName,
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_InstrumentID,
                    instrumentname: this.$store.state.instrument.selected_instrument.Nat_InstrumentName,
                    tests: this.$store.state.qc.qcs,
                    sdate: this.xstartdate,
                    scontrol: this.$store.state.control.x_search,
                    sinstrument: this.xsearch,
                    isgrafik : data.M_QcResultLogIsGraphic
                })

            },
            set_date2(x) {
                this.startdate = x.new_date
            },
            set_date3(x) {
                this.enddate = x.new_date
            },
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            updateAlert_success(val) {
                this.$store.commit("control/update_alert_success", val)
            },
            activeFormQc(data) {
                this.xid = data.id

                this.msgalertqc = "Yakin, mau mengaktifkan qc ini?"
                this.dialogdeletealertqc = true
            },
            closeActiveAlertQc() {
                this.$store.dispatch("qc/activeqc", {
                    xid: this.xid,
                    controlid: this.$store.state.instrument.selected_instrument.Nat_QcControlID,
                    controlname: this.$store.state.instrument.selected_instrument.Nat_QcControlName,
                    instrumentid: this.$store.state.instrument.selected_instrument.Nat_InstrumentID,
                    instrumentname: this.$store.state.instrument.selected_instrument.Nat_InstrumentName,
                    name: this.xname,
                    scontrol: this.$store.state.control.x_search,
                    sqc: this.xsearch
                })
                this.dialogdeletealertqc = false
            }
        },
        watch: {
            xsearch(val, old) {
                console.log(val)
                this.xsearch = val
                this.thr_search()

            },
            xstartdate(val, old) {
                console.log(val)
                this.xstartdate = val
                this.thr_sdate()
            },
            search_control(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_control()
            },
            search_test(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_test()
            },
            search_city(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_city()
            },
            search_district(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_district()
            },
            search_kelurahan(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_kelurahan()
            },
            search_company(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_company()
            },
            search_mou(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_mou()
            },
            search_doctor(val, old) {
                if (val == old) return
                if (!val) return
                if (val.length < 1) return
                if (this.$store.state.qc.update_autocomplete_status == 1) return
                this.thr_search_doctor()
            }
        }
    }
</script>