<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="患者姓名" prop="userName">
                <el-input v-model="queryParams.userName" placeholder="请输入患者姓名" clearable size="small"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="医生确认" prop="doctorConfirm">
                <el-select v-model="queryParams.doctorConfirm" placeholder="请选择" clearable size="small">
                    <el-option label="未确认" value="0" />
                    <el-option label="已确认" value="1" />
                </el-select>
            </el-form-item>
            <el-form-item label="患者确认" prop="userConfirm">
                <el-select v-model="queryParams.userConfirm" placeholder="请选择" clearable size="small">
                    <el-option label="未确认" value="0" />
                    <el-option label="已确认" value="1" />
                </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" border :data="list" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="患者姓名" align="center" prop="userName" />
            <el-table-column label="患者补充说明" :show-overflow-tooltip="true" align="center" prop="userAdvice"></el-table-column>
            <el-table-column label="患者确认状态" align="center" prop="userConfirm">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.userConfirm == 0">未确认</el-tag>
                    <el-tag type="success" v-if="scope.row.userConfirm == 1">已确认</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="医生确认状态" align="center" prop="doctorConfirm">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.doctorConfirm == 0">未确认</el-tag>
                    <el-tag type="success" v-if="scope.row.doctorConfirm == 1">已确认</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleDetial(scope.row)">详情</el-button>
                    <el-button size="mini" type="text" @click="handleAdvice(scope.row)">建议</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" />

        <el-dialog title="采集信息详情" :visible.sync="collectionOpen" width="1000px" append-to-body>
            <el-form ref="collectionForm" :model="collectionForm" label-width="110px">
                <div v-for="(answer, index) in collectionForm.answers">
                    <div style="margin-bottom: 20px;margin-top: 20px;">
                        <span style="font-size: 15px;font-weight: bold;    margin-left: 31px">{{ answer.title }}</span>
                    </div>
                    <div style="margin-left: 31px;">
                        <el-checkbox-group  v-model="collectionForm.answers[index].value" size="mini" >
                        <el-checkbox v-for="dict in answer.options" :label="dict.value" >{{ dict.name }}</el-checkbox>
                    </el-checkbox-group>
                    </div>
                </div>
            </el-form>
            <el-form-item label="过敏情况" prop="allergy">
                <el-input type="textarea" :rows="2" placeholder="请输入过敏情况" v-model="form.allergy">
                </el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input type="textarea" :rows="2" placeholder="请输入备注" v-model="form.remark">
                </el-input>
            </el-form-item>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancel">关 闭</el-button>
            </div>
        </el-dialog>

        <el-dialog title="医生建议"  :visible.sync="open" width="1000px" append-to-body>
            <el-form ref="form" :rules="rules" :model="form" label-width="110px">
                <el-form-item label="建议" prop="doctorAdvice">
                    <el-input :autosize="{ minRows: 15, maxRows: 100}" v-model="form.doctorAdvice" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" :disabled="isLoading"  @click="submitForm">确 认</el-button>
                <el-button @click="cancelAdvice">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { list, detail, doctorConfirm } from "@/api/collection.js";
export default {
    name: "collection",
    data() {
        return {
            queryParams: {
                pageNum: 1,
                pageSize: 10,
            },
            isLoading: false,
            // 遮罩层
            loading: true,
            // 导出遮罩层
            exportLoading: false,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            list: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                type: "0",
            },
            // 表单参数
            form: {
                id:undefined,
                doctorAdvice:undefined
            },
            // 表单校验
            rules: {
                doctorAdvice:[{ required: true, message: '请输入医生建议', trigger: 'blur' },]

            },
            collectionForm: {
                answers:[]
            },
            collectionOpen : false,
        }
    },
    created() {
        this.getList();
    },

    methods: {
        // submitForm() {
        //     this.$refs["form"].validate(valid => {
        //         if (valid) {
        //             // fill(this.form).then(res => {
        //             //     this.msgSuccess("填写成功");
        //             //     this.open = false;
        //             //     this.getList();
        //             // })
        //         }
        //     });
        // },
        handleAdvice(row){
            this.form = {
                id:undefined,
                doctorAdvice:undefined
            },
            this.open = true;
            this.form.id = row.id;
            if(row.doctorAdvice == null) {
                this.form.doctorAdvice = ''
            } else {
                this.form.doctorAdvice = row.doctorAdvice;
            }
            
        },
        cancelAdvice(){
            this.open = false;
            this.reset();
        },
        async submitForm(){
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.isLoading) return;
                    this.isLoading = true;
                    try {
                        // 执行异步操作，比如API请求
                        doctorConfirm(this.form).then(res => {
                        this.msgSuccess("填写成功");
                        this.open = false;
                        this.getList();
                        this.isLoading = false;
                    })
                    } catch (error) {
                        // 处理错误
                        console.error(error);
                    } finally {
                        // 无论成功或失败，都恢复按钮状态
                        this.isLoading = false;
                    }
                    
                }
            });
        },
        handleDetial(row) {
            detail(row.id).then(res => {
                this.collectionForm = res.data;

            })
            this.collectionOpen = true;
        },
        // handleFill(row) {
        //     detail(row.id).then(res => {
        //         this.form = res.data
        //     })
        //     this.open = true;
        // },
        getList() {
            this.loading = true;
            list(this.queryParams).then(response => {
                this.list = response.data.list;
                this.total = response.data.total;
                this.loading = false;
            });
        },

        // 取消按钮
        cancel() {
            this.collectionOpen = false;
            this.restCollection();
        },
        restCollection(){
            this.collectionForm = {
                answers:[]
            };
            this.resetForm("collectionForm");
        },
        // 表单重置
        reset() {
            this.form = {
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.patientId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        }
    }

};
</script>

<style scoped>
/* :deep(.el-checkbox__input.is-disabled + .el-checkbox__label) {
  color: #1890ff !important;
}

.el-checkbox__input.is-disabled.is-checked + .el-checkbox__label {
  color: #1890ff !important;
}

:deep(.el-checkbox__input.is-disabled.is-checked .el-checkbox__inner) {
  background-color: #1890ff !important;
  border-color: #1890ff !important;
} */
/* ::v-deep .el-checkbox__input.is-disabled + span.el-checkbox__label {
    color: #1890ff;
} */
/* ::v-deep .el-checkbox-button.is-disabled.is-checked .el-checkbox-button__inner {
    color: #1890ff;
}
::v-deep .el-checkbox__input.is-disabled.is-checked .el-checkbox__inner::after {
    border-color: #1890ff;
} */
</style>