<template>
  <div v-loading="isLoading" class="shop-detail">
    <el-form v-if="product!==null" ref="formRef" :model="formData" :rules="formRules" label-width="120px">
      <el-form-item label="设备名称：">
        <span>{{ product.productName }}</span>
      </el-form-item>
      <el-form-item label="设备SN码：">
        <span>{{ product.snCode }}</span>
      </el-form-item>
      <el-form-item label="设备图片：">
        <el-image
          style="width: 140px; height: 140px"
          :src="product.productImage"
          :preview-src-list="imgList"
        />
      </el-form-item>
      <el-form-item label="授权牌：">
        <el-image
          style="width: 140px; height: 140px"
          :src="product.certificateImage"
          :preview-src-list="imgList"
        />
      </el-form-item>
      <template v-if="product.paramList.length>0">
        <el-form-item label="相关参数：">
          <table>
            <tr v-for="(params,index) in product.paramList" :key="index">
              <td>{{ params.paramName }}：</td>
              <td>{{ params.paramContent }}</td>
            </tr>
          </table>
        </el-form-item>
      </template>

      <el-form-item label="审核状态：">
        <el-radio-group v-model="formData.auditStatus">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="0">不通过</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="formData.auditStatus!==1" prop="invalidReason" label="原因：">
        <el-input v-model="formData.invalidReason" type="textarea" placeholder="请说明原因" />
      </el-form-item>
      <el-form-item>
        <el-button type="info" @click="$_back">返回</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProductById, auditProduct } from '@/api/product'
import { mapGetters, mapMutations } from 'vuex'
export default {
  data() {
    return {
      authId: '', // 当前产品所属授权机构id
      isLoading: false,
      formData: {
        auditBy: '', // 审核人id
        productId: '', // 商品id
        auditStatus: 1, // 商品审核状态
        invalidReason: '' // 审核信息
      },
      formRules: {
        invalidReason: { required: true, message: '不通过原因不能为空', tigger: 'blur' }
      },
      imgList: [], // 图片列表
      product: null // 商品信息
    }
  },
  computed: {
    ...mapGetters(['authUserId'])
  },
  created() {
    this.formData.productId = this.$route.query.id
    this.authId = this.$route.query.authId
    this.getDetail()
  },
  methods: {
    ...mapMutations({ setMessageState: 'webSocket/SET_MESSAGE_STATE' }),
    // 获取商品详情
    getDetail() {
      this.isLoading = true
      getProductById({ productId: this.formData.productId })
        .then(res => {
          if (res.code !== 0) return
          this.product = res.data
          this.imgList = [this.product.productImage, this.product.certificateImage]
        }).finally(() => {
          this.isLoading = false
        })
    },
    // 提交审核信息
    submit() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.isLoading = true
          // 指定审核人
          this.formData.auditBy = this.authUserId
          // 提交审核信息
          auditProduct(this.formData).then(res => {
            if (res.code !== 0) return
            this.$message.success(res.data)
            this.$store.dispatch('tagsView/delView', this.$route)
            this.$router.back()
            // this.setMessageState({ id: parseInt(this.formData.productId), type: 2 })
          }).finally(() => {
            this.isLoading = false
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.shop-detail{
  margin-bottom: 80px;
  font-size: 14px;
  color: #444;
  .params-list{
    margin: 0;
    padding: 0;
    list-style: none;
    li{
      margin-bottom: 15px;
    }
  }
  .el-form {
    width: 800px;
    margin: 25px auto 0;
    .el-button{
      width: 120px;
    }
  }
}
</style>
