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

        <scTable ref="xGridTable" :apiObj="$API.basic.qualityPlan" :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 }">
                <template v-if="row.reviewStatus == '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 v-if="$TOOL.data.get('USER_INFO').id == row.reviewUserId" type="primary" link @click="table_detail(row, 'approval')">
                        <template #icon><sc-iconify icon="material-symbols:approval-outline"></sc-iconify></template>审批
                    </el-button>
                </template>
                <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>

    <plan-detail v-if="dialog.detail" ref="planRef" @success="refreshTable" @closed="dialogClose"></plan-detail>
    <plan-desc v-if="dialog.desc" ref="planDescRef" @success="refreshTable" @closed="dialog.desc = false"></plan-desc>
</template>

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

import API from "@/api";
import TOOL from "@/utils/tool";
import { reviewStatusDic, qualityPlanTypeDic } from "@/utils/basicDic";
import { mapFormItemInput, mapFormItemSelect, mapFormItemDatePicker, mapFormItemTenant } from "@/components/scTable/helper";
import planDetail from "./detail";
import planDesc from "./desc";

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

const selectConfig = reactive({
    options: reviewStatusDic,
    events: {
        change: data => XEUtils.merge(formConfig.data, data)
    }
});

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", "方案编号"),
        mapFormItemSelect("reviewStatus", "审批状态", selectConfig),
        mapFormItemSelect("type", "方案类型", { ...selectConfig, options: qualityPlanTypeDic }),
        mapFormItemDatePicker("createTime", "创建日期", daterangeConfig)
    ]
});

const paramsColums = reactive([
    { column: "orderBy", defaultValue: "createTime_desc" },
    { column: "tenantId" },
    { column: "nameLike" },
    { column: "codeLike" },
    { column: "reviewStatus" },
    { column: "type" },
    { 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: "所属租户", 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" } },
    { visible: false, type: "html", field: "inspectUserName", title: "质检人员", minWidth: 120, sortable: true },
    { type: "html", field: "type", title: "方案类型", minWidth: 100, sortable: true, formatter: ({ cellValue }) => XEUtils.get(qualityPlanTypeDic, cellValue, cellValue) },
    { visible: false, type: "html", field: "sampleRate", title: "抽检比例", minWidth: 120, sortable: true },
    { visible: false, type: "html", field: "passedRate", title: "合格率", minWidth: 120, sortable: true },
    { visible: false, type: "html", field: "reviewUserName", title: "审批人员", minWidth: 120, sortable: true },
    { field: "reviewStatus", title: "审批状态", minWidth: 100, editRender: { name: "$cell-tag", options: reviewStatusDic } },
    { type: "html", field: "createTime", title: "创建日期", minWidth: 120, sortable: true, formatter: ({ cellValue }) => TOOL.dateFormat(cellValue, "YYYY-MM-DD") || cellValue },
    { visible: false, type: "html", field: "remark", title: "概要", minWidth: 300, sortable: true },
    { title: "操作", fixed: "right", width: 220, slots: { default: "action" } }
]);

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

const planRef = ref();
const planDescRef = ref();
const dialog = reactive({
    detail: false,
    desc: false
});
const table_add = () => {
    dialog.detail = true;
    nextTick(() => planRef.value?.open());
}

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

const table_detail = (row, mode = "detail") => {
    dialog.desc = true;
    nextTick(() => planDescRef.value?.setData(row, mode));
}

const table_del = ({ id }) => {
    ElMessageBox.confirm("是否确认删除该质检方案?", "删除警告", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消"
    }).then(() => {
        API.basic.qualityPlan.del({ id }).then(() => {
            ElMessage.success("操作成功");
            refreshTable();
        });
    }).catch(() => {});
}

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