<template>
    <!-- 专家处方 -->
    <!-- <div class="work-item" :class="{ dark: prescriptioData?.orderStatus === 0 && ROLETYPE != '3' }"> -->
    <div class="work-item">
        <div class="work-info">
            <!-- <img
                v-if="prescriptioData?.orderStatus === 0 && ROLETYPE == '3'"
                class="todo-img"
                src="@/assets/img/gallery/todo.png"
            /> -->
            <div class="prescription-box">
                <div class="step-box">
                    <step-box
                        :orderStatus="infoItem.orderStatus"
                        v-if="infoItem.orderStatus || infoItem.orderStatus === 0"
                    ></step-box>
                </div>
                <div class="work-info-conetnt">
                    <div class="info-item">
                        <div class="info-name">农事编号：</div>
                        <div class="info-value">{{ infoItem.orderId }}</div>
                    </div>
                    <div class="info-item">
                        <div class="info-name">服务亩数：</div>
                        <div class="info-value">{{ infoItem.area }}亩</div>
                    </div>
                    <div class="info-item">
                        <div class="info-name">服务区域：</div>
                        <div class="info-value">{{ infoItem.serviceRegion }}</div>
                    </div>
                    <div v-if="prescriptioData?.orderStatus === 0 && ROLETYPE == '3'">
                        <div class="info-item">
                            <div class="info-name">触发条件：</div>
                            <div class="info-value">
                                {{ infoItem.condition }}
                                <div class="select-value">
                                    <el-select v-model="infoItem.condition" placeholder="请选择" style="width: 80px">
                                        <el-option v-for="num in 9" :key="num" :value="20 + (num - 1) * 5 + '%'">
                                            {{ 20 + (num - 1) * 5 }}%
                                        </el-option>
                                    </el-select>
                                </div>
                            </div>
                        </div>
                        <div class="info-item time-picker">
                            <div class="info-name">执行时间：</div>
                            <div class="info-value">
                                <el-date-picker
                                    style="width: 150px"
                                    value-format="YYYY-MM-DD"
                                    v-model="infoItem.executeDate"
                                    type="date"
                                    :clearable="false"
                                    class="date-info"
                                    placeholder="选择日期"
                                />
                            </div>
                        </div>
                    </div>
                    <div v-else>
                        <div class="info-item">
                            <div class="info-name">触发条件：</div>

                            <div class="info-value">{{ infoItem.condition }}</div>
                        </div>
                        <div class="info-item">
                            <div class="info-name">执行时间：</div>
                            <div class="info-value">{{ infoItem.executeDate }}</div>
                        </div>
                    </div>
                </div>
                <!-- <div class="desc">
                    {{ prescriptioData.expertPrescription || prescriptioData.weatherWarningMsg }}
                </div> -->
                <!-- 显示简略信息 -->
                <!-- v-if="prescriptioData?.orderStatus === 0 && ROLETYPE == '3'" -->
                <div class="prescription-item">
                    <div class="recipe-box">
                        <div class="recipe-title">
                            <div class="recipe-name">药物处方</div>
                        </div>
                        <div class="usage-mode">
                            施用方式：<span>{{ prescriptioData.usageMode }}</span>
                        </div>
                        <div class="new-wrap">
                            <div class="new-title">
                                <div class="title-1"><div class="table-name">使用功效</div></div>
                                <div class="title-2"><div class="table-name">肥药名称</div></div>
                                <div class="title-3"><div class="table-name">执行方式</div></div>
                                <div class="title-4"><div class="table-name">药肥配比</div></div>
                                <div class="title-5"><div class="table-name">单亩用量</div></div>
                            </div>
                            <div
                                class="new-table-wrap"
                                v-for="(prescriptionItem, prescriptionI) in prescriptioData.prescriptionList"
                                :key="prescriptionI"
                            >
                                <div
                                    class="new-prescription"
                                    v-for="(subP, subI) in prescriptionItem.pesticideFertilizerList"
                                    :key="subI"
                                >
                                    <div class="new-table">
                                        <div class="line-l">
                                            <div class="line-1 title-1">{{ subP.typeName }}</div>
                                            <div class="line-2">{{ subP.defaultName }}</div>
                                        </div>
                                        <div class="line-r" :class="{'has-border': prescriptioData.usageMode === '叶面施'}">
                                            <div class="line-3">
                                                <div class="sub-line title-3" :class="{'execute-line': prescriptioData.usageMode === '叶面施'}">人工</div>
                                                <div class="sub-line title-4">1:{{ subP.ratio }}{{ subP.unit }}</div>
                                                <div class="sub-line title-5">{{ subP.muUsage }}{{ subP.unit }}</div>
                                            </div>
                                            <div class="line-4" v-if="prescriptioData.usageMode === '叶面施'">
                                                <div class="sub-line title-3 execute-line">无人机</div>
                                                <div class="sub-line title-4">{{ subP.ratio2 ? subP.ratio2 : "---" }}</div>
                                                <div class="sub-line title-5">{{ subP.muUsage2 ? subP.muUsage2 : "---" }}</div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="note-text" v-if="subP.remark">{{ subP.remark }}</div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="prescription-item" v-if="prescriptioData?.orderStatus === 0">
                    <div class="sub-title">
                        <div class="sub-line"></div>
                        <div class="sub-name">指标动态</div>
                        <div class="sub-line"></div>
                    </div>
                    <div class="prescription-chart">
                        <!-- <chart :key="1" type="feature" :indexName="infoItem.indexName" :chartJson="prescriptioData.indexJson"></chart> -->
                    </div>
                </div>
                <div class="prescription-item" v-if="prescriptioData?.orderStatus === 0 && ROLETYPE == '3'">
                    <el-button class="btn" type="danger" plain @click="remove()">删除</el-button>
                    <el-button class="btn" type="primary" @click="submitForm(formRef)">确认下发</el-button>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ElMessage, ElMessageBox } from "element-plus";
