<template>
    <global-page title="考核记录登记" showFooterBtn @handleFooterClick="handleSubmit">
        <view class="content">
            <view class="mocp-form">
                <up-form labelPosition="left" labelWidth="auto" :model="formData" ref="formRef" :rules="rules">
                    <up-form-item label="事件来源" :borderBottom="true">
                        <up-input border="none" inputAlign="right" placeholder="请输入" clearable v-model="formData.eventSource"></up-input>
                    </up-form-item>
                    <up-form-item label="基地/职能部门" :borderBottom="true">
                        <global-picker
                            pickAlign="right"
                            clearable
                            v-model="formData.department"
                            :options="department"
                            emptyValue="-1"
                        ></global-picker>
                    </up-form-item>
                    <up-form-item label="考核对象" :borderBottom="true">
                        <global-picker pickAlign="right" clearable v-model="formData.appraisee" :options="appraisee" emptyValue="-1"></global-picker>
                    </up-form-item>
                    <up-form-item label="机号" :borderBottom="true">
                        <global-picker pickAlign="right" clearable :options="deviceNumList" v-model="formData.ac" @change="changeAc"></global-picker>
                    </up-form-item>
                    <up-form-item label="客户" :borderBottom="true">
                        <up-input border="none" inputAlign="right" clearable v-model="formData.acOwn" disabled disabledColor="#fff"></up-input>
                    </up-form-item>
                    <up-form-item label="机型" :borderBottom="true">
                        <up-input border="none" inputAlign="right" clearable v-model="formData.acType" disabled disabledColor="#fff"></up-input>
                    </up-form-item>
                    <up-form-item label="日期" :borderBottom="true" required prop="eventTime">
                        <global-date pickAlign="right" clearable v-model="formData.eventTime"></global-date>
                    </up-form-item>
                    <up-form-item label="事件描述" :borderBottom="true" labelPosition="top">
                        <up-textarea
                            placeholder="一段很长很长的内容文字，长文本自动换行，该选项的描述是一段很长的内容"
                            v-model="formData.eventMsg"
                            :height="40"
                            border="none"
                        ></up-textarea>
                    </up-form-item>
                    <up-form-item label="事件类别" :borderBottom="true">
                        <global-picker pickAlign="right" clearable v-model="formData.eventType" dictkey="ar_eventType" :emptyValue="-1"></global-picker>
                    </up-form-item>
                    <up-form-item label="考核类型" :borderBottom="true">
                        <global-picker
                            pickAlign="right"
                            clearable
                            v-model="formData.examineType"
                            :options="examineType"
                            emptyValue="-1"
                        ></global-picker>
                    </up-form-item>
                    <up-form-item label="考核依据" :borderBottom="true" labelPosition="top">
                        <up-textarea
                            placeholder="一段很长很长的内容文字，长文本自动换行，该选项的描述是一段很长的内容"
                            v-model="formData.examineBasis"
                            :height="40"
                            border="none"
                        ></up-textarea>
                    </up-form-item>
                    <up-form-item label="分值" :borderBottom="true">
                        <up-input
                            border="none"
                            inputAlign="right"
                            placeholder="请输入"
                            v-model.number="formData.score"
                            type="number"
                            clearable
                        ></up-input>
                    </up-form-item>
                    <up-form-item label="当班人员" :borderBottom="true">
                        <up-input border="none" inputAlign="right" placeholder="请输入" v-model="formData.onDutyUser" clearable></up-input>
                    </up-form-item>
                </up-form>
            </view>
            <card-details
                type="company"
                title="公司值班信息"
                showfooter
                :idcard="details.dmName"
                :calendar="details.optionSaveTime"
                edit
                @handleAdd="handleAdd(0)"
                v-if="details"
            >
                <card-details-item label="公司值班经理" :value="details.dmName || '-'"></card-details-item>
                <card-details-item
                    label="公司值班经理意见"
                    :value="details.opinionType != -1 ? useGetDictByValue('ar_opinionType', details.opinionType) : '-'"
                ></card-details-item>
                <card-details-item label="公司值班经理意见描述" :value="details.dmMsg || '-'"></card-details-item>
            </card-details>
            <card-details
                type="quality"
                title="品质中心信息"
                showfooter
                :idcard="details.qmName"
                :calendar="details.qualitySaveTime"
                edit
                @handleAdd="handleAdd(1)"
                v-if="details"
            >
                <card-details-item label="品质中心经理" :value="details.qmName || '-'"></card-details-item>
                <card-details-item
                    label="品质中心经理意见"
                    :value="details.qualityOpinionType != -1 ? useGetDictByValue('ar_opinionType', details.qualityOpinionType) : '-'"
                ></card-details-item>
                <card-details-item label="品质中心经理意见描述" :value="details.qmMsg || '-'"></card-details-item>
            </card-details>
            <view class="appeal">
                <view class="appeal-title">
                    <text>申诉情况</text>
                    <global-picker pickAlign="right" dictkey="ar_appealInfo" v-model="formData.appealInfo" clearable></global-picker>
                </view>
                <view class="appeal-content">
                    <global-upload :fileList="getFileList" v-model="formData.file"></global-upload>
                </view>
            </view>
        </view>
    </global-page>
