<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible" width="50%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" :size="ELsize"
      label-width="80px">
      <el-form-item label="模板名称" prop="name">
        <el-input placeholder="输入模板名称" v-model="dataForm.name" maxlength="30" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="模板类型" prop="freePost">
        <el-radio-group v-model="dataForm.freePost" @change="freePostChange">
          <el-radio :label="false">买家承担运费</el-radio>
          <el-radio :label="true">卖家包邮</el-radio>
        </el-radio-group>
      </el-form-item>
      <!-- <template v-if="dataForm.freePost">
        <el-form-item label="免邮门槛金额" prop="freePostThreshold" label-width="120px">
          <el-input-number v-model="dataForm.freePostThreshold" :min="0" label="免邮门槛金额"></el-input-number>
        </el-form-item>
        <el-form-item label="免邮运费" prop="freePostFreight">
          <el-input-number v-model="dataForm.freePostFreight" :min="0" label="免邮运费"></el-input-number>
        </el-form-item>
      </template> -->
      <el-form-item label="计价方式" prop="freightComputeMode" v-if="dataForm.freePost == false">
        <el-radio-group v-model="dataForm.freightComputeMode">
          <el-radio :label="0">按件数</el-radio>
          <el-radio :label="1">按重量</el-radio>
          <el-radio :label="2">按体积</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="默认模板">
        <el-switch v-model="dataForm.general">
        </el-switch>
        <span>设为新商品默认模板（新添加的商品支持商家配送时的默认模板）</span>
      </el-form-item>
      <table style="width: 100%" v-if="dataForm.freePost == false && dataForm.freightComputeMode != null">
        <tr>
          <th style="width: 20%;">可配送区域</th>
          <th v-if="dataForm.freightComputeMode == 0">首件数（个）</th>
          <th v-if="dataForm.freightComputeMode == 1">首重量（Kg）</th>
          <th v-if="dataForm.freightComputeMode == 2">首体积 （M3）</th>
          <th>运费（元）</th>
          <th v-if="dataForm.freightComputeMode == 0">续件（个）</th>
          <th v-if="dataForm.freightComputeMode == 1">续重量（Kg）</th>
          <th v-if="dataForm.freightComputeMode == 2">续体积（M3）</th>
          <th>续费（元）</th>
        </tr>
        <tr>
          <th>默认区域</th>
          <th v-if="dataForm.freightComputeMode != null">
            <el-form-item prop="defaultRule.startStandards">
              <el-input v-model="dataForm.defaultRule.startStandards"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item prop="defaultRule.startFreight">
              <el-input v-model="dataForm.defaultRule.startFreight"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item prop="defaultRule.continued">
              <el-input v-model="dataForm.defaultRule.continued"></el-input>
            </el-form-item>
          </th>
          <th>
            <el-form-item prop="defaultRule.continuedFreight">
              <el-input v-model="dataForm.defaultRule.continuedFreight"></el-input>
            </el-form-item>
          </th>
        </tr>
      </table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :size="ELsize" @click="visible = false">取消</el-button>
      <el-button :size="ELsize" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { freightTemplateAdd, freightTemplateInfo, freightTemplateUpdate } from '@/api/operating'
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        name: '',
        freePost: null, // 是否免邮
        general: false, // 是否是默认模板
        freightComputeMode: 0, // 运费计算方式(0件，1重,2体积)
        defaultRule: {
          startStandards: null, // 首件，首重，首体积初始门槛（首件，首重，首体积(运费)）
          startFreight: null, // 初始运费（首件，首重，首体积(运费)）
          continued: null, // 续加多少量
          continuedFreight: null // 续加的运费
        }
      },
      dataRule: {
        name: [{
          required: true,
          message: '模板名称不能为空',
          trigger: 'blur'
        }],
        freePost: [{
          required: true,
          message: '请选择是否免邮',
          trigger: 'blur'
        }],
        general: [{
          required: true,
          message: '请选择是否是默认模板',
          trigger: 'blur'
        }],
        'defaultRule.startStandards': [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        }],
        'defaultRule.startFreight': [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        }],
        'defaultRule.continued': [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        }],
        'defaultRule.continuedFreight': [{
          required: true,
          message: '请输入',
          trigger: 'blur'
        }],
        // freePostThreshold: [{
        //   required: true,
        //   message: '请输入免邮门槛金额',
        //   trigger: 'blur'
        // }],
        // freePostFreight: [{
        //   required: true,
        //   message: '请输入免邮运费',
        //   trigger: 'blur'
        // }],
      }
    }
  },

  methods: {
    init(id) {
      this.reset()

      this.dataForm.id = id || null
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (id) {
        // 存在id 查询详情
        freightTemplateInfo(id).then(res => {
          if (res && res.code == 200) {
            this.dataForm = res.data
          }
        })
      }
    },
    freePostChange(e) {
      if (e == false) {
        this.dataForm.freightComputeMode = 0
      }
      // else {
      //   this.dataForm.freePostThreshold = undefined;
      //   this.dataForm.freePostFreight = undefined;
      // }
    },
    // 提交
    dataFormSubmit() {
      // if (!this.dataForm.name) {
      //   this.$message.error('请输入模版名称！');
      //   return
      // }
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          if (this.dataForm.freePost) {
            this.dataForm.freightComputeMode = 0
            this.dataForm.defaultRule.startStandards = 0
            this.dataForm.defaultRule.startFreight = 0
            this.dataForm.defaultRule.continued = 0
            this.dataForm.defaultRule.continuedFreight = 0;
            // if (this.dataForm.freePostThreshold === undefined) {
            //   this.$message.error('请输入免邮门槛金额！');
            //   return
            // } else if (this.dataForm.freePostFreight === undefined) {
            //   this.$message.error('请输入免邮运费！');
            //   return
            // }
          }
          if (!this.dataForm.id) {
            delete this.dataForm.id
            // this.dataForm.businessId = 18;
            freightTemplateAdd(this.dataForm).then(res => {
              if (res && res.code == 200) {
                this.$message({
                  message: '添加成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('result')
              }
            })
          } else {
            freightTemplateUpdate(this.dataForm).then(res => {
              if (res && res.code == 200) {
                this.$message({
                  message: '修改成功',
                  type: 'success'
                })
                this.visible = false
                this.$emit('result')
              }
            })
          }
        }
      })
    },

    reset() {
      this.dataForm = {
        name: '',
        freePost: null, // 是否免邮
        general: false, // 是否是默认模板
        freightComputeMode: null, // 运费计算方式(0件，1重,2体积)
        defaultRule: {
          startStandards: null, // 首件，首重，首体积初始门槛（首件，首重，首体积(运费)）
          startFreight: null, // 初始运费（首件，首重，首体积(运费)）
          continued: null, // 续加多少量
          continuedFreight: null // 续加的运费
        }
      }
    }

  }
}
</script>

<style lang="scss" scoped>
table {
  border-collapse: collapse;
  border-spacing: 0;
  border-left: 1px solid #cccccc;
  border-top: 1px solid #cccccc;
}

th {
  border-right: 1px solid #cccccc;
  border-bottom: 1px solid #cccccc;
  padding: 5px 15px;
}

th .el-form-item {
  margin-bottom: 0px;
}

th {
  ::v-deep .el-form-item__content {
    margin-left: 0px !important;
  }
}

::v-deep .el-form-item__error {
  font-weight: 400;
}
</style>