import eventBus from "@/api/eventBus";
import chart from "./chart.vue";
import { onActivated, onMounted, reactive, ref } from "vue";
import { useStore } from "vuex";
import stepBox from "@/components/common/stepBox.vue";
import customTable from "./table.vue";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();

const props = defineProps({
    prescriptioData: {
        type: Object,
    },
});

const tableHeader = [
    {
        name: "使用功效",
        props: "typeName",
    },
    {
        name: "肥药名称",
        props: "defaultName",
    },
    {
        name: "执行方式",
        props: "usageMode1",
    },
    {
        name: "药肥配比",
        props: "usageMode",
    },
    {
        name: "单亩用量",
        props: "muUsage",
    },
];

const tableData = ref([
    {
        brand: "",
        defaultDroneRatio: null,
        defaultName: "灌水",
        defaultRatio: null,
        executeStyle: null,
        id: "686142764512972800",
        muPrice: null,
        muUsage: 222,
        muUsage2: null,
        orderId: "686142764492001280",
        pesticideFertilizerCode: "",
        pesticideFertilizerId: "15",
        pesticideFertilizerName: "灌水",
        price: null,
        ratio: 11,
        ratio2: null,
        remark: "",
        typeName: "调节",
        unit: "kg",
        usageMode: "",
        list: [],
    },
]);

const infoItem = ref({});

const store = useStore();
const ROLETYPE = '3';
// const ROLETYPE = store.state.app.curRole;

// 农事类型--区分表格显示字段等
const workType = ref(false);

let pesticideFertilizersOptions = ref([
    {
        id: "null",
        name: "芸苔素内酯 15000倍",
        typeName: "30",
        defaultRatio: null,
        defaultDroneRatio: null,
        unit: 0,
        defaultName: "调节",
    },
]);
VE_API.order.pesticideFertilizers().then(({ data }) => {
    pesticideFertilizersOptions.value = data;
    tableData.value[0].list = data;
});

