<template>
  <el-dialog
    :title="title"
    :visible.sync="modal"
    width="80%"
    :close-on-click-modal="false"
    @close="
      res => {
        $emit('cancel');
      }
    "
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="上传位置">
        {{ form.kindergartenName }}/ {{ form.activityName }}
      </el-form-item>

      <el-form-item label="图片"
        ><img
          :src="form.listPreview"
          class="pre-img"
          @click="
            () =>
              this.$AdvanceViewImageModal({
                items: [{ src: form.listPreview }]
              })
          "
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-radio-group v-if="auditStatus !== 1" v-model="form.auditStatus">
          <el-radio :label="1">通过</el-radio>
          <el-radio :label="-1">不通过</el-radio>
        </el-radio-group>
        <span v-else>{{ form.auditStatus | auditStatusName }}</span>
      </el-form-item>
      <el-form-item label="审核备注">
        <el-input
          v-if="auditStatus !== 1"
          v-model="form.auditMsg"
          type="textarea"
          :rows="2"
          placeholder="请输入内容"
        >
        </el-input>
        <span v-else>{{ form.auditMsg }}</span>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="modal = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getItem, auditItem } from '@/api/photoWarehouse';

export default {
  name: 'photoVerifyItemModal',

  filters: {
    auditStatusName: function(num) {
      switch (num) {
        case 1:
          return '通过';
        case 0:
          return '待审核';
        case -1:
          return '拒绝';
        default:
          return '-';
      }
    }
  },

  props: {
    id: {
      type: String,
      default: ''
    },
    auditStatus: {
      type: Number,
      default: 0
    }
  },

  data() {
    return {
      title: '照片审核详情',
      modal: true,
      form: {
        auditStatus: null,
        auditMsg: ''
      },
      rules: {
        auditStatus: [
          { required: true, message: '请选择是否通过', trigger: 'blur' }
        ]
      }
    };
  },

  watch: {
    id: {
      handler(id) {
        id && this.loadData();
      },
      immediate: true
    }
  },

  mounted() {},

  methods: {
    async loadData() {
      const { success, data, msg } = await getItem({
        id: this.id
      });
      if (success) {
        this.form = data;
      }
    },

    handleConfirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          const params = {
            auditStatus: this.form.auditStatus,
            auditMsg: this.form.auditMsg
          };
          if (this.id) params.id = this.id;
          const { success, msg } = await auditItem(params);
          if (success) {
            this.$success('保存成功！');
            this.modal = false;
            this.$g_emit('photo_reload');
          }
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.pre-img {
  height: 80px;
}
</style>
