<template>
    <div class="app-container">
        <el-tabs v-model="queryParams.routeCheckStatus" @tab-click="handleClick">
            <el-tab-pane label="未审核" name="UNAUDITED"></el-tab-pane>
            <el-tab-pane label="已审核" name="AGREE"></el-tab-pane>
            <el-tab-pane label="已忽略" name="DISAGREE"></el-tab-pane>
        </el-tabs>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="80px">
            <el-form-item label="患者姓名" prop="patientName">
                <el-input v-model="queryParams.patientName" placeholder="请输入患者姓名" clearable
                    @keyup.enter.native="handleQuery" style="width:200px" />
            </el-form-item>
            <el-form-item label="患者电话" prop="patientPhone">
                <el-input v-model="queryParams.patientPhone" placeholder="请输入患者电话" clearable
                    @keyup.enter.native="handleQuery" style="width:200px" />
            </el-form-item>
            <el-form-item label="出院/就诊时间" prop="dischargeTimeStart" label-width="120px">
                <el-date-picker v-model="dischargeTime" type="daterange" range-separator="至" start-placeholder="开始日期"
                    style="width:300px" value-format="yyyy-MM-dd" end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="诊断" prop="mainDiagnosis">
                <el-input v-model="queryParams.mainDiagnosis" placeholder="请选择诊断" clearable style="width:200px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="患者来源" prop="patientSource">
                <el-select v-model="queryParams.patientSource" placeholder="请选择患者来源" clearable style="width:200px">
                    <el-option v-for="dict in dict.type.patient_source" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="医院" prop="hospitalAgencyId">
                <el-select v-model="queryParams.hospitalAgencyId" filterable placeholder="请选择医院" style="width:200px"
                    clearable @clear="clearhospitalAgency" @change="changehospitalAgency">
                    <el-option v-for="item in hospitalAgencylist" :key="item.id" :label="item.agencyName"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="院区" prop="campusAgencyId" v-if="queryParams.hospitalAgencyId">
                <el-select v-model="queryParams.campusAgencyId" filterable placeholder="请选择院区" style="width:200px"
                    clearable @clear="clearcampusAgency" @change="changecampusAgency">
                    <el-option v-for="item in campusAgencylist" :key="item.id" :label="item.agencyName"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="科室" prop="departmentId" v-if="queryParams.hospitalAgencyId">
                <el-select v-model="queryParams.departmentId" filterable placeholder="请选择科室" style="width:200px"
                    clearable @clear="cleardepartment" @change="changedepartment">
                    <el-option v-for="item in departmentlist" :key="item.id" :label="item.departmentName"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="病区" prop="wardId" v-if="queryParams.hospitalAgencyId">
                <el-select v-model="queryParams.wardId" filterable placeholder="请选择病区" style="width:200px" clearable>
                    <el-option v-for="item in wardlist" :key="item.id" :label="item.departmentName" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-table v-loading="loading" :data="patientInfoList">
            <el-table-column label="序号" type="index" width="55" align="center" />
            <el-table-column label="患者姓名" align="center" prop="patientName" />
            <el-table-column label="患者电话" align="center" prop="patientPhone" width='120' />
            <el-table-column label="性别" align="center" prop="sex">
                <template slot-scope="scope">
                    {{ scope.row.sex == 'MALE' ? '男' : '' }}
                    {{ scope.row.sex == 'FEMALE' ? '女' : '' }}
                </template>
            </el-table-column>
            <el-table-column label="年龄" align="center" prop="age" />
            <el-table-column label="出生日期" align="center" prop="birthDate" width='130' />
            <el-table-column label="医院" align="center" prop="hospitalAgencyName" width='130' />
            <el-table-column label="院区" align="center" prop="campusAgencyName" width='130' />
            <el-table-column label="科室名称" align="center" prop="departmentName" width='130' />
            <el-table-column label="病区名称" align="center" prop="wardName" width='130' />
            <el-table-column label="住院/门诊号" align="center" prop="inHospitalNumber" width='130' />
            <el-table-column label="患者来源" align="center" prop="patientSource">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.patient_source" :value="scope.row.patientSource" />
                </template>
            </el-table-column>
            <el-table-column label="医生" align="center" prop="attendingPhysicianName" />
            <el-table-column label="诊断" align="center" prop="mainDiagnosis" />
            <el-table-column label="手术名称" align="center" prop="surgicalName" />
            <el-table-column label="出院/就诊时间" align="center" prop="dischargeTime" width='130'>
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.dischargeTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <!-- <el-table-column label="就诊方式" align="center" prop="visitMethod">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.visit_method" :value="scope.row.visitMethod" />
                </template>
            </el-table-column>
            <el-table-column label="住址" align="center" prop="address" /> -->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleAuthRole(scope.row)"
                        v-if="queryParams.routeCheckStatus == 'UNAUDITED'">审核</el-button>
                    <el-button size="mini" type="text" @click="handleLook(scope.row)"
                        v-if="queryParams.routeCheckStatus != 'UNAUDITED'">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>
