<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch"
            label-width="110px">
            <el-form-item :label="tLang('pms','项目编码')" prop="subjectCode">
                <el-input v-model="queryParams.subjectCode" :placeholder="tLang('common', '请输入') + tLang('pms','项目编码')" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item :label="tLang('pms','项目名称')" prop="subjectName">
                <el-input v-model="queryParams.subjectName" :placeholder="tLang('common', '请输入') + tLang('pms','项目名称')" clearable
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item :label="tLang('pms','项目类型')" prop="subjectType">
                <el-select v-model="queryParams.subjectType" :placeholder="tLang('common', '请输入') + tLang('pms','项目类型')" clearable>
                    <el-option v-for="dict in pms_dvsubject_type" :key="dict.value" :label="tLang('pms',dict.label)"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="search" size="mini" @click="handleQuery">{{ tLang('common', '搜索') }}</el-button>
                <el-button icon="refresh" size="mini" @click="resetQuery">{{ tLang('common', '重置') }}</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="plus" size="mini" @click="handleAdd"
                    v-hasPermi="['mes:dv:dvsubject:add']">{{ $t('common.新增') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['mes:dv:dvsubject:edit']">{{ $t('common.修改') }}</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['mes:dv:dvsubject:remove']">{{ $t('common.删除') }}</el-button>
            </el-col>
            <right-toolbar  v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dvsubjectList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column :label="tLang('pms','项目编码')" align="center" prop="subjectCode" />
            <el-table-column :label="tLang('pms','项目类型')" align="center" prop="subjectType">
                <template #default="scope">
                    <dict-tag :options="pms_dvsubject_type" :value="scope.row.subjectType" />
                </template>
            </el-table-column>
            <el-table-column :label="tLang('pms','项目名称')" align="center" prop="subjectName" :show-overflow-tooltip="true" />
            <el-table-column :label="tLang('pms','项目内容')" align="center" prop="subjectContent" :show-overflow-tooltip="true" />
            <el-table-column :label="tLang('pms','标准')" align="center" prop="subjectStandard" :show-overflow-tooltip="true" />
            <!-- <el-table-column :label="是否启用" align="center" prop="enableFlag">
                <template #default="scope">
                    <dict-tag :options="sys_yes_no" :value="scope.row.enableFlag" />
                </template>
            </el-table-column> -->
            <el-table-column :label="tLang('device','备注')" align="center" prop="remark" />
            <el-table-column :label="tLang('common','操作')" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button size="mini" type="text" icon="edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['mes:dv:dvsubject:edit']">{{tLang('common', '修改')}}</el-button>
                    <el-button size="mini" type="text" icon="delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['mes:dv:dvsubject:remove']">{{tLang('common', '删除')}}</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改设备点检保养项目对话框 -->
        <el-dialog :title="title" v-model="open" width="960px" append-to-body>
            <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
                <el-row>
                    <el-col :span="8">
                        <el-form-item :label="tLang('pms','项目编码')" prop="subjectCode">
                            <el-input v-model="form.subjectCode"  disabled />
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <!-- <el-form-item label-width="10">
                            <el-switch v-model="autoGenFlag" active-color="#13ce66" active-text="自动生成"
                                @change="handleAutoGenChange(autoGenFlag)" v-if="form.id === null" />

                        </el-form-item> -->
                    </el-col>
                    <el-col :span="12">
                        <el-form-item :label="tLang('pms','项目编码')" prop="subjectName">
                            <el-input v-model="form.subjectName" :placeholder="tLang('common', '请输入') + tLang('pms','项目名称')" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item :label="tLang('pms','项目类型')" prop="subjectType">
                            <el-radio-group v-model="form.subjectType">
                                <el-radio v-for="dict in pms_dvsubject_type" :key="dict.value" :label="dict.value">
                                    {{ tLang('pms',dict.label) }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="12">
                        <el-form-item label="是否启用">
                            <el-radio-group v-model="form.enableFlag">
                                <el-radio v-for="dict in sys_yes_no" :key="dict.value" :label="dict.value">{{
            dict.label }}</el-radio>
                            </el-radio-group>
                        </el-form-item>
                    </el-col> -->
                </el-row>
                <el-form-item :label="tLang('pms','项目内容')" prop="subjectContent">
                    <el-input type="textarea" v-model="form.subjectContent" :placeholder="tLang('common', '请输入') + tLang('pms','项目内容')" />
                </el-form-item>
                <el-form-item :label="tLang('pms','标准')" prop="subjectStandard">
                    <el-input type="textarea" v-model="form.subjectStandard" :placeholder="tLang('common', '请输入') + tLang('pms','标准')" autosize />
                </el-form-item>
                <el-form-item :label="tLang('common','备注')" prop="remark">
                    <el-input type="textarea" v-model="form.remark" :placeholder="tLang('common', '请输入') + tLang('common','备注')" autosize />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">{{ tLang('common', '确 定') }}</el-button>
                    <el-button @click="cancel">{{ tLang('common', '取 消') }}</el-button>
                </div>
            </template>

        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive, getCurrentInstance } from "vue";
import { listDvsubject, getDvsubject, delDvsubject, addDvsubject, updateDvsubject } from "@/api/pms/dvsubject";
import { genCode } from "@/api/system/autocode/rule"
const { proxy } = getCurrentInstance();

const { sys_yes_no, pms_dvsubject_type } = proxy.useDict("sys_yes_no", "pms_dvsubject_type");


let formRef = ref(null);
let autoGenFlag = ref(false);
// 遮罩层
let loading = ref(true);
// 选中数组
let ids = ref([]);
// 非单个禁用
let single = ref(true);
// 非多个禁用
let multiple = ref(true);
// 显示搜索条件
let showSearch = ref(false);
// 总条数
let total = ref(0);
// 点检设备表格数据
let dvsubjectList = ref([]);
// 弹出层标题
let title = ref("");
// 是否显示弹出层
let open = ref(false);
// 查询参数
let queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    subjectCode: null,
    subjectName: null,
    subjectType: null,
    subjectContent: null,
    subjectStandard: null,
    // enableFlag: null,
})
// 表单参数
let form = ref({})
let rules = {
    subjectCode: [
        { required: true, message: proxy.tLang('pms','项目编码') + proxy.tLang('validate','不能为空'), trigger: "blur" }
    ],
    subjectType: [
        { required: true, message: proxy.tLang('common', '请选择') + proxy.tLang('pms','项目类型'), trigger: "blur" }
    ],
    subjectContent: [
        { required: true, message: proxy.tLang('pms','项目内容') + proxy.tLang('validate','不能为空'), trigger: "blur" }
    ],
    // enableFlag: [
    //     { required: true, message: "是否启用不能为空", trigger: "blur" }
    // ],
    subjectName: [
        { required: true, message: proxy.tLang('pms','项目名称') + proxy.tLang('validate','不能为空'), trigger: "blur" }
    ],
}

