<template>
    <el-dialog title="选择卡类型" :visible.sync="dialogFormVisible">
        <img :src="logo" style="display: block; margin: -50px auto 0px" alt="logo" />
        <el-form :model="yibaoinfo" :inline="true">
            <div style="text-align: center">
                <el-form-item label="卡类型">
                    <el-radio-group v-model="yibaoinfo.cardtype">
                        <el-radio label="02">身份证</el-radio>
                        <el-radio label="03">社会保障卡</el-radio>
                        <!-- <el-radio label="06">电子社会保障卡</el-radio> -->
                        <el-radio label="00">就诊卡</el-radio>
                        <el-radio label="01">电子医保凭证</el-radio>
                    </el-radio-group>
                </el-form-item>
            </div>
            <div>
                <!-- <el-form-item label="卡号">
                    <el-input v-model="yibaoinfo.cardno"></el-input>
                </el-form-item> -->
                <div style="text-align: center">
                    <el-form-item label="医保类型">
                        <el-radio-group v-model="yibaoinfo.channel">
                            <el-radio label="local">本地</el-radio>
                            <el-radio label="oil">油田</el-radio>
                            <el-radio label="offsite">异地</el-radio>
                            <el-radio label="longdistance">跨省异地</el-radio>
                        </el-radio-group>
                    </el-form-item>
                </div>
                <div style="text-align: center">
                    <el-input
                        v-if="yibaoinfo.cardtype == '01'"
                        v-model="adminpingzhengma"
                        type="text"
                        placeholder="请输入患者卡号"
                        @keyup.enter.native="prepare()"
                        ref="input"
                        style="width: 300px"
                    ></el-input>
                </div>
                <div style="text-align: center">
                    <el-input v-if="yibaoinfo.cardtype == '00'" v-model="jiuzhencard" type="text" placeholder="请输入患者卡号" @keyup.enter.native="prepare()" ref="input" style="width: 300px"></el-input>
                </div>
            </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="prepare()">确 定</el-button>
        </div>
    </el-dialog>
</template>

