<template>
	<el-container class="is-vertical">
        <sc-page-header @add="table_add"></sc-page-header>
        
        <scTable ref="xGridTable" :apiObj="$API.outsourcing.order" :formConfig="formConfig" :paramsColums="paramsColums" :columns="columns">
            <template #code_link="{ row }">
                <vxe-text status="primary" @click="table_detail(row)">{{ row.code }}</vxe-text>
            </template>

            <template #action="{ row }">
                <el-button v-if="row.status == 'processing'" type="primary" link @click="table_receipt(row)">
                    <template #icon><sc-iconify icon="tabler:package-import"></sc-iconify></template>收货
                </el-button>
                <template v-if="row.status == 'pending'">
                    <el-button type="primary" link @click="table_edit(row)">
                        <template #icon><sc-iconify icon="ant-design:edit-outlined"></sc-iconify></template>修改
                    </el-button>
                    <el-button type="primary" link @click="table_del(row)">
                        <template #icon><sc-iconify icon="ant-design:delete-outlined"></sc-iconify></template>删除
                    </el-button>
                </template>
            </template>
        </scTable>
	</el-container>

    <order-detail v-if="dialog.detail" ref="orderRef" @success="refreshTable" @closed="dialogClose"></order-detail>
    <order-desc v-if="dialog.desc" ref="orderDescRef" @closed="dialog.desc = false"></order-desc>
</template>

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

import API from "@/api";
import TOOL from "@/utils/tool";
import { outsourcingDic } from "@/utils/basicDic";
import { mapFormItemInput, mapFormItemSelect, mapFormItemDatePicker, mapFormItemTenant } from "@/components/scTable/helper";
import orderDetail from "./detail";
import orderDesc from "./desc";

import store from "@/store";
watch(() => store.state.tenant.tenantId, () => refreshTable());

const daterangeConfig = reactive({
    resetValue: () => [],
    props: {
        type: "daterange",
        startPlaceholder: "开始日期",
        endPlaceholder: "结束日期",
        format: "YYYY-MM-DD"
    }
});

const formConfig = reactive({
    data: {},
    items: [
        mapFormItemTenant({ events: { change: data => XEUtils.merge(formConfig.data, data) } }),
        mapFormItemInput("nameLike", "计划主题"),
        mapFormItemInput("codeLike", "计划编号"),
        mapFormItemDatePicker("createTime", "创建日期", daterangeConfig)
    ]
});

const paramsColums = reactive([
    { column: "orderBy", defaultValue: "createTime_desc" },
    { column: "tenantId" },
    { column: "nameLike" },
    { column: "codeLike" },
    { column: "createTimeBegin", field: "createTime[0]" },
    { column: "createTimeEnd", field: "createTime[1]" }
]);

const columns = reactive([
    { type: "seq", fixed: "left", width: 60 },
    { visible: computed(() => store.state.tenant.tenantId === "0"), type: "html", field: "tenantName", title: "所属租户", fixed: "left", minWidth: 200, sortable: true, formatter: ({ cellValue, row }) => cellValue || XEUtils.get(XEUtils.find(store.state.tenant.tenants, item => item.id == row.tenantId), "name") },
    { type: "html", field: "name", title: "委外主题", fixed: "left", minWidth: 150, sortable: true },
    { field: "code", title: "委外编号", fixed: "left", minWidth: 150, sortable: true, className: "vxe-table-link-cell", slots: { default: "code_link" } },
    { field: "status", title: "委外状态", minWidth: 120, editRender: { name: "$cell-tag", options: outsourcingDic.orderStatus } },
    { type: "html", field: "supplierName", title: "加工厂商", fixed: "left", minWidth: 150, sortable: true },
    { type: "html", field: "orderDate", title: "委外日期", minWidth: 120, sortable: true },
    { visible: false, type: "html", field: "createTime", title: "创建日期", minWidth: 120, sortable: true, formatter: ({ cellValue }) => TOOL.dateFormat(cellValue, "YYYY-MM-DD") || cellValue },
    { title: "操作", fixed: "right", width: 140, slots: { default: "action" } }
]);

// 显示隐藏 筛选表单
const xGridTable = ref();
const refreshTable = (mode = "add") => (xGridTable.value.searchData(mode), xGridTable.value.reloadColumn(columns));

const orderRef = ref();
const orderDescRef = ref();
const dialog = reactive({
    detail: false,
    desc: false,
    receipt: false
});

const table_add = () => {
    dialog.detail = true;
    nextTick(() => orderRef.value?.setData());
}

const table_edit = row => {
    dialog.detail = true;
    nextTick(() => orderRef.value?.setData(row, "edit"));
}

const table_detail = row => {
    dialog.desc = true;
    nextTick(() => orderDescRef.value?.setData(row));
}

const table_receipt = row => {
    // 收货日期、是否需要质检
}

const table_del = ({ id }) => {
    ElMessageBox.confirm("是否确认删除该委外订单?", "删除警告", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消"
    }).then(() => {
        API.outsourcing.order.del({ id }).then(() => {
            ElMessage.success("操作成功");
            refreshTable();
        });
    }).catch(() => {});
}

const dialogClose = isDel => {
    dialog.detail = false;
    isDel && refreshTable();
}
</script>