<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="方案名称" prop="programmeName">
        <el-input v-model="formData.programmeName" placeholder="请输入方案名称" maxlength="30" show-word-limit />
      </el-form-item>
      <el-form-item label="优惠定价系数" prop="preferentialPrice">
        <el-input v-model="formData.preferentialPrice" placeholder="请输入优惠定价系数" />
      </el-form-item>
      <el-form-item label="市场价定价系数" prop="marketPrice">
        <el-input v-model="formData.marketPrice" placeholder="请输入市场价定价系数" />
      </el-form-item>
      <el-form-item label="供货价定价系数" prop="supplyPrice">
        <el-input v-model="formData.supplyPrice" placeholder="请输入供货价定价系数" />
      </el-form-item>
      <el-form-item label="渠道价定价系数" prop="channelPrice">
        <el-input v-model="formData.channelPrice" placeholder="请输入渠道价定价系数" />
      </el-form-item>
      <el-form-item label="活动开始时间" prop="startTime">
        <el-date-picker
          v-model="formData.startTime"
          size="medium"
          type="date"
          placeholder="选择活动开始时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="活动结束时间" prop="endTime">
        <el-date-picker
          v-model="formData.endTime"
          size="medium"
          type="date"
          placeholder="选择活动结束时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getShopId } from '@/utils/auth'
import { priceControlGetById, priceControlSavePlan, priceControlUpdatePlan } from '@/api/selectionCenter/selectionCenter'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        priceId: '',
        shopId: getShopId(),
        programmeName: '',
        preferentialPrice: '',
        marketPrice: '',
        supplyPrice: '',
        channelPrice: '',
        startTime: '',
        endTime: '',
        status: '',
        type: '',
        isDefault: '',
        createTime: '',
        updateTime: ''
      },
      formRules: {
        shopId: [
          { required: true, message: '缺少店铺ID' }
        ],
        programmeName: [
          { required: true, message: '请输入方案名称' }
        ],
        preferentialPrice: [
          { required: false, message: '请输入优惠定价系数' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' } // >0的数字, 最多2位小数正则
        ],
        marketPrice: [
          { required: false, message: '请输入市场价定价系数' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        supplyPrice: [
          { required: false, message: '请输入供货价定价系数' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        channelPrice: [
          { required: false, message: '请输入渠道价定价系数' },
          { pattern: /^0\.\d{0,2}$|^[1-9]\d*\.\d{0,2}$|^[1-9]\d*$/, message: '数值有误' }
        ],
        startTime: [
          { required: false, message: '请选择活动开始时间' }
        ],
        endTime: [
          { required: false, message: '请选择活动结束时间' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.priceId ? '编辑定时任务' : '添加定时任务'
      this.visible = true
      this.initList()
      if (params.priceId) {
        this.getInfo(params.priceId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await priceControlGetById({ id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          priceId: res.data.priceId || '',
          shopId: res.data.shopId || getShopId(),
          programmeName: res.data.programmeName || '',
          preferentialPrice: res.data.preferentialPrice || '',
          marketPrice: res.data.marketPrice || '',
          supplyPrice: res.data.supplyPrice || '',
          channelPrice: res.data.channelPrice || '',
          startTime: res.data.startTime || '',
          endTime: res.data.endTime || '',
          status: res.data.status,
          type: res.data.type,
          isDefault: res.data.isDefault,
          createTime: res.data.createTime || '',
          updateTime: res.data.updateTime || ''
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          await this.$validatorForm('formData')
          const loading = this.$loading({ text: '提交中，请稍候……' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            this.formData.priceId ? await priceControlUpdatePlan(params) : await priceControlSavePlan(params)
            loading.close()
            this.$message({ message: `${this.formData.priceId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

