<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('物流名称')" prop="logistics_name">
        <el-input v-model="form.logistics_name" :placeholder="__('物流名称')" />
      </el-form-item>

      <el-form-item :label="__('快递公司')" prop="express_id">
        <el-select
          v-model="form.express_id"
          class="filter-item"
          clearable
          :placeholder="__('快递公司')"
        >
          <el-option
            v-for="item in dataList"
            :key="item"
            :label="item.express_name"
            :value="item.express_id"
          />
        </el-select>
      </el-form-item>

      <el-form-item :label="__('联系手机')" prop="logistics_mobile">
        <el-input
          v-model="form.logistics_mobile"
          :placeholder="__('联系手机')"
        />
      </el-form-item>
      <el-form-item :label="__('联系人')" prop="logistics_contacter">
        <el-input
          v-model="form.logistics_contacter"
          :placeholder="__('联系人')"
        />
      </el-form-item>

      <el-form-item :label="__('联系地址')" prop="logistics_address">
        <el-input
          v-model="form.logistics_address"
          :placeholder="__('联系地址')"
        />
      </el-form-item>

      <el-form-item :label="__('是否开启')" prop="logistics_is_default">
        <el-switch
          v-model="form.logistics_is_enable"

          :active-value="1"

          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item :label="__('是否默认')" prop="logistics_is_default">
        <el-switch
          v-model="form.logistics_is_default"

          :active-value="1"

          :inactive-value="0"
        />
      </el-form-item>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getExpressList } from '@/api/base/express'
  import { doEdit } from '@/api/store/express/logistics'

  export default {
    name: 'StoreExpressLogisticsEdit',
    data() {
      return {
        dataList: [],
        value: '',
        form: {
          logistics_is_default: 1,
          logistics_is_enable: 1,
        },
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {
      this.functionExpress()
    },
    mounted() {
      this.rules = {
        store_id: [
          { required: true, trigger: 'blur', message: this.__('店铺') },
        ],

        logistics_name: [
          { required: true, trigger: 'blur', message: this.__('物流名称') },
        ],

        express_name: [
          { required: true, trigger: 'blur', message: this.__('快递公司') },
        ],

        logistics_mobile: [
          { required: true, trigger: 'blur', message: this.__('联系手机') },
        ],
        logistics_contacter: [
          { required: true, trigger: 'blur', message: this.__('联系人') },
        ],

        logistics_address: [
          { required: true, trigger: 'blur', message: this.__('联系地址') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      async functionExpress() {
        // 发请求获取渠道下拉框的值
        await getExpressList()
          .then((res) => {
            this.dataList = res.data // 把获取到的数据赋给this.dataList
          })
          .catch((err) => {})
      },
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          logistics_is_default: 1,
          logistics_is_enable: 1,
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')

              this.$emit('fetch-data')
              this.close()
            } else {
              this.$baseMessage(msg, 'error')
            }
          } else {
            return false
          }
        })
      },
    },
  }
</script>
