<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="150px"
                prop="supply_transfer_flag"
              >
                <el-radio-group
                  v-model="form.supply_transfer_flag"
                  size="medium"
                >
                  <el-radio
                    v-for="(item, index) in supply_transfer_flagOptions"
                    :key="index"
                    :disabled="item.disabled"
                    :label="item.value"
                  >
                    {{ item.label }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item
                :label="__('超时转单/小时')"
                label-width="150px"
                prop="order_rsync_time"
              >
                <el-tooltip class="item" effect="dark" content="当商家超过此处设置时长未处理订单时，自动转单给供应商（单位为小时）" placement="top-start">
                  <el-input
                    v-model="form.order_rsync_time"
                    clearable
                    :placeholder="__('单位为小时')"
                    :style="{ width: '100%' }"
                  />
                </el-tooltip>
              </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'
  import { URL } from '@/config'

  export default {
    name: 'Supply',
    components: {},
    data() {
      return {
        form: {
          supply_transfer_flag: '',
          order_rsync_time: '',
        },
        rules: {
          supply_transfer_flag: [
            {
              required: true,
              message: this.__('是否支持一件代发不能为空'),
              trigger: 'change',
            },
          ],
          order_rsync_time: [
            {
              required: true,
              message: this.__('单位为小时'),
              trigger: 'blur',
            },
          ],
        },
        supply_transfer_flagOptions: [
          {
            label: this.__('是'),
            value: '1',
          },
          {
            label: this.__('否'),
            value: '2',
          },
        ],
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      async fetchData() {
        const { data } = await getList()
        this.form = {
          supply_transfer_flag: data.supply_transfer_flag,
          order_rsync_time: data.order_rsync_time,
        }
      },
      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>
