<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="30%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item :label="__('发货日期')" prop="logistics_time">
        <el-date-picker
          v-model="form.logistics_time"
          format="yyyy-MM-dd HH:mm:ss"
          :placeholder="__('选择发货日期')"
          :style="{ width: '100%' }"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item :label="__('物流公司')" prop="logistics_id">
        <el-select
          v-model.trim="form.logistics_id"
          clearable
          :placeholder="__('请选择物流公司')"
          :style="{ width: '100%' }"
          @change="$forceUpdate()"
        >
          <el-option
            v-for="(item, index) in expressList"
            :key="index"
            :label="item.logistics_name"
            :value="item.logistics_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('发货详细地址')" prop="ss_id">
        <el-select
          v-model="form.ss_id"
          clearable
          :placeholder="__('请选择发货详细地址')"
          :style="{ width: '100%' }"
          @change="$forceUpdate()"
        >
          <el-option
            v-for="item in addressList"
            :key="item.ss_id"
            :label="item.ss_address"
            :value="item.ss_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="__('物流单号')" prop="order_tracking_number">
        <el-input
          v-model="form.order_tracking_number"
          clearable
          :placeholder="__('请输入物流单号')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('物流备注')" prop="logistics_explain">
        <el-input
          v-model="form.logistics_explain"
          clearable
          :placeholder="__('请输入物流备注')"
          :style="{ width: '100%' }"
        />
      </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 { getAddressList } from '@/api/store/shipping/address'
  import { getExpressList } from '@/api/store/express/logistics'
  import { saveOrderLogistics } from '@/api/order/logistics'
  import { renderTime } from '@/utils/date'
  import logistics from '@/views/settings/config/track/logistics'

  export default {
    name: 'ExpressEdit',
    data() {
      return {
        form: {
          logistics_time: '',
          ss_id: undefined,
          order_logistics_id: undefined,
        },
        rules: {
          logistics_time: [
            {
              required: true,
              message: '请输入单行文本发货日期',
              trigger: 'blur',
            },
          ],
          logistics_id: [
            {
              required: true,
              message: '请输入物流公司',
              trigger: 'blur',
            },
          ],
          ss_id: [
            {
              required: true,
              message: '请输入发货地址',
              trigger: 'blur',
            },
          ],
          order_tracking_number: [
            {
              required: true,
              message: '请输入物流单号',
              trigger: 'blur',
            },
          ],
        },
        title: this.__('发货确认'),
        dialogFormVisible: false,
        addressList: [],
        expressList: [],
      }
    },
    created() {},
    mounted() {
      this.getAddressList()
      this.getExpress()
    },
    methods: {
      nhao(row) {
        console.log(row)
      },
      __,
      showEdit(row) {
        if (row.logistics_id) {
          this.form = {
            order_id: row.order_id,
            stock_bill_id: row.stock_bill_id,
            logistics_id: row.logistics_id,
            logistics_explain: row.logistics_explain,
            order_tracking_number: row.order_tracking_number,
            logistics_time: renderTime(row.logistics_time),
          }
        } else {
          this.form.order_id = row.order_id
          this.form.stock_bill_id = row.stock_bill_id
          this.dispose()
        }
        ;(this.form.order_logistics_id = row.order_logistics_id),
          (this.dialogFormVisible = true)
      },
      dispose() {
        let ss_is_default = this.addressList.filter(
          (s) => 1 === s.ss_is_default
        )
        if (ss_is_default.length > 0) {
          this.form.ss_id = ss_is_default[0].ss_id
        }
        let logistics_is_default = this.expressList.filter(
          (s) => 1 === s.logistics_is_default
        )
        if (logistics_is_default.length > 0) {
          this.form.logistics_id = logistics_is_default[0].logistics_id
        }
      },
      close() {
        this.form = { logistics_time: '', ss_id: undefined }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await saveOrderLogistics(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      async getAddressList() {
        const { data } = await getAddressList()
        if (data && data.length > 0) {
          this.addressList = data
        }
      },
      async getExpress() {
        const { data } = await getExpressList()
        if (data && data.length > 0) {
          this.expressList = data
        }
      },
    },
  }
</script>
