<template>
    <global-page title="添加公司值班信息" showEdit @handleCancel="handleCancel" @handleSave="handleSave">
        <view class="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"></global-picker>
                </up-form-item>
                <up-form-item :label="formData.isDuty == 0 ? '公司值班经理意见' : '品质中心经理意见'" :borderBottom="true">
                    <global-picker pickAlign="right" clearable v-model="formData.opinionType" dictkey="opinionType"></global-picker>
                </up-form-item>
                <up-form-item
                    :label="formData.isDuty == 0 ? '公司值班经理意见描述' : '品质中心经理意见描述'"
                    :borderBottom="true"
                    labelPosition="top"
                >
                    <up-textarea
                        placeholder="一段很长很长的内容文字，长文本自动换行，该选项的描述是一段很长的内容"
                        :height="70"
                        v-model="formData.msg"
                    ></up-textarea>
                </up-form-item>
            </up-form>
        </view>
    </global-page>
</template>

<script setup>
import { saveRqmOpinionApi } from '@/api/appraisal-record'
import { getAdminListApi } from '@/api/base'
import { onLoad } from '@dcloudio/uni-app'
import { reactive, ref } from 'vue'
import useAppraisalRecordStore from '@/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.$message.showToast(res.message)
    }
}
const handleCancel = () => {
    uni.navigateBack()
}
const handleSave = async () => {
    const params = {
        ...formData
    }
    if (!params.mid) {
        params.mid = -1
    }
    if (!params.opinionType) {
        params.opinionType = -1
    }
    const res = await saveRqmOpinionApi(params, { loading: true })
    if (res.code == 200) {
        uni.navigateBack()
        appraisalRecordStore.getRqmDetails()
        uni.$message.showToast(res.message)
    } else {
        uni.$message.showToast(res.message)
    }
}
</script>

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