<template>
    <el-dialog v-model="visible" :title="titleMap[mode]" fullscreen :close-on-click-modal="false" @closed="$emit('closed')">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120">
            <el-collapse v-model="activeNames">
                <el-collapse-item title="基本信息" name="basic">
                    <el-row>
                        <el-col v-if="$store.state.tenant.tenantId === '0'" :md="8" :xs="24">
                            <el-form-item label="所属租户" prop="tenantId">
                                <el-select v-if="!form.id" v-model="form.tenantId" filterable placeholder="请选择所属租户">
                                    <el-option v-for="item in $store.state.tenant.tenants" :key="item.id" :label="item.name" :value="item.id"></el-option>
                                </el-select>
                                <el-input v-else v-model="tenantName" readonly></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :md="8" :xs="24">
                            <el-form-item label="计划主题" prop="name">
                                <el-input v-model="form.name" placeholder="请输入计划主题"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :md="8" :xs="24">
                            <el-form-item label="计划编号" required>
                                <el-input v-model="form.code" :readonly="!!form.id" maxlength="50" show-word-limit clearable placeholder="不填将自动生成"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :md="8" :xs="24">
                            <el-form-item label="计划开工日期" prop="beginDate">
                                <vxe-date-picker v-model="form.beginDate" :end-date="form.endDate" value-format="yyyy-MM-dd" transfer placeholder="请选择计划开工日期"></vxe-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :md="8" :xs="24">
                            <el-form-item label="计划完工日期" prop="endDate">
                                <vxe-date-picker v-model="form.endDate" :start-date="form.beginDate" value-format="yyyy-MM-dd" transfer placeholder="请选择计划完工日期"></vxe-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :md="8" :xs="24">
                            <el-form-item label="计划优先级">
                                <el-select v-model="form.priority" placeholder="请选择计划优先级">
                                    <el-option v-for="(label, key) in productionDic.priority" :key="key" :label="label" :value="key"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>

                <el-collapse-item v-if="form.type == 'process'" title="委外工序" name="process">
                    <sc-form-table ref="formTableRef" v-model="form.childrenList[form.type]" :disabled="!!form.saleOrderId" v-bind="tableOptions[form.type]"></sc-form-table>
                </el-collapse-item>

                <el-collapse-item v-if="form.type == 'whole'" title="产品信息" name="material">
                    <sc-form-table ref="formTableRef" v-model="form.childrenList[form.type]" :disabled="!!form.saleOrderId" v-bind="tableOptions[form.type]"></sc-form-table>
                </el-collapse-item>

                <el-collapse-item title="其他说明" name="other">
                    <el-row>
                        <el-col :xs="24">
                            <el-form-item label="概要" label-width="100">
                                <el-input v-model="form.remark" type="textarea" maxlength="200" :rows="4" placeholder="请输入内容"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-collapse-item>
            </el-collapse>
        </el-form>

        <template #footer>
            <el-button auto-insert-space @click="visible = false">取消</el-button>
            <el-button :loading="isSaving" type="primary" auto-insert-space @click="submit">保存</el-button>
        </template>
    </el-dialog>
</template>

<script setup>
import moment from "moment";
import XEUtils from "xe-utils";

import API from "@/api";
import store from "@/store";
import { productionDic, outsourcingDic } from "@/utils/basicDic";
import { tableOptions } from "./main";

const $emit = defineEmits(["success", "closed"]);
const visible = ref(false);
const isSaving = ref(false);

const activeNames = ref(["basic", "material", "process", "other"]);
const mode = ref("add");
const titleMap = reactive({
    add: "新增委外计划",
    edit: "修改委外计划"
});

provide("tenantId", computed(() => form.value.tenantId));
const tenantName = computed(() => XEUtils.get(XEUtils.find(store.state.tenant.tenants, item => item.id == form.value.tenantId), "name"));
const form = ref({
    id: null,
    tenantId: store.state.tenant.tenantId,
    saleOrderId: null,
    name: null,
    code: null,
    type: "whole",
    beginDate: null,
    endDate: null,
    priority: "medium",
    childrenList: {
        process: [],
        whole: []
    },
    remark: null
});
const rules = reactive({
    tenantId: [{ required: true, message: "请选择所属租户" }],
    name: [{ required: true, message: "请输入计划主题" }],
    beginDate: [{ required: true, message: "请选择计划开工日期" }],
    endDate: [{ required: true, message: "请选择计划完工日期" }]
});

const open = () => visible.value = true;
const setData = data => {//工序委外新增
    open();
    mode.value = "edit";
    XEUtils.objectEach(form.value, (_, key) => {
        if (key == "priority") XEUtils.set(form.value, key, XEUtils.get(data, key) || "medium");
        else if (key == "childrenList") {
            data.type == "whole" && XEUtils.set(form.value.childrenList, data.type, XEUtils.map(XEUtils.get(data, key), item => ({ ...item.material, number: item.number })));
            if (data.type == "process") {}
        } else XEUtils.set(form.value, key, XEUtils.get(data, key))
    });
}

const formRef = ref();
const formTableRef = ref();
const submit = () => {
    formRef.value.validate(async valid => {
        if (valid) {
            if (!form.value.childrenList[form.value.type].length) return ElMessage.warning("请添加产品信息后再保存");
            
            if (await formTableRef.value.validateFormTable()) {
                const data = XEUtils.omit(form.value, "childrenList");
                const childrenList = XEUtils.map(form.value.childrenList[form.value.type], item => ({ materialCode: item.code, number: item.number }));
                XEUtils.set(data, "childrenList", childrenList);
                
                isSaving.value = true;
                API.outsourcing.plan[mode.value](data).then(res => {
                    ElMessage.success("操作成功");
                    isSaving.value = false;
                    visible.value = false;
                    $emit("success", mode.value);
                }).catch(() => isSaving.value = false);
            }
        } else {
            return false;
        }
    });
}

defineExpose({
    open,
    setData
});
</script>

<style scoped>
.el-form {padding-left: 16px;padding-right: 22px;}
.el-form .vxe-date-picker {flex-direction: row-reverse;width: 100%;}
.el-form .vxe-date-picker :deep(.vxe-date-picker--suffix) {border-radius: var(--vxe-ui-base-border-radius) 0 0 var(--vxe-ui-base-border-radius);}
.el-form .vxe-date-picker :deep(.vxe-date-picker--control-icon) {padding-left: .5em;padding-right: 0;color: var(--el-input-placeholder-color,var(--el-text-color-placeholder));}
.el-form .vxe-date-picker :deep(.vxe-date-picker--inner::placeholder) {color: var(--el-input-placeholder-color,var(--el-text-color-placeholder));}

.el-collapse {border: none;}
.el-collapse-item {margin-top: 15px;padding: 0 24px;background-color: var(--el-fill-color-blank);border: 1px solid var(--el-border-color-light);border-radius: 4px;color: var(--el-text-color-primary);box-shadow: var(--el-box-shadow-light);transition: var(--el-transition-duration);}
.el-collapse-item :deep(.el-collapse-item__header) {border-bottom-color: transparent;line-height: 55px;font-size: 16px;font-weight: bold;}
.el-collapse-item :deep(.el-collapse-item__header.is-active) {border-bottom: 1px solid var(--el-border-color-lighter);}
.el-collapse-item :deep(.el-collapse-item__wrap) {border: none;}
.el-collapse-item :deep(.el-collapse-item__content) {padding: 20px 28px 20px 0;}
.el-collapse-item:nth-child(2) :deep(.el-collapse-item__content) {padding-right: 0;}
</style>