// 页面加载
function getList() {
    loading.value = true;
    listDvsubject(queryParams).then(response => {
        dvsubjectList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}


// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        id: null,
        subjectCode: null,
        subjectName: null,
        subjectType: null,
        subjectContent: null,
        subjectStandard: null,
        // enableFlag: "Y",
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
    }
    autoGenFlag.value = false;
    proxy.resetForm("formRef");
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryForm");
    handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.id)
    single.value = selection.length !== 1
    multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = proxy.tLang('common','添加') + proxy.tLang('pms','点检保养项目');
    handleAutoGenChange(true)
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const id = row.id || ids.value
    getDvsubject(id).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = proxy.tLang('common','修改') + proxy.tLang('pms','点检保养项目');
    });
}
/** 提交按钮 */
function submitForm() {
    formRef.value.validate(valid => {
        if (valid) {
            if (form.value.id != null) {
                updateDvsubject(form.value).then(response => {
                    proxy.$modal.msgSuccess(proxy.tLang('common', '修改成功'));
                    open.value = false;
                    getList();
                });
            } else {
                addDvsubject(form.value).then(response => {
                    proxy.$modal.msgSuccess(proxy.tLang('common', '新增成功'));
                    open.value = false;
                    getList();
                });
            }
        }
    });
}
/** 删除按钮操作 */
function handleDelete(row) {
    const subjectIds = row.id || ids.value;
    proxy.$modal.confirm(proxy.tLang('tip', '确定删除选中记录？') + subjectIds ).then(function () {
        return delDvsubject(subjectIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess(proxy.tLang('common', '删除成功'));
    }).catch(() => { });
}
/** 导出按钮操作 */
function handleExport() {
    proxy.download('dv/dvsubject/export', {
        ...queryParams
    }, `dvsubject_${new Date().getTime()}.xlsx`)
}
//自动生成编码
function handleAutoGenChange(autoGenFlag) {
    if (autoGenFlag) {
        genCode('SUBJECT_CODE').then(response => {
            form.value.subjectCode = response;
        });
    } else {
        form.value.subjectCode = null;
    }
}
onMounted(() => {
    getList();
})
</script>

<style lang="scss" scoped></style>
