<template>
  <div class="page-form-container">
    <device-detail :product-id="auditForm.productId" :relation-id="auditForm.relationId" />
    <template v-if="type === 'audit'">
      <el-form ref="auditForm" label-width="112px" :model="auditForm" :rules="rules">
        <el-form-item label="审核：">
          <el-radio-group v-model="auditForm.auditStatus">
            <el-radio :label="1">通过</el-radio>
            <el-radio :label="0">不通过</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="auditForm.auditStatus === 0" label="原因：" prop="invalidReason">
          <el-input v-model="auditForm.invalidReason" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <div class="control-box">
        <el-button type="info" @click="navigateBack">返回</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </template>
  </div>
</template>

<script>
import { auditProduct, auditProductCheck } from '@/api/product'
import { mapGetters } from 'vuex'
import { DeviceDetail } from '@/views/components'
export default {
  components: {
    DeviceDetail
  },
  data() {
    return {
      type: 'review',
      auditForm: {
        authId: '',
        productId: '',
        relationId: '',
        auditStatus: 1, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      }
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.auditForm.authId = this.$route.query.authId
    this.auditForm.productId = this.$route.query.productId
    this.auditForm.relationId = this.$route.query.relationId
    this.type = this.$route.query.type
    this.auditAuthCheck()
  },
  methods: {
    async auditAuthCheck() {
      try {
        await auditProductCheck(this.auditForm.productId)
      } catch (error) {
        console.log(error)
      }
    },

    // 提交审核信息
    async submit() {
      try {
        await this.$refs.auditForm.validate()
        this.onAudit()
      } catch (error) {
        console.log(error)
      }
    },

    // 审核
    async onAudit() {
      try {
        this.isLoading = true
        const res = await auditProduct(this.auditForm)
        this.$message.success(res.data)
        this.$store.dispatch('tagsView/delView', this.$route)
        this.$router.back()
      } catch (error) {
        console.log(error)
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>
