<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="750px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="form.useStatus == 2 ? '审核通过' : '审核驳回'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" label-width="82px">
      <el-form-item label="物流单号:" prop="title" v-if="form.useStatus == 2">
        <el-input
          clearable
          :maxlength="60"
          v-model="form.descrip"
          placeholder="请输入物流单号"
        />
      </el-form-item>
      <el-form-item label="驳回理由:" prop="title" v-else>
        <el-input
          clearable
          v-model="form.descrip"
          :rows="2"
          type="textarea"
          placeholder="请输入驳回理由"
        />
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { updateGroupsExchangeUseStatus } from '@/api/sylive/groups-exchange';
  export default {
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        cfId: null,
        type: null,
        useStatus: 0,
        descrip: ''
      };
      return {
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 提交状态
        loading: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 保存编辑 */
      save() {
        if (this.form.useStatus != 2 && !this.form.descrip) {
          this.$message.error('请输入驳回理由');
          return;
        }
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          updateGroupsExchangeUseStatus(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    watch: {
      visible(visible) {
        if (visible) {
          if (this.data.cfId) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