</template>

<script setup>
import CardDetails from './components/card-details.vue'
import CardDetailsItem from './components/card-details-item.vue'
import { onShow } from '@dcloudio/uni-app'
import useBaseStore from 'mocp/store/base'
import { useGetDictByValue } from 'mocp/hooks/use-dict/useDict'
import { rules, formData, formRef } from './constants/edit.compositions'
import { changeAc } from './constants/edit.functionals'
import useAppraisalRecordStore from 'mocp/store/appraisal-record'
import { storeToRefs } from 'pinia'
import { updateRqmDataApi } from 'mocp/api/appraisal-record'

const appraisalRecordStore = useAppraisalRecordStore()
const { details, getFileList } = storeToRefs(appraisalRecordStore)
//获取下拉框选项
const {
    selectList: { deviceNumList }
} = useBaseStore()
const {
    selectList: { appraisee, department, examineType }
} = useAppraisalRecordStore()
// 页面初始化
onShow(() => {
    formData.id = details.value.id
    formData.ac = details.value.ac
    formData.acOwn = details.value.acOwn
    formData.acType = details.value.acType
    formData.appealInfo = details.value.appealInfo
    formData.appraisee = details.value.appraisee
    formData.department = details.value.department
    formData.dmUid = details.value.dmUid
    formData.eventMsg = details.value.eventMsg
    formData.eventSource = details.value.eventSource
    formData.eventTime = String(details.value.eventTime)
    formData.eventType = details.value.eventType
    formData.examineBasis = details.value.examineBasis
    formData.examineType = details.value.examineType
    formData.onDutyUser = details.value.onDutyUser
    formData.opinionMsg = details.value.opinionMsg
    formData.opinionType = details.value.opinionType
    formData.score = details.value.score
    formData.qmUid = details.value.qmUid
    formData.qualityOpinionMsg = details.value.qualityOpinionMsg
    formData.qualityOpinionType = details.value.qualityOpinionType
    formData.status = details.value.status
    formData.emailSendTime = details.value.emailSendTime
})
//提交表单
const handleSubmit = async () => {
    await formRef.value?.validate()
    const params = { ...formData, file: JSON.stringify(formData.file) }
    const res = await updateRqmDataApi(params, { loading: true })
    if (res.code == 200) {
        uni.$mocpJump.navigateBack()
        appraisalRecordStore.getRqmDetails()
        uni.$emit('appraisalRecordReload')
        uni.$mocpMessage.showToast(res.message)
    } else {
        uni.$mocpMessage.showToast(res.message)
    }
}
// 添加
const handleAdd = (isDuty) => {
    let params = {}
    let realName = ''
    if (isDuty == 0) {
        params = { id: details.value.id, isDuty, mid: details.value.dmUid, opinionType: details.value.opinionType, msg: details.value.dmMsg }
        realName = details.value.dmName
    } else {
        params = { id: details.value.id, isDuty, mid: details.value.qmUid, opinionType: details.value.qualityOpinionType, msg: details.value.qmMsg }
        realName = details.value.qmName
    }
    uni.$mocpJump.navigateTo('/panel/appraisal-record/add').then(() => {
        uni.$emit('appraisalRecordAdd', params, realName)
    })
}
</script>
<style lang="scss" scoped>
@import './constants/edit.scss';
</style>