onMounted(() => {
    dynamicValidateForm.usageMode = props.prescriptioData.usageMode;
    dynamicValidateForm.prescriptionList = props.prescriptioData.prescriptionList.map((item) => ({ ...item }));
    // submit()    // submit()
    console.log("dynamicValidateForm.domains",props.prescriptioData);
    infoItem.value = props.prescriptioData;
    if(infoItem.value.area)
      infoItem.value.area = infoItem.value.area.toFixed(2);
    for(let item of  infoItem.value.conditionList){
      infoItem.value.condition=item.name + "大于" + (parseFloat(item.value) * 100) + "%";
    }
});

// 表单
const formRef = ref();
const dynamicValidateForm = reactive({
    name: "",
    conditionRate: "",
    executeDate: "",
    usageMode: "",
    prescriptionList: [
        {
            name: "",
            pesticideFertilizerList: [
                {
                    key: 1,
                    typeName: "",
                    muUsage: "",
                    muUsage2: "",
                    ratio: "",
                    ratio2: "",
                    remark: "",
                },
            ],
        },
    ],
});

const addDomain = (parentIndex) => {
    dynamicValidateForm.prescriptionList[parentIndex].pesticideFertilizerList.unshift({
        key: Date.now(),
        muUsage: "",
        muUsage2: "",
        ratio: "",
        ratio2: "",
        remark: "",
    });
};

const removeDomain = (parentIndex, item) => {
    const index = dynamicValidateForm.prescriptionList[parentIndex].pesticideFertilizerList.indexOf(item);
    if (index !== -1) {
        dynamicValidateForm.prescriptionList[parentIndex].pesticideFertilizerList.splice(index, 1);
    }
};

const resetItemForm = (parentIndex, index) => {
    dynamicValidateForm.prescriptionList[parentIndex].pesticideFertilizerList[index] = {
        typeName: "",
        muUsage: "",
        muUsage2: "",
        ratio: "",
        ratio2: "",
        remark: "",
    };
};
/**
 * 选择药肥的时候修改订单中药肥pesticideFertilizerId 以外其他数据
 * @param index
 */
const handlePesticideFertilizerChange = (index) => {
    let obj = pesticideFertilizersOptions.value.filter(
        (item) => dynamicValidateForm.domains[index].pesticideFertilizerId === item.id
    )[0];
    dynamicValidateForm.domains[index] = {
        ...dynamicValidateForm.domains[index],
        typeName: obj.typeName,
        unit: obj.unit,
        defaultRatio: obj.defaultRatio,
        usageModeList: obj.usageModeList,
        ratio: obj.defaultRatio,
        defaultName: obj.defaultName,
        pesticideFertilizerName: obj.name,
    };
    console.log(dynamicValidateForm.domains);
};

const submitForm = (formEl) => {
    if (!formEl) return;
    formEl.validate((valid) => {
        if (valid) {
            submit();
        } else {
            console.log("error submit!");
        }
    });
};

const submit = () => {
    const data = {
        orderId: props.prescriptioData.orderId,
        orderStatus: 1,
        pesticideFertilizers: dynamicValidateForm.domains,
    };
    VE_API.order.confirm(data).then(({ code }) => {
        if (code == 0) {
            console.log("专家下发处方成功");
            eventBus.emit("discover:submitForm");
            window.location.reload();
        }
    });
};

const formatIndex = (index) => {
    return String(index + 1).padStart(2, "0");
};

const handleTableData = (item) => {
    return [
        {
            typeName: item.typeName,
            defaultName: item.defaultName,
            executeStyle: 1,
            ratio: item.ratio || item.defaultRatio,
            usageMode: item.usageMode,
            muUsage: item.muUsage,
            unit: item.unit,
        },
        {
            typeName: item.typeName,
            defaultName: item.defaultName,
            executeStyle: 2,
            ratio: item.ratio2,
            usageMode: "---",
            muUsage: item.muUsage2,
            unit: item.unit,
        },
    ];
};

