<template>
  <div class="page-form-container">
    <device-detail :product-id="productId" />
    <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 } from '@/api/product'
import { mapGetters } from 'vuex'
import { DeviceDetail } from '@/views/components'
export default {
  components: {
    DeviceDetail
  },
  data() {
    return {
      type: 'review',
      productId: '',
      auditForm: {
        productId: '',
        auditBy: '', // 审核人id
        authId: '', // 机构id
        auditStatus: 1, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      }
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.authId = this.$route.query.authId
    this.productId = this.$route.query.productId
    this.type = this.$route.query.type
  },
  methods: {
    // 提交审核信息
    submit() {
      this.auditForm.productId = this.productId
      this.$refs.auditForm.validate((valid) => {
        if (valid) {
          this.isLoading = true
          // 指定审核人
          this.auditForm.auditBy = this.authUserId
          auditProduct(this.auditForm)
            .then((res) => {
              this.$message.success(res.data)
              this.$store.dispatch('tagsView/delView', this.$route)
              this.$router.back()
            })
            .finally(() => {
              this.isLoading = false
            })
        }
      })
    }
  }
}
</script>
