<template>
  <div class="page-form-container cate-review">
    <div class="row">
      <div class="col">设备名称：</div>
      <div class="col">{{ cateData.name }}</div>
    </div>
    <div class="row">
      <div class="col">设备图片：</div>
      <div class="col">
        <preview-image v-if="cateData.image" class="preview-image" :src="cateData.image" />
      </div>
    </div>
    <div class="row">
      <div class="col">所属品牌：</div>
      <div class="col">{{ cateData.brandName }}</div>
    </div>
    <div class="row">
      <div class="col">相关参数：</div>
      <div class="col">
        <param-list :list="cateData.paramList" />
      </div>
    </div>
    <div class="row">
      <div class="col">审核状态：</div>
      <div class="col">
        <audit-status :status="cateData.auditStatus" :reason="cateData.invalidReason" />
      </div>
    </div>
    <el-form v-if="type === 'audit'" 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-item>
        <div class="control-box" style="text-align: left">
          <el-button type="info" @click="navigateBack">返回</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { auditProductCate, auditProductCateCheck, fetchAuthProductFormData } from '@/api/product'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      type: 'review',
      isLoading: false,
      auditForm: {
        productTypeId: '', // 机构id
        auditStatus: 1, // 审核状态
        invalidReason: '' // 审核信息
      },
      rules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      },
      cateData: {}
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.auditForm.productTypeId = this.$route.query.id
    this.type = this.$route.query.type
    this.getDetail()
    this.auditAuthCheck()
  },
  methods: {
    async auditAuthCheck() {
      try {
        await auditProductCateCheck(this.auditForm.productTypeId)
      } catch (error) {
        console.log(error)
      }
    },

    // 数据回显
    async getDetail() {
      try {
        const { data } = await fetchAuthProductFormData({ productTypeId: this.auditForm.productTypeId })
        this.cateData = data
      } catch (error) {
        console.log(error)
      }
    },

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

    // 审核
    async onAudit() {
      try {
        this.isLoading = true
        const res = await auditProductCate(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>
<style lang="scss" scoped>
.row {
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  margin-bottom: 25px;
  .col {
    font-size: 14px;
    color: #333;
    &:first-child {
      min-width: 100px;
      text-align: right;
      margin-right: 8px;
      font-weight: bold;
      color: #666;
    }

    .param {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      // grid-template-rows: repeat(2, 1fr);
      grid-column-gap: 0px;
      grid-row-gap: 0px;

      div {
        padding-bottom: 16px;
        text-align: left;

        &:first-child {
          text-align: right;
        }
      }
    }

    .preview-image {
      margin-left: 12px;
      &:first-child {
        margin-left: 0;
      }
    }
  }
}

.el-button {
  width: 120px;
}
</style>
