<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="stock_bill_id">
        <el-input v-model="form.stock_bill_id" :placeholder="__('购货')" />
      </el-form-item>
      <el-form-item :label="__('购货')" prop="stock_bill_number">
        <el-input v-model="form.stock_bill_number" :placeholder="__('购货')" />
      </el-form-item>
      <el-form-item :label="__('是否审核')" prop="stock_bill_checked">
        <el-input
          v-model="form.stock_bill_checked"
          :placeholder="__('是否审核')"
        />
      </el-form-item>
      <el-form-item :label="__('单据日期')" prop="stock_bill_date">
        <el-input
          v-model="form.stock_bill_date"
          :placeholder="__('单据日期')"
        />
      </el-form-item>
      <el-form-item :label="__('更新时间')" prop="stock_bill_modify_time">
        <el-input
          v-model="form.stock_bill_modify_time"
          :placeholder="__('更新时间')"
        />
      </el-form-item>
      <el-form-item :label="__('创建时间')" prop="stock_bill_time">
        <el-input
          v-model="form.stock_bill_time"
          :placeholder="__('创建时间')"
        />
      </el-form-item>
      <el-form-item
        :label="__('业务类别purchase_type_id, sale_type_id')"
        prop="bill_type_id"
      >
        <el-input
          v-model="form.bill_type_id"
          :placeholder="__('业务类别purchase_type_id, sale_type_id')"
        />
      </el-form-item>
      <el-form-item :label="__('库存类型')" prop="stock_transport_type_id">
        <el-input
          v-model="form.stock_transport_type_id"
          :placeholder="__('库存类型')"
        />
      </el-form-item>
      <el-form-item :label="__('所属店铺')" prop="store_id">
        <el-input v-model="form.store_id" :placeholder="__('所属店铺')" />
      </el-form-item>
      <el-form-item :label="__('所属仓库')" prop="warehouse_id">
        <el-input v-model="form.warehouse_id" :placeholder="__('所属仓库')" />
      </el-form-item>
      <el-form-item :label="__('源单号码')" prop="order_id">
        <el-input v-model="form.order_id" :placeholder="__('源单号码')" />
      </el-form-item>
      <el-form-item :label="__('备注')" prop="stock_bill_remark">
        <el-input v-model="form.stock_bill_remark" :placeholder="__('备注')" />
      </el-form-item>
      <el-form-item :label="__('经办人')" prop="employee_id">
        <el-input v-model="form.employee_id" :placeholder="__('经办人')" />
      </el-form-item>
      <el-form-item :label="__('制单人')" prop="admin_id">
        <el-input v-model="form.admin_id" :placeholder="__('制单人')" />
      </el-form-item>
      <el-form-item :label="__('其它金额')" prop="stock_bill_other_money">
        <el-input
          v-model="form.stock_bill_other_money"
          :placeholder="__('其它金额')"
        />
      </el-form-item>
      <el-form-item :label="__('订单金额')" prop="stock_bill_amount">
        <el-input
          v-model="form.stock_bill_amount"
          :placeholder="__('订单金额')"
        />
      </el-form-item>
      <el-form-item :label="__('是否有效')" prop="stock_bill_enable">
        <el-input
          v-model="form.stock_bill_enable"
          :placeholder="__('是否有效')"
        />
      </el-form-item>
      <el-form-item :label="__('关联编号')" prop="stock_bill_src_id">
        <el-input
          v-model="form.stock_bill_src_id"
          :placeholder="__('关联编号')"
        />
      </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 { doEdit } from '@/api/stock/bill'

  export default {
    name: 'StockBillEdit',
    data() {
      return {
        form: {},
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.rules = {
        stock_bill_id: [
          { required: true, trigger: 'blur', message: this.__('购货') },
        ],
        stock_bill_number: [
          { required: true, trigger: 'blur', message: this.__('购货') },
        ],
        stock_bill_checked: [
          { required: true, trigger: 'blur', message: this.__('是否审核') },
        ],
        stock_bill_date: [
          { required: true, trigger: 'blur', message: this.__('单据日期') },
        ],
        stock_bill_modify_time: [
          { required: true, trigger: 'blur', message: this.__('更新时间') },
        ],
        stock_bill_time: [
          { required: true, trigger: 'blur', message: this.__('创建时间') },
        ],
        bill_type_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('业务类别purchase_type_id, sale_type_id'),
          },
        ],
        stock_transport_type_id: [
          { required: true, trigger: 'blur', message: this.__('库存类型') },
        ],
        store_id: [
          { required: true, trigger: 'blur', message: this.__('所属店铺') },
        ],
        warehouse_id: [
          { required: true, trigger: 'blur', message: this.__('所属仓库') },
        ],
        order_id: [
          { required: true, trigger: 'blur', message: this.__('源单号码') },
        ],
        stock_bill_remark: [
          { required: true, trigger: 'blur', message: this.__('备注') },
        ],
        employee_id: [
          { required: true, trigger: 'blur', message: this.__('经办人') },
        ],
        admin_id: [
          { required: true, trigger: 'blur', message: this.__('制单人') },
        ],
        stock_bill_other_money: [
          { required: true, trigger: 'blur', message: this.__('其它金额') },
        ],
        stock_bill_amount: [
          { required: true, trigger: 'blur', message: this.__('订单金额') },
        ],
        stock_bill_enable: [
          { required: true, trigger: 'blur', message: this.__('是否有效') },
        ],
        stock_bill_src_id: [
          { required: true, trigger: 'blur', message: this.__('关联编号') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        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')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
