<template>
  <div>
    <el-row>
      <el-col :span="24">
        <el-card class="box-card">
          <div>
            <el-form
              ref="form"
              label-width="100px"
              :model="form"
              :rules="rules"
              size="medium"
            >
              <el-form-item
                :label="__('平台转单抽佣比例')"
                label-width="200px"
                prop="transferPla"
              >
                <el-input
                  v-model="form.transferPla"
                  clearable
                  :placeholder="__('单位 - %')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('转单总比例')"
                label-width="200px"
                prop="transferSto"
              >
                <el-input
                  v-model="form.transferSto"
                  clearable
                  :placeholder="__('单位 - %')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
              <el-form-item
                :label="__('设定骑手抢单范围/公里')"
                label-width="200px"
                prop="scope"
              >
                <el-input
                  v-model="form.scope"
                  clearable
                  :placeholder="__('单位为公里')"
                  :style="{ width: '100%' }"
                />
              </el-form-item>
            </el-form>
          </div>
          <div>
            <el-button
              class="form-sub-btn"
              icon="el-icon-s-tools"
              type="primary"
              @click="handleEdit"
            >
              {{ __('修改') }}
            </el-button>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doEdit } from '@/api/base/config'

  export default {
    name: 'Paotui',
    components: {},
    data() {
      return {
        form: {
          transferPla: undefined,
          transferSto: undefined,
          scope: '',
        },
        rules: {
          transferPla: [
            {
              required: true,
              message: '单位 - %',
              trigger: 'blur',
            },
          ],
          transferSto: [
            {
              required: true,
              message: '单位 - %',
              trigger: 'blur',
            },
          ],
          scope: [
            {
              required: true,
              message: '单位为公里',
              trigger: 'blur',
            },
          ],
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          transferPla: data.transferPla,
          transferSto: data.transferSto,
          scope: data.scope,
        }
      },
      handleEdit() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit({
              param: JSON.stringify(this.form),
            })

            if (status == 200) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
