<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="50%"
    @close="close"
  >
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item :label="__('活动名称')" prop="activity_title">
        <el-input
          v-model="form.activity_title"
          :placeholder="__('活动名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item :label="__('活动开始时间')" 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>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple"></div>
          <el-form-item :label="__('活动结束时间')" 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="__('砍价商品')" prop="product_item_name">
        <el-input
          v-model="form.product_item_name"
          clearable
          :placeholder="__('砍价商品')"
          readonly
          @focus="showItemTable"
        />
      </el-form-item>
      <el-row :gutter="20">
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-form-item :label="__('砍价有效期')" prop="cut_hour">
              <el-input v-model="form.cut_hour" placeholder="请输入有效期（小时）"  clearable />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple"></div>
          <el-form-item :label="__('活动商品总数')" prop="product_count">
            <el-input v-model="form.product_count" placeholder="请输入参与活动商品总数"  clearable />
          </el-form-item>
        </el-col>
      </el-row>


      <el-form-item :label="__('砍价底价')" prop="cut_down_min_limit_price">
        <el-input
          v-model="form.cut_down_min_limit_price"
          oninput="form.cut_down_min_limit_price.replace(/[^0-9.]/g,'')"
          :placeholder="__('砍价底价')"
          type="number"
        />
      </el-form-item>
      <el-form-item :label="__('砍价方式')" prop="cut_down_type">
        <el-radio v-model="form.cut_down_type" :label="1">
          {{ __('固定价格') }}
        </el-radio>
        <el-radio v-model="form.cut_down_type" :label="2">
          {{ __('价格范围') }}
        </el-radio>
      </el-form-item>
      <el-form-item
        v-if="form.cut_down_type === 1"
        :label="__('固定价格')"
        prop="cut_down_fixed_price"
      >
        <el-input
          v-model="form.cut_down_fixed_price"
          oninput="form.cut_down_fixed_price.replace(/[^0-9.]/g,'')"
          :placeholder="__('固定价格')"
          :style="{ width: '100%' }"
          type="number"
        />
      </el-form-item>
      <el-form-item
        v-if="form.cut_down_type === 2"
        :label="__('砍价人数')"
        prop="cut_down_fixed_price"
      >
        <el-input-number
          v-model="form.cut_down_user_num"
          :label="__('砍价人数')"
          :max="10000000"
          :min="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>
    <ProductItemTable ref="productItemTable" @checkItem="getCheckItem" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import ProductItemTable from '@/components/activity/ProductItemDialogTable'
  import { doEdit, Update } from '@/api/store/activity/base'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreActivityBaseEdit',
    components: { ProductItemTable },
    data() {
      return {
        form: {
          product_item_name: '',
          item_id: '',
          cut_down_type: 1,
          cut_down_user_num: 0,
          cut_down_fixed_price: '',
          activity_type_id: 1124,
          activity_starttime: '',
          activity_endtime: '',
          cut_hour:'',
          product_count:''
        },
        title: '',
        dialogFormVisible: false,
        mounted() {
          this.$forceUpdate()
        },
        rules: {
          activity_title: [
            { required: true, trigger: 'blur', message: this.__('活动标题') },
          ],
          // cut_down_fixed_price: [
          //   {required: true, trigger: 'blur', message: this.__('请输入固定价格')},
          // ],
          cut_down_user_num: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入砍价人数'),
            },
          ],
          cut_down_min_limit_price: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输入砍价底价'),
            },
          ],
          product_item_name: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('请输选择砍价商品'),
            },
          ],
          activity_starttime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动开始时间'),
            },
          ],
          activity_endtime: [
            {
              required: true,
              trigger: 'blur',
              message: this.__('活动结束时间'),
            },
          ],
          cut_hour: [
            { required: true, trigger: 'blur', message: this.__(' 砍价有效期') },
          ],
          product_count: [
            { required: true, trigger: 'blur', message: this.__('参与活动商品总数') },
          ],
        },
      }
    },
    computed: {
      isReadOnly() {
        return this.form.activity_id !== undefined
      },
    },
    created() {},
    methods: {
      __,
      getCheckItem(row) {
        this.form.item_id = row.item_id
        this.form.product_item_name = row.product_item_name
        this.$refs['form'].validateField(
          ['product_item_name', 'item_id'],
          (errorMsg) => {
            if (!errorMsg) {
              //执行校验成功的相关操作
            }
          }
        )
      },
      showEdit(row) {
        if (!row) {
          this.title = this.__('新增')
        } else {
          this.title = this.__('编辑')
          this.form = {
            activity_id: row.activity_id,
            activity_title: row.activity_title,
            activity_type_id: 1124,
            activity_starttime: renderTime(row.activity_starttime),
            activity_endtime: renderTime(row.activity_endtime),
            item_id: row.activity_rule.item_id,
            product_item_name: row.activity_rule.product_item_name,
            cut_down_fixed_price: row.activity_rule.cut_down_fixed_price,
            cut_down_min_limit_price:
              row.activity_rule.cut_down_min_limit_price,
            cut_down_type: row.activity_rule.cut_down_type,
            cut_down_user_num: row.activity_rule.cut_down_user_num,
            cut_hour:row.cut_hour,
            product_count:row.product_count
          }
        }

        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          product_item_name: '',
          item_id: '',
          cut_down_type: 1,
          cut_down_fixed_price: '',
          activity_type_id: 1124,
          activity_starttime: '',
          cut_down_user_num: '',
          activity_endtime: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      showItemTable() {
        this.$refs['productItemTable'].showTable()
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.activity_id && this.form.activity_id != undefined) {
              const { msg, status } = await Update(this.form)
              if (200 == status) {
                this.$baseMessage(msg, 'success')
              } else {
                this.$baseMessage(msg, 'error')
              }
              this.$emit('fetch-data')
              this.close()
            } else {
              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>
