<template>
    <div class="accompanied-container">
        <div class="col">
            <div class="header">兼证</div>
            <div :class="['col-item', active[0] == index ? 'active' : '']" v-for="(item, index) in listData"
                :key="index" @click="handleList(index)">
                {{ item.name }}
            </div>
        </div>
        <div class="col">
            <div class="header">治法</div>
            <div :class="['col-item', active[1] == index ? 'active' : '']" v-for="(item, index) in therapyList"
                :key="index" @click="handleTherapy(index)">
                {{ item.name }}
            </div>
        </div>
        <div class="col">
            <div class="header">药品</div>
            <div :class="['col-item']" v-for="(item, index) in medList" :key="index">
                <el-checkbox :value="item.checked" @change="ypChange(index)">{{ item.ypmc }}</el-checkbox>
            </div>
        </div>
    </div>
</template>
<script>
    import {
        accompaniedData
    } from "@/utils/data.js";
    import {

        changeAndJoin,

    } from "@/api/knowledge.js";
    import {CC_Dosage2Basis} from '@/utils/medicine';
    accompaniedData.forEach(item => {
        item.child.forEach(item1 => {
            item1.child.forEach(item2 => {
                item2.checked = false
            })
        })
    });
    export default {
        data() {
            return {
                active: [0, 0, 0],
                listData: accompaniedData, // 兼证 列表
                therapyList: [], // 治法列表
                medList: [], // 药品列表
            };
        },
        watch: {

        },
        created() {
            this.$nextTick(() => {
                this.therapyList = accompaniedData[this.active[0]].child;
                this.medList = this.therapyList[this.active[1]].child;
            });
        },
        methods: {
            // 兼症点击
            handleList(index) {
                this.active = [index, 0, 0]
                this.therapyList = accompaniedData[this.active[0]].child;
                this.medList = this.therapyList[this.active[1]].child;
            },
            // 治法点击
            handleTherapy(index) {
                let active_0 = this.active[0]
                this.active = [active_0, index, 0]
                this.medList = this.therapyList[this.active[1]].child;
            },
            // 药品改变
            ypChange(index) {

                let active_0 = this.active[0]
                let active_1 = this.active[1]
                let item = this.medList[index]
                let parent = this.$parent.$parent

                let parentTable = parent.recipe_tabs[parent.recipe_tabs_c].totalTableD

                let isRepate = false
                // 判断是否重复添加药品
                parentTable.forEach(parentItem => {
                    if (parentItem.medid == item.pid) {
                        isRepate = true

                    }
                })

                if (isRepate) {
                    this.medList[index].checked = false
                    this.therapyList[active_1].child = this.medList
                    this.$message.warning("请勿重复添加药品");
                    return
                }

                this.medList[index].checked = !this.medList[index].checked
                this.therapyList[active_1].child = this.medList



                if (item.checked) { // 添加药品
                    let scope = parentTable[parentTable.length - 1]
                    let obj = {
                        row: {
                            id: scope.id + 1
                        }
                    }
                    // parent.clickPid = item.pid
                    // parent.addDis1(obj, 1)
                    parent.chooseDis1({
                        row: scope
                    }, 0, item, 'accompanied')
                    // this.changeAndJoin(item)
                } else {
                    let id = item.pid
                    let scope = null

                    parentTable.forEach(parentItem => {
                        if (id == parentItem.medid) {
                            scope = parentItem
                            parent.deleteDis1({
                                row: scope
                            })
                        }
                    })


                }

            },

            // 名家验案和 方剂转方/合方
            async changeAndJoin(item) {
                // type == 1 转方 type ==2 合方

                let parent = this.$parent.$parent

                let drugs = [item];

                // 新方式处理药房
                let pharmacyName = "";
                // let type1 = 0
                let type1 = parent.recipe_tabs[parent.recipe_tabs_c].radio;
                parent.pharmacyList.forEach(item => {
                    if (item.pid == parent.recipe_tabs[parent.recipe_tabs_c].pharmacyID) {
                        pharmacyName = item.pharmacyName;
                    }
                });


                let ids = [];
                drugs.forEach(item => {
                    item.oldDose = CC_Dosage2Basis(item);
                    let idDose = item.pid + "&" + (item.oldDose || 0);
                    ids.push(idDose);
                });


                const loading = this.$loading({
                    lock: true,
                    text: "正在处理药品数据,请稍等",
                    spinner: "el-icon-loading",
                    background: "rgba(0, 0, 0, 0.7)"
                });

                let res = await changeAndJoin({
                    pharmacyid: parent.recipe_tabs[parent.recipe_tabs_c].pharmacyID,
                    type: type1, // parent.recipe_tabs[parent.recipe_tabs_c].radio,
                    drugIds: ids
                }).catch(err => {
                    loading.close();
                });
                // 中药处方
                if (res.ResultCode == 0) {
                    let noDrugs = []; // 查找不到的药品
                    let hasDrugs = []; // 查找的到的药品
                    res.Data.forEach((item, index) => {
                        drugs.forEach((item1, index1) => {
                            if (item == item1.pid) {
                                // debugger
                                item1.color = "red";
                                let obj = {
                                    drugList: [],
                                    page: 1,
                                    loadMore: true,
                                    usageList: [],
                                    seqn: item1.seqn || '',
                                    ypmc: item1.matname || item1.ypmc,
                                    gg: "",
                                    dw: item1.dw,
                                    lsjg: item1.price ? item1.price : 0,
                                    price: item1.price ? item1.price : 0,
                                    kc: 0,
                                    cdmc: "无",
                                    pid: item1.pid,
                                    color: "red",
                                    dose: item1.dose || 0,
                                    useage: item1.useage || '',
                                    showSearch: false,
                                    matid: item1.pid,

                                    key: "",
                                    search_i: index
                                };
                                obj.oldDose = item1.oldDose;
                                noDrugs.push(obj);
                                // noDrugs.push(item1.matname)
                            }

                            if (item instanceof Object && item.oldYpid == item1.matid) {
                                item.ggnum = +item.ggnum || 1;
                                item.xbzxs = +item.xbzxs || '34'.includes(item.zylx) ? item.ggnum : 1;
                                hasDrugs.push(item);
                            }
                        });
                    });
                    hasDrugs = hasDrugs.concat(noDrugs);
                    if (hasDrugs.length > 0) {
                        parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].totalTableD = parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].totalTableD.filter(item => {
                            return item.name;
                        });
                        // console.log(hasDrugs, "has");
                        // 赋值给表格
                        hasDrugs.forEach((item, index) => {
                            let obj1 = {
                                color: item.color ? item.color : "#000",
                                drugList: [],
                                page: 1,
                                loadMore: true,
                                usageList: [],
                                id: item.seqn,
                                name: item.ypmc,
                                spec: item.gg, // 规格
                                dose: item.dose, // 剂量
                                unit: item.dw, // 单位
                                // usage: item.usagestr, // 用法
                                usage: item.useage,
                                // price: item.lsjg,
                                price: item.price ? item.price : 0,
                                total: item.price * item.dose,
                                inventory: item.kc, // 库存
                                key: "",
                                search_i: index,
                                medid: item.pid, // 药品id
                                originname: item.cdmc, // 产地名称
                                showSearch: false
                            };
                            obj1.ggnum = +item.ggnum || 1;
                            obj1.xbzxs = +item.xbzxs || '34'.includes(item.zylx) ? obj1.ggnum : 1;
                            parent.recipe_tabs[parent.recipe_tabs_c].totalTableD.push(obj1);
                            parent.getSelectType("中药药品用法", index);
                        });
                        parent.recipe_tabs[parent.recipe_tabs_c].totalTableD.push({
                            drugList: [],
                            page: 1,
                            loadMore: true,
                            usageList: [],
                            showSearch: false,
                            search_i: 0,
                            id: 1,
                            key: ""
                        });
                        parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].totalTableD = this.removeRepeat(
                            parent.recipe_tabs[parent.recipe_tabs_c].totalTableD,
                            "medid"
                        );
                        parent.recipe_tabs[parent.recipe_tabs_c].totalTableD.filter(
                            (item3, index3) => {
                                return (item3.id = index3 + 1);
                            }
                        );

                        parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].tableData = parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].totalTableD.filter(item2 => {
                            if (item2.medid) {
                                // if (item2.color != '#000') {
                                // this.getRationalMedForPlat(item2.medid)
                                // } else {
                                // this.getRationalMed(item2.medid)
                                // }
                                if (item2.medid.indexOf("-") != -1) {
                                    this.$parent.$parent.$parent.getRationalMed(item2.medid);
                                } else {
                                    this.$parent.$parent.$parent.getRationalMedForPlat(item2.medid);
                                }
                            }

                            return Number(item2.id) % 2 != 0;
                        });
                        parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].tableData1 = parent.recipe_tabs[
                            parent.recipe_tabs_c
                        ].totalTableD.filter(item2 => {
                            return Number(item2.id) % 2 == 0;
                        });
                    }

                    this.$forceUpdate()

                    loading.close();
                }

            },
            // 去重
            removeRepeat(arr, key) {
                let obj = {};
                arr = arr.reduce((pre, next) => {
                    obj[next[key]] ? "" : (obj[next[key]] = true && pre.push(next));
                    return pre;
                }, []);
                return arr;
            },
        }
    };
</script>
<style scoped lang="scss">
    .accompanied-container {
        width: 100%;
        display: flex;
        align-items: flex-start;
        justify-content: flex-start;
        max-height: 200px;
        overflow: auto;
    }

    .col {
        flex: 1;
        border: 1px solid #dcdfe6;
        border-bottom: 0;

        .header {
            background: #f3fffb;
            height: 20px;
            font-size: 12px;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            border-bottom: 1px solid #dcdfe6;
        }

        .col-item {
            height: 20px;
            font-size: 12px;
            text-align: center;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 0 4px;
            border-bottom: 1px solid #dcdfe6;
            cursor: pointer;
        }

        .active {
            background: #e6fff6;
        }
    }

    .col:nth-child(2),
    .col:nth-child(3) {
        // border-left: 0;

        .header {}
    }

    .col:nth-child(3) {
        .col-item {
            justify-content: flex-start;
        }
    }

    ::v-deep .el-checkbox__label {
        font-size: 12px;
        line-height: 20px;
        padding-left: 5px;
    }
</style>