<template>
  <div class="app-container">
    <el-form ref="form" :model="form" :rules="rules" label-width="160px">
      <el-card v-loading="loading" shadow="never">
        <div slot="header" class="card-header">
          <span>店铺设置</span>
          <span class="header-tip">全平台设置，针对所有店铺生效</span>
        </div>
        <el-form-item label="商品是否默认审核通过" prop="defaultAuditPass">
          <el-radio-group v-model="form.defaultAuditPass">
            <el-radio label="1">开启（提交上架后直接出售中）</el-radio>
            <el-radio label="0">关闭（提交上架后待审核）</el-radio>
          </el-radio-group>
          <div class="form-tip">修改后仅对<strong>此后新提交上架</strong>的商品生效，已有商品状态不变</div>
        </el-form-item>
        <el-form-item label="最大子管理员数量" prop="maxSubAdmin">
          <el-input-number v-model="form.maxSubAdmin" :min="1" :max="99" :precision="0" controls-position="right" />
          <span class="form-tip-inline">全平台统一配额，范围 1～99，默认 5</span>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" v-hasPermi="['agri:shop:setting']">保 存</el-button>
          <el-button @click="loadConfig">重 置</el-button>
        </el-form-item>
      </el-card>
    </el-form>
  </div>
</template>

<script>
import { getShopSetting, saveShopSetting } from "@/api/agri/shopSetting"

export default {
  name: "AgriShopSetting",
  data() {
    return {
      // 页面加载遮罩
      loading: true,
      // 表单数据
      form: {
        defaultAuditPass: "0",
        maxSubAdmin: 5
      },
      // 表单校验规则
      rules: {
        defaultAuditPass: [
          { required: true, message: "请选择商品默认审核策略", trigger: "change" }
        ],
        maxSubAdmin: [
          { required: true, message: "请输入子管理员上限", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.loadConfig()
  },
  methods: {
    /** 加载全局店铺设置 */
    loadConfig() {
      this.loading = true
      getShopSetting().then(response => {
        const data = response.data || {}
        this.form = {
          defaultAuditPass: data.defaultAuditPass != null ? data.defaultAuditPass : "0",
          maxSubAdmin: data.maxSubAdmin != null ? data.maxSubAdmin : 5
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    /** 保存全局店铺设置 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return
        }
        saveShopSetting(this.form).then(response => {
          this.$modal.msgSuccess(response.msg || "商品默认审核设置仅对此后新提交上架的商品生效")
          this.loadConfig()
        })
      })
    }
  }
}
</script>

<style scoped>
.card-header {
  display: flex;
  align-items: center;
  gap: 12px;
}
.header-tip {
  color: #909399;
  font-size: 13px;
  font-weight: normal;
}
.form-tip {
  color: #909399;
  font-size: 12px;
  line-height: 1.5;
  margin-top: 4px;
}
.form-tip-inline {
  color: #909399;
  font-size: 12px;
  margin-left: 8px;
}
</style>
