<template>
    <a-form-model layout="vertical" :model="form" :rules="rules" ref="DrawerForm">
        <a-form-model-item label="组织分类" prop="orgType">
            <a-select v-model="form.orgType" :disabled="isView">
                <a-select-option value="ORGANIZATION"> 组织 </a-select-option>
                <a-select-option value="DEPARTMENT"> 部门 </a-select-option>
            </a-select>
        </a-form-model-item>
        <a-form-model-item label="名称" prop="orgName">
            <a-input v-model="form.orgName" :disabled="isView" />
        </a-form-model-item>
        <a-form-model-item label="上级组织名称" prop="parentOrgId">
            <a-select v-model="form.parentOrgId" disabled :options="options" />
        </a-form-model-item>
        <a-form-model-item v-if="form.orgType === 'DEPARTMENT'" label="岗位" prop="orgJobs">
            <RequestSelect
                :formatData="formatData"
                :request="Api.getJobs"
                v-model="form.orgJobs"
                mode="multiple"
                :disabled="isView"
            />
        </a-form-model-item>
        <a-form-model-item label="显示顺序" prop="viewIndex">
            <a-input v-model="form.viewIndex" :disabled="isView" />
        </a-form-model-item>
        <a-form-model-item label="描述" prop="remark">
            <a-textarea v-model="form.remark" :disabled="isView" :rows="4" />
        </a-form-model-item>
    </a-form-model>
</template>

<script>
import Api from '@/api/organization';
import FormMixin from '@/components/FormMixin';
import RequestSelect from '@/components/MySelect/RequestSelect.vue';

export default {
    mixins: [FormMixin],
    components: { RequestSelect },
    props: {
        tableRef: Object,
    },
    created() {
        const dataArr = this.tableRef
            ?.getTableData()
            .map((i) => ({ value: i.orgId, label: i.orgName, key: i.orgId }));
        const parentOrgId = this.tableRef?.getSelectedRowKeys()[0];
        this.options = [...this.options, ...dataArr];
        this.form.parentOrgId = parentOrgId || 0;
    },
    data() {
        return {
            options: [{ value: 0, label: '组织更目录', key: 0 }],
            form: {
                parentOrgId: 0,
            },
            rules: {
                orgName: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
                orgType: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
                viewIndex: [{ required: true, message: 'Please select Activity zone', trigger: 'change' }],
            },
            Api,
            formatData: { value: 'jobId', label: 'jobName', key: 'jobId' },
        };
    },
    methods: {
        add() {
            return Api.addOrg({ ...this.form });
        },
        edit() {
            return Api.updateOrg({ ...this.form });
        },
    },
};
</script>
