<template>
	<el-container class="is-vertical">
        <sc-page-header @add="table_add"></sc-page-header>

        <scTable ref="xGridTable" v-bind="xGridOptions">
            <template #code_link="{ row }">
                <vxe-text status="primary" @click="table_detail(row)">{{ row.code }}</vxe-text>
            </template>

            <template #action>
                <!-- 入库申请/返工（派工后） -->
                <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_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>
        </scTable>
	</el-container>
</template>

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

import API from "@/api";
import TOOL from "@/utils/tool";
import { salesDic } from "@/utils/basicDic";
import { mapFormItemInput, mapFormItemSelect, mapFormItemDatePicker, mapFormItemTenant } from "@/components/scTable/helper";

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

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

const xGridOptions = reactive({
    // apiObj: API.production.prePlan,
    toolbarConfig: { export: false },
    formConfig: {
        data: {},
        items: [
            mapFormItemInput("nameLike", "质检任务主题"),
            mapFormItemInput("codeLike", "质检任务编号"),
            mapFormItemDatePicker("createTime", "计划周期", daterangeConfig)
        ]
    },
    paramsColums: [
        { column: "orderBy", defaultValue: "createTime_desc" },
        { column: "status", defaultValue: "pending" },
        { column: "tenantId" },
        { column: "nameLike" },
        { column: "codeLike" },
        { column: "createTimeBegin", field: "createTime[0]" },
        { column: "createTimeEnd", field: "createTime[1]" }
    ],
    columns: [
        { 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" } },
        { type: "html", field: "product", title: "质检产品", minWidth: 150, sortable: true },
        { type: "html", field: "num", title: "数量", minWidth: 150, sortable: true },
        { field: "status", title: "质检状态", minWidth: 120, editRender: { name: "$cell-tag", options: salesDic.planStatus } },
        { visible: false, type: "html", field: "createTime", title: "创建日期", minWidth: 120, sortable: true, formatter: ({ cellValue }) => TOOL.dateFormat(cellValue, "YYYY-MM-DD") || cellValue },
        { title: "操作", fixed: "right", width: 220, slots: { default: "action" } }
    ]
});

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

const dispatchRef = ref();
const dispatchDescRef = ref();
const dialog = reactive({
    detail: false,
    desc: false
});

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

const table_edit = row => {
    dialog.detail = true;
    nextTick(() => dispatchRef.value?.setData(row));
}

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

const table_del = ({ id }) => {
    ElMessageBox.confirm("是否确认删除该派工单?", "删除警告", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消"
    }).then(() => {
        // API.production.plan.del({ id }).then(() => {
        //     ElMessage.success("操作成功");
        //     refreshTable();
        // });
    }).catch(() => {});
}
</script>