<template>
    <global-page
        :title="formData.isDuty == 0 ? '添加公司值班信息' : '添加品质中心信息'"
        navLeftType="text"
        showNavRight
        navRightType="button"
        navRightText="保存"
        @handleRightClick="handleRightClick"
    >
        <view class="mocp-form">
            <up-form labelPosition="left" labelWidth="auto">
                <up-form-item :label="formData.isDuty == 0 ? '公司值班经理' : '品质中心经理'" :borderBottom="true">
                    <global-picker pickAlign="right" clearable v-model="formData.mid" :options="selectList" emptyValue="-1"></global-picker>
                </up-form-item>
                <up-form-item :label="formData.isDuty == 0 ? '公司值班经理意见' : '品质中心经理意见'" :borderBottom="true">
                    <global-picker pickAlign="right" clearable v-model="formData.opinionType" dictkey="ar_opinionType" :emptyValue="-1"></global-picker>
                </up-form-item>
                <up-form-item
                    :label="formData.isDuty == 0 ? '公司值班经理意见描述' : '品质中心经理意见描述'"
                    :borderBottom="true"
                    labelPosition="top"
                >
                    <up-textarea
                        placeholder="一段很长很长的内容文字，长文本自动换行，该选项的描述是一段很长的内容"
                        :height="40"
                        border="none"
                        v-model="formData.msg"
                    ></up-textarea>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { saveRqmOpinionApi } from 'mocp/api/appraisal-record'
import { getAdminListApi } from 'mocp/api/base'
import { onLoad } from '@dcloudio/uni-app'
import { reactive, ref } from 'vue'
import useAppraisalRecordStore from 'mocp/store/appraisal-record'

const appraisalRecordStore = useAppraisalRecordStore()
//表单数据
const formData = reactive({
    id: '',
    isDuty: 0,
    mid: '-1',
    opinionType: -1,
    msg: ''
})
onLoad(() => {
    uni.$once('appraisalRecordAdd', (data, realName) => {
        Object.assign(formData, data)
        getAdminList(realName || '')
    })
})

//获取下拉框列表
const selectList = ref([])
const getAdminList = async (realName) => {
    const params = {
        pageIndex: 1,
        pageSize: 100,
        realName
    }
    const res = await getAdminListApi(params, { loading: true })
    if (res.code == 200) {
        selectList.value = res.data.list.map((item) => {
            return {
                label: `${item.nickname}(${(item.enetAccount || '无易网号') + '-' + (item.deptFullName || '无机构')})`,
                value: item.id
            }
        })
    } else {
        uni.$mocpMessage.showToast(res.message)
    }
}
const handleRightClick = async () => {
    const res = await saveRqmOpinionApi(formData, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        appraisalRecordStore.getRqmDetails()
        uni.$mocpMessage.showToast(res.message)
    } else {
        uni.$mocpMessage.showToast(res.message)
    }
}
</script>

<style lang="scss" scoped>
.mocp-form {
    background: #fff;
    padding: 0 32rpx;
}
</style>
