<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item
        :label="__('活动名称')"
        label-width="140px"
        prop="activity_title"
      >
        <el-input
          v-model="form.activity_title"
          clearable
          :placeholder="__('请输入活动名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="__('活动开始时间')"
            label-width="140px"
            prop="activity_starttime"
          >
            <el-date-picker
              v-model="form.activity_starttime"
              :placeholder="__('活动开始时间')"
              :readonly="isReadOnly"
              :style="{ width: '100%' }"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="__('活动结束时间')"
            label-width="140px"
            prop="activity_endtime"
          >
            <el-date-picker
              v-model="form.activity_endtime"
              :placeholder="__('选择日期')"
              :readonly="isReadOnly"
              :style="{ width: '100%' }"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        :label="__('活动简介')"
        label-width="140px"
        prop="activity_intro"
      >
        <el-input
          v-model="form.activity_intro"
          :autosize="{ minRows: 4, maxRows: 4 }"
          :placeholder="__('请输入活动简介')"
          :style="{ width: '100%' }"
          type="textarea"
        />
      </el-form-item>
      <el-form-item
        :label="__('拼团商品')"
        label-width="140px"
        prop="product_name"
      >
        <el-input
          v-model="form.product_name"
          :placeholder="__('请输入拼团商品')"
          :readonly="true"
          @focus="showItemTable"
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item
            :label="__('限购次数')"
            label-width="140px"
            prop="group_buy_limit"
          >
            <el-input
              v-model="form.group_buy_limit"
              clearable
              :placeholder="__('请输入限购次数')"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            :label="__('可优惠价格')"
            label-width="140px"
            prop="group_sale_price"
          >
            <el-input
              v-model="form.group_sale_price"
              clearable
              :placeholder="__('请输入可优惠价格')"
              :style="{ width: '100%' }"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item
        :label="__('团购商品成团数量')"
        label-width="140px"
        prop="group_quantity"
      >
        <el-input
          v-model="form.group_quantity"
          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>
    <ProductItemTable ref="productItemTable" @checkItem="getCheckItem" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/store/activity/base'
  import ProductItemTable from '@/components/activity/ProductBaseDialogTable'
  import { renderTime } from '@/utils/date'
  export default {
    name: 'StoreActivityBaseUpdate',
    components: { ProductItemTable },
    data() {
      return {
        form: {
          activity_starttime: '',
          activity_endtime: '',
          product_name: '',
          activity_type_id: 1126,
        },
        rules: {
          activity_title: [
            {
              required: true,
              message: '请输入活动名称',
              trigger: 'blur',
            },
          ],
          product_name: [
            {
              required: true,
              trigger: 'change',
              message: this.__('请选择商品'),
            },
          ],
          activity_starttime: [
            {
              required: true,
              message: '请输入活动开始时间',
              trigger: 'blur',
            },
          ],
          activity_endtime: [
            {
              required: true,
              message: '请输入活动结束时间',
              trigger: 'blur',
            },
          ],
          activity_intro: [
            {
              required: true,
              message: '请输入活动简介',
              trigger: 'blur',
            },
          ],
          group_buy_limit: [
            {
              required: true,
              message: '请输入限购次数',
              trigger: 'blur',
            },
          ],
          group_sale_price: [
            {
              required: true,
              message: '请输入可优惠价格',
              trigger: 'blur',
            },
          ],
          group_quantity: [
            {
              required: true,
              message: '请输入团购商品成团数量',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    computed: {
      isReadOnly() {
        return this.form.activity_id !== undefined
      },
    },
    created() {},
    methods: {
      getCheckItem(row) {
        this.form.product_id = row.product_id
        this.form.product_name = row.product_name
      },
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = {
            activity_type_id: 1126,
            activity_id: row.activity_id,
            activity_starttime: renderTime(row.activity_starttime),
            activity_endtime: renderTime(row.activity_endtime),
            activity_title: row.activity_title,
            product_name: row.activity_rule.product_name,
            product_id: row.activity_rule.product_id,
            activity_intro: row.activity_rule.activity_intro,
            group_buy_limit: row.activity_rule.group_buy_limit,
            group_sale_price: row.activity_rule.group_sale_price,
            group_quantity: row.activity_rule.group_quantity,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          activity_starttime: '',
          activity_endtime: '',
          item_name: '',
          activity_type_id: 1126,
        }
        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
          }
        })
      },
      showItemTable() {
        this.$refs['productItemTable'].showTable()
      },
    },
  }
</script>