<script>
import { mzxx, mzxxs } from '@/api/mz'
import { getPatientInfo } from '@/api/patient'
import { getPatientInfo2 } from '@/api/jiesuan'
export default {
    name: 'SelectCard',
    props: {
        patientcard:Object,
        visible: Boolean,
        flag: String,
        xzbz: String,
    },
    data() {
        return {
            adminpingzhengma:'',
            jiuzhencard:'',
            logo: require('@/assets/logo/CardReader.gif'),
            form: {
                name: '',
                region: '',
                date1: '',
                date2: '',
                delivery: false,
                type: [],
                resource: '',
                desc: '',
            },
            formLabelWidth: '120px',
            yibaoinfo: {
                idcard: '',
                cardtype: '1',
                cardno: '',
                channel: 'local',
                sfzh: '',
                ksbm: '',
                xm: '',
            },
        }
    },
    computed: {
        dialogFormVisible: {
            get() {
                return this.visible
            },
            set(val) {
                this.$emit('update:visible', val)
            },
        },
    },
    mounted() {
        this.socketInit()
    },
    created() {
        if (!window.WebSocket) {
            alert('该版本浏览器不支持WebSocket')
        } else {
            console.log('支持websocket')
        }
        this.ws = new WebSocket('ws://127.0.0.1:9000/')
        // this.ws.onmessage = function (event) {
        //     const result = JSON.parse(event.data)
        //     //成功发送请求后
        //     if (result.RespCode == 0) {
        //         //赋值读取到的信息
        //         // this.searchyibao()

        //         //读取失败
        //     } else {
        //         alert(result.RespData)
        //     }
        // }
        this.ws.onerror = function (evt) {
            console.log('读卡器连接失败')
        }
    },

    methods: {
        //读取身份证按钮
        // readIdcard() {
        //   this.showName = "0";
        //   if (this.patient_id) {
        //     this.getPatientInfo();
        //   } else {
        //     this.socket.send('{ "MethodName": "OpenDevice" }');
        //     if (is_id_card) {
        //       this.socket.send('{ "MethodName": "ReadIDCard"}');
        //     } else {
        //       this.socket.send('{ "MethodName": "ReadSSSECard" }');
        //     }
        //   }
        // },
        socketInit() {
            this.ws = new WebSocket('ws://127.0.0.1:9000/')
            this.ws.onopen
            let self = this
            this.ws.onmessage = function (evt) {
                let res = JSON.parse(evt.data)
                if (res.MethodName == 'ReadIDCard' && res.RespCode != 0) {
                    setTimeout(() => {
                        self.readIdcard(false)
                    }, 200)
                }
                // 赋值调接口
                if (res.MethodName == 'ReadIDCard' && res.RespCode == 0) {
                    // 身份证读取
                    self.yibaoinfo.cardno = res.RespData.CardId
                    self.getmzxx('1')
                } else if (res.MethodName == 'ReadSSSECard' && res.RespCode == 0) {
                    // 社保卡读取
                    if (self.yibaoinfo.channel == 'offsite') {
                        self.yibaoinfo.cardno = res.RespData.sfzh
                    } else {
                        self.yibaoinfo.idcard = res.RespData.CardNo
                    }
                    self.yibaoinfo.sfzh = res.RespData.sfzh
                    self.yibaoinfo.ksbm = res.RespData.ksbm
                    self.yibaoinfo.xm = res.RespData.xm
                    self.getmzxx('2')
                } else {
                    // self.$message.error('患者信息读取失败！')
                    self.yibaoinfo.cardno = ''
                    return false
                }
                // self.searchyibao()
            }
        },
        //选择卡类型后操作
        prepare() {
            //有卡就读卡
            if (this.yibaoinfo.cardtype == '02') {
                //读身份证
                this.ws.send('{ "MethodName": "OpenDevice" }')
                this.ws.send('{ "MethodName": "ReadIDCard"}')

                //没卡就手写传卡号
            } else if (this.yibaoinfo.cardtype == '03') {
                //读医保卡
                this.ws.send('{ "MethodName": "OpenDevice" }')
                this.ws.send('{ "MethodName": "ReadSSSECard" }')
            }  else if (this.yibaoinfo.cardtype == '01') {
                this.getmzxx('3')
            } else if (this.yibaoinfo.cardtype == '00') {
                this.getmzxx('4')
            }
            // else {
            //     this.searchyibao()
            // }
        },
        getmzxx(e) {
            if (e == '1') {
                this.param = {
                    grbh: this.yibaoinfo.cardno,
                    xzbz: 'C',
                    yltclb: '0',
                    channel: this.yibaoinfo.channel,
                    card_type: this.yibaoinfo.cardtype,
                    from_web: '1',
                }
                mzxx(this.param)
                    .then(response => {
                        // this.patientInfo = response.data
                        // this.form.brxm = response.data.xm
                        // this.form.brxb = response.data.xb == '2' ? '女' : '男'
                        this.dialogFormVisible = false
                         this.$emit('getyibaoinfo', response.data)
                    })
                    .finally(() => {
                        this.dialogFormVisible = false
                    })
            }
            if (e == '2' && this.yibaoinfo.channel == 'offsite') {
                this.param = {
                    grbh: this.yibaoinfo.cardno,
                    xzbz: 'C',
                    yltclb: '0',
                    channel: this.yibaoinfo.channel,
                    card_type: '02',
                    from_web: '1',
                }
                mzxx(this.param)
                    .then(response => {
                        // this.patientInfo = response.data
                        // this.form.brxm = response.data.xm
                        // this.form.brxb = response.data.xb == '2' ? '女' : '男'
                        this.dialogFormVisible = false
                        this.$emit('getyibaoinfo', response.data)
                    })
                    .finally(() => {
                        this.dialogFormVisible = false
                    })
            }
            if (e == '2' && this.yibaoinfo.channel != 'offsite') {
                this.param = {
                    p_xzbz: this.patientcard.p_xzbz,
                    p_xm: this.yibaoinfo.xm,
                    p_sbm: this.yibaoinfo.ksbm,
                    p_gfbb: '03',
                    p_shbzhm: this.yibaoinfo.sfzh,
                    p_kh: this.yibaoinfo.idcard,
                    channel: this.yibaoinfo.channel,
                    p_ewm: '',
                    p_dzpzywm: '201',
                    card_type: this.yibaoinfo.cardtype,
                    from_web: '1',
                }
                mzxxs(this.param)
                    .then(response => {
                        // this.patientInfo = response.data
                        // this.form.brxm = response.data.xm
                        // this.form.brxb = response.data.xb == '2' ? '女' : '男'
                        this.dialogFormVisible = false
                        this.$emit('getyibaoinfo', response.data)
                    })
                    .finally(() => {
                        this.dialogFormVisible = false
                    })
            } if (e == '3') {
                this.param = {
                    p_xzbz: this.patientcard.p_xzbz,
                    p_xm: this.yibaoinfo.xm,
                    p_sbm: this.yibaoinfo.p_sbm,
                    p_gfbb: '03',
                    p_shbzhm: this.yibaoinfo.sfzhm,
                    p_kh: this.yibaoinfo.idcard,
                    channel: this.yibaoinfo.channel,
                    p_ewm: this.adminpingzhengma,
                    p_dzpzywm: '201',
                    card_type: this.yibaoinfo.cardtype,
                    from_web: '1',
                }
                mzxxs(this.param)
                    .then(response => {
                        // this.patientInfo = response.data
                        // this.form.brxm = response.data.xm
                        // this.form.brxb = response.data.xb == '2' ? '女' : '男'
                        this.dialogFormVisible = false
                        this.$emit('getyibaoinfo', response.data)
                    })
                    .finally(() => {
                        this.loading = false
                    })
            }
            if (e == '4') {
                this.param = {
                    grbh: this.jiuzhencard,
                    xzbz: 'C',
                    yltclb: '0',
                    channel: this.yibaoinfo.channel,
                    card_type: this.yibaoinfo.cardtype,
                }
                mzxx(this.param)
                    .then(response => {
                        // this.patientInfo = response.data
                        // this.form.brxm = response.data.xm
                        // this.form.brxb = response.data.xb == '2' ? '女' : '男'
                        this.dialogFormVisible = false
                        this.$emit('getyibaoinfo', response.data)
                    })
                    .finally(() => {
                        this.loading = false
                    })
            }
        },
        searchyibao() {
            //确定之后，如果有卡就是读卡，没卡传号码
            if (this.flag == 'one') {
                getPatientInfo({
                    grbh: this.yibaoinfo.cardno,
                    xzbz: this.xzbz,
                })
                    .then(response => {
                        //获取了医保信息，赋值界面
                        const yibao = response.data
                        this.$emit('getyibaoinfo', yibao)
                        this.dialogFormVisible = false
                    })
                    .finally(() => {})
            } else if (this.flag == 'two') {
                getPatientInfo2({
                    grbh: this.yibaoinfo.cardno,
                    xzbz: this.xzbz,
                    // cardNo: this.yibaoinfo.cardno,
                    // type: this.yibaoinfo.cardtype,
                    // xzbz: '', // todo 是否可默以医疗
                    // yltclb: '',
                    // ptmzskbz: '',
                    // shbzhm: '', //社会保号码 由his控制读卡器时，跨省导地读卡必传
                    // xm: '', //姓名 由his控制读卡器时，跨省导地读卡必传
                    // sbm: '', //卡识别码 由his控制读卡器时，跨导地读卡必传
                    // gfbb: '', //卡规范版本 由his控制读卡器时，跨省导地读卡必传
                    // ewm: '', // 子社保卡或医保电子证二维码号或身份信检索码(刷脸维码号时p kh传”刷脸巨地结控制刷脸设备时，传入刷脸
                    // dzpzywm: '', // 电子医保证业务码
                })
                    .then(response => {
                        //获取了医保信息，赋值界面
                        const yibao = response
                        this.$emit('getyibaoinfo', yibao)
                        this.dialogFormVisible = false
                    })
                    .finally(() => {})
            }
        },
    },
}
</script>

<style>
</style>
