<template>
  <view class="content">
    <view class="card">
      <u-form labelPosition="top" :model="form" :rules="rules" ref="formRef" labelWidth="140px">
        <u-form-item label="审批结果" prop="APPROVAL_RESULTS" borderBottom required labelPosition="left">
          <u-radio-group v-model="form.APPROVAL_RESULTS">
            <u-radio label="通过" name="1"/>
            <u-radio :customStyle="{marginLeft: '8px'}" label="不通过" name="0"/>
          </u-radio-group>
        </u-form-item>
        <u-form-item label="审批意见" prop="APPROVAL_OPINIONS" borderBottom required>
          <u-textarea v-model="form.APPROVAL_OPINIONS" border="none" autoHeight/>
        </u-form-item>
        <u-form-item label="签字" prop="SIGN" borderBottom required>
          <view style="flex: 1">
            <view>
              <u-button type="primary" size="mini" text="签字"
                        :customStyle="{position: 'absolute',top: '-46upx',right: '20upx',width: '100upx'}"
                        @click="signVisible = true"/>
            </view>
            <view v-if="form.SIGN">
              <u-image width="400rpx" height="200rpx" :src="form.SIGN"/>
            </view>
          </view>
        </u-form-item>
      </u-form>
      <view class="mt-10">
        <u-button type="primary" text="下一步" @click="$u.debounce(fnSubmit, 1000,true)"/>
      </view>
    </view>
    <sign :signShow.sync="signVisible" @confirm="fnSign"/>
  </view>
</template>

<script>
import Sign from '@/components/sign/sign.vue'
import {setTaskFile, setTaskSign} from "@/api";
import {resolveNextOperation} from "@/utils/submitHomeworkProcess";

export default {
  components: {
    Sign,
  },
  data() {
    return {
      EW_RU_JOB_ID: '',
      TYPE: '',
      taskId: '',
      CORP_ID: '',
      vernier: '',
      signVisible: false,
      form: {
        APPROVAL_RESULTS: '',
        APPROVAL_OPINIONS: '',
        SIGN: '',
      },
      rules: {
        APPROVAL_RESULTS: [{type: 'string', required: true, message: '请选择审批结果', trigger: ['blur', 'change']}],
        APPROVAL_OPINIONS: [{type: 'string', required: true, message: '请输入审批意见', trigger: ['blur', 'change']}],
        SIGN: [{type: 'string', required: true, message: '请签字', trigger: ['blur', 'change']}],
      },
    }
  },
  onLoad(query) {
    this.EW_RU_JOB_ID = query.EW_RU_JOB_ID
    this.TYPE = query.TYPE
    this.taskId = query.taskId
    this.vernier = query.vernier
  },
  methods: {
    fnSign(event) {
      this.form.SIGN = event.filePath
    },
    async fnSubmit() {
      try {
        await this.$refs.formRef.validate()
        try {
          const {CODE} = await setTaskFile({formData: {type: 0}, name: 'files', filePath: this.form.SIGN})
          await setTaskSign({
            IMG_CODE: CODE,
            ...this.form,
            SIGN: '',
            EW_RU_JOB_ID: this.EW_RU_JOB_ID,
            EW_RU_TASK_ID: this.taskId
          })
          await resolveNextOperation({
            EW_RU_TASK_ID: this.taskId,
            CORP_ID: null,
            TYPE: this.TYPE,
            EW_RU_JOB_ID:this.EW_RU_JOB_ID,
            vernier: this.vernier
          })
        } catch(e) {
          console.log(e)
        }
      } catch {
        uni.$u.toast('请补全必填项')
      }
    }
  },
}
</script>

<style scoped lang="scss">

</style>