</template>

<script>
import { tasklist } from "@/api/manage/manualReview";
import { getAge } from "@/utils/age";
import { selectAgencyList, getDepartmentList, getAgencyList } from "@/api/manage/selectAgencyList";
import { usergetList } from "@/api/unitconfig/patientConfiguration";
export default {
    name: "manualReview",
    dicts: ['patient_type', 'visit_method', 'sign_status', 'patient_source'],
    data() {
        return {
            attendingPhysicianlist: [],
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 患者信息表格数据
            patientInfoList: [],
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                patientName: null,
                patientPhone: null,
                birthDate: null,
                patientType: null,
                visitMethod: null,
                mainDiagnosis: null,
                attendingPhysicianId: null,
                hospitalAgencyId: null,
                hospitalAgencyName: null,
                campusAgencyId: null,
                campusAgencyName: null,
                departmentId: null,
                departmentName: null,
                wardId: null,
                wardName: null,
                surgicalName: null,
                admissionTimeStart: null,
                admissionTimeEnd: null,
                outpatientNumber: null,
                dischargeTimeStart: null,
                dischargeTimeEnd: null,
                dischargeMethod: null,
                patientSource: null,
                routeCheckStatus: 'UNAUDITED',
            },
            dischargeTime: [],
            admissionTime: [],
            //医院list
            hospitalAgencylist: [],
            // 院区list
            campusAgencylist: [],
            //科室list
            departmentlist: [],
            //病区list
            wardlist: [],
        };
    },
    created() {
        this.getList();
        this.selectAgencyinfo();
    },
    methods: {
        handleClick(tab, event) {
            this.dischargeTime = []
            this.queryParams.dischargeTimeStart = null
            this.queryParams.dischargeTimeEnd = null
            this.admissionTime = []
            this.queryParams.admissionTimeStart = null
            this.queryParams.admissionTimeEnd = null
            this.queryParams.pageNum = 1
            this.getList();
        },
        //主治医生
        usergetListinfo() {
            usergetList({
                hospitalAgencyId: this.queryParams.hospitalAgencyId,
                postName: 'DOCTOR'
            }).then(res => {
                this.attendingPhysicianlist = res.data
            })
        },
        //查看操作
        handleLook(row) {
            this.$router.push({
                path: "/task/LookAuditing",
                query: {
                    patientId: row.patientId,
                    routeCheckStatus: this.queryParams.routeCheckStatus,
                },
            });
        },
        /** 审核操作 */
        handleAuthRole(row) {
            this.$router.push({
                path: "/task/Auditing",
                query: {
                    patientId: row.patientId,
                },
            });
        },
        /** 查询患者信息列表 */
        getList() {
            this.loading = true;
            this.queryParams.taskCreateType = 'MANUAL_CREATE'
            tasklist(this.queryParams).then(response => {
                response.rows.forEach(e => {
                    e.birthDate ? e.age = getAge(e.birthDate) : ''
                })
                this.patientInfoList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        /** 搜索按钮操作 */
        handleQuery() {
            if (this.dischargeTime?.length > 0) {
                this.queryParams.dischargeTimeStart = this.dischargeTime[0]
                this.queryParams.dischargeTimeEnd = this.dischargeTime[1]
            } else {
                this.queryParams.dischargeTimeStart = null
                this.queryParams.dischargeTimeEnd = null
            }
            if (this.admissionTime?.length > 0) {
                this.queryParams.admissionTimeStart = this.admissionTime[0]
                this.queryParams.admissionTimeEnd = this.admissionTime[1]
            } else {
                this.queryParams.admissionTimeStart = null
                this.queryParams.admissionTimeEnd = null
            }
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.admissionTime = [];
            this.queryParams.admissionTimeStart = null
            this.queryParams.admissionTimeEnd = null
            this.dischargeTime = []
            this.queryParams.dischargeTimeStart = null
            this.queryParams.dischargeTimeEnd = null
            this.resetForm("queryForm");
            this.handleQuery();
        },
        selectAgencyinfo() {
            let query = {
                agencyStatus: 'ON',
                nodeType: 'HOSPITAL',
            }
            selectAgencyList(query).then(res => {
                this.hospitalAgencylist = res.data
            })
        },
        //获取下级单位
        getAgencyListinfo(nodeType, id) {
            let query = {
                nodeType: nodeType,
            }
            if (nodeType == 'HOSPITAL') {
                query.hospitalId = id
            } else if (nodeType == 'CAMPUS') {
                query.campusId = id
            } else if (nodeType == 'DEPARTMENT') {
                query.departmentId = id
            }
            getAgencyList(query).then(res => {
                if (nodeType == 'HOSPITAL') {
                    this.campusAgencylist = res.data.campusList
                    this.departmentlist = res.data.departmentList
                } else if (nodeType == 'CAMPUS') {
                    this.departmentlist = res.data.departmentList
                }
                this.wardlist = res.data.wardList
            })
        },
        //选中医院获取院区
        changehospitalAgency(id) {
            this.getAgencyListinfo('HOSPITAL', id)
            // let query = {
            //   agencyStatus: 'ON',
            //   nodeType: 'CAMPUS',
            //   parentId: id,
            // }
            // selectAgencyList(query).then(res => {
            //   this.campusAgencylist = res.data
            this.queryParams.campusAgencyId = null
            this.queryParams.departmentId = null
            this.queryParams.wardId = null
            this.queryParams.attendingPhysicianId = null
            // })
            //医生
            this.usergetListinfo();
        },
        //选中院区获取科室
        changecampusAgency(id) {
            this.getAgencyListinfo('CAMPUS', id)
            // let query = {
            //   nodeType: 'DEPARTMENT',
            //   hospitalAgencyId: id,
            // }
            // getDepartmentList(query).then(res => {
            //   this.departmentlist = res.data
            this.queryParams.departmentId = null
            this.queryParams.wardId = null
            // })
        },
        //选中科室获取病区
        changedepartment(id) {
            this.getAgencyListinfo('DEPARTMENT', id)
            // let query = {
            //   nodeType: 'WARD',
            //   parentDepartmentId: id,
            // }
            // getDepartmentList(query).then(res => {
            //   this.wardlist = res.data
            this.queryParams.wardId = null
            // })
        },
        //清空医院
        clearhospitalAgency() {
            this.queryParams.campusAgencyId = null
            this.queryParams.departmentId = null
            this.queryParams.wardId = null
            this.queryParams.attendingPhysicianId = null
        },
        //清空院区
        clearcampusAgency() {
            this.queryParams.departmentId = null
            this.queryParams.wardId = null
            this.getAgencyListinfo('HOSPITAL', this.queryParams.hospitalAgencyId)
        },
        //清空科室
        cleardepartment() {
            this.queryParams.wardId = null
            if (this.queryParams.departmentId) {
                this.getAgencyListinfo('DEPARTMENT', this.queryParams.departmentId)
            } else if (this.queryParams.campusAgencyId) {
                this.getAgencyListinfo('CAMPUS', this.queryParams.campusAgencyId)
            } else if (this.queryParams.hospitalAgencyId) {
                this.getAgencyListinfo('HOSPITAL', this.queryParams.hospitalAgencyId)
            }
        },
    }
};
</script>