const remove = () => {
    ElMessageBox.confirm("是否要删除这个农事?", "警告", {
        confirmButtonText: "删除",
        cancelButtonText: "取消",
        type: "warning",
    }).then(() => {
        VE_API.farm.deleteFarmWork({ libId: infoItem.value.farmWorkLibId }).then(({ code }) => {
            if (code === 0) {
                ElMessage({
                    type: "success",
                    message: "删除成功！",
                });
                setTimeout(() => {
                    // router.go(-1);
                    router.replace("/expert_album?reload=true");
                }, 500);
            }
        });
    });
};
</script>

<style lang="scss" scoped>
@import "./boxClass.scss";
.recipe-box {
    .recipe-title {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 10px;
        padding-left: 10px;
        position: relative;
        &::before {
            content: "";
            position: absolute;
            left: 0;
            top: 3px;
            width: 3px;
            height: 15px;
            background: #fff;
            border-radius: 11px;
        }
        .recipe-name {
            color: #fff;
            font-size: 15px;
        }
        .recipe-btn {
            color: #FFD489;
            display: flex;
            align-items: center;
            cursor: pointer;
            .icon{
                margin-right: 3px;
            }
        }
    }
    .usage-mode {
        display: flex;
        align-items: center;
        font-size: 16px;
        color: #727272;
        padding: 2px 0 12px 0;
        span {
            color: #fff;
        }
    }
}
.prescription-result {
    position: relative;
    &.has-wrap {
        border: 0.5px solid #444444;
        border-radius: 4px;
        padding: 8px 10px 10px 10px;
    }
    .result-index {
        position: absolute;
        left: -1px;
        top: -1px;
        padding: 0 8px;
        background: #2199f8;
        color: #fff;
        font-size: 12px;
        border-radius: 4px 0 4px 0;
    }
    .result-title {
        display: flex;
        justify-content: center;
        align-items: center;
        padding-bottom: 8px;
        .title-item {
            display: flex;
            align-items: center;
            .name {
                color: rgba(33, 153, 248, 0.85);
            }
            .value {
                color: rgba(0, 0, 0, 0.4);
            }
        }
        .title-item + .title-item {
            padding-left: 16px;
        }
    }
}
.new-wrap {
    border-radius: 5px;
    text-align: center;
    border: 1px solid #444444;
    .new-title {
        background: rgba(255, 255, 255, 0.04);
        border-radius: 5px 5px 0 0;
        display: flex;
        color: #727272;
        // justify-content: space-around;
        padding: 5px 6px;
        font-size: 14px;
        .table-name {
            width: 40px;
            font-size: 14px;
            margin: 0 auto;
        }
    }

    .title-1 {
        width: 62px;
    }
    .title-2 {
        flex: 1;
    }
    .title-3 {
        width: 64px;
    }
    .title-4 {
        width: 76px;
    }
    .title-5 {
        width: 68px;
    }
    .new-table-wrap {
        .new-prescription {
            padding: 0 8px;
            .new-table {
                display: flex;
                align-items: center;
                // border: 1px solid #444444;
                border-radius: 5px;
                color: #CECECE;
                font-size: 16px;
                .line-l {
                    display: flex;
                    flex: 1;
                    padding: 8px 0;
                    .line-2 {
                        flex: 1;
                        padding: 0 2px;
                    }
                }
                .line-r {
                    // padding: 8px 0;
                    &.has-border {
                        border-left: 1px solid #444444;
                    }
                    .line-3 {
                        display: flex;
                        align-items: center;
                    }
                    .sub-line {
                        padding: 10px 0;
                    }
                    .line-4 {
                        display: flex;
                        align-items: center;
                        border-top: 1px solid #444444;
                    }
                    .execute-line {
                        border-right: 1px solid #444444;
                    }
                }

            }
            .note-text {
                // margin: 8px 0 4px 0;
                color: #9F9F9F;
                background: rgba(255, 255, 255, 0.05);
                padding: 6px 8px;
                border-radius: 5px;
                text-align: left;
                font-size: 16px;
            }
        }
        .new-prescription + .new-prescription {
            // margin-top: 8px;
            border-top: 1px solid #444444;
        }
    }
}
</style>
