                <template>
                  <div class="app-container calendar-list-container">
                    <el-form :rules="rules" ref="addForm" :model="updatepolinfo" label-position="left" label-width="80px"
                              style='width: 1000px;margin-left:20px'>
                            <el-row :gutter="16" style="margin-bottom: 15px;">
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('promotion.name')" prop="po_name" style="width: 310px;">
                                    <div class="block">
                                  <el-input v-model="updatepolinfo.po_name"> </el-input>
                                    </div>
                                  </el-form-item>
                                </div>
                              </el-col>
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('promotion.desc')" prop="cb_name" style="width: 310px;">
                                    <div class="block">
                                  <el-input v-model="updatepolinfo.po_desc"> </el-input>
                                    </div>
                                  </el-form-item>
                                </div>
                              </el-col>
                            </el-row>

                            <el-row :gutter="16" style="margin-bottom: 15px;">
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('adminstock.startdate')" prop="po_startdate" style="width: 310px;">
                                    <div class="block">
                                  <el-date-picker v-model="updatepolinfo.po_startdate" value-format="yyyy-MM-dd" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1" style="width: 230px;">
                            </el-date-picker>
                                    </div>
                                  </el-form-item>
                                </div>
                              </el-col>
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('adminstock.enddate')" prop="po_enddate" style="width: 310px;">
                                    <div class="block">
                                  <el-date-picker v-model="updatepolinfo.po_enddate" value-format="yyyy-MM-dd" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1" style="width: 230px;">
                            </el-date-picker>
                                    </div>
                                  </el-form-item>
                                </div>
                              </el-col>
                            </el-row>


                            <el-row :gutter="16" style="margin-bottom: 15px;">
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('promotion.mkt')" prop="po_mkt" style="width: 310px;">
                                    <div class="block">
                                    <el-select  v-if="mktEnable" disabled class="filter-item" v-model="updatepolinfo.po_mkt" default-first-optionplaceholder="请选择城市" style="width: 230px;">
                                      <el-option v-for="item in branchesOptions" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                                      </el-option>
                                    </el-select>
                                    <el-select  v-else class="filter-item" v-model="updatepolinfo.po_mkt" default-first-optionplaceholder="请选择城市" style="width: 230px;">
                                      <el-option v-for="item in branchesOptions" :key="item.branche_code" :label="item.branche_name" :value="item.branche_code">
                                      </el-option>
                                    </el-select>
                                    </div>
                                  </el-form-item>
                                </div>
                              </el-col>
                            </el-row>

                            <el-row :gutter="16" style="margin-bottom: 15px;">
                              <el-col :span="8">
                                <div class="grid-content bg-purple" style="width: 300px;">
                                  <el-form-item  :label="$t('promotion.headimg')" style="width: 710px;" >
                                <div class="block" style="width: 510px;">
                                <el-upload
                                  :action="url"
                                  list-type="picture-card"
                                  :on-preview="handlePictureCardPreview"
                                  :file-list="fileListvoucher"
                                  :on-success="uploadSuccess"
                                  :before-upload="beforeUpdate"
                                  :limit=1
                                  :on-remove="handleRemovevoucher">
                                  <i class="el-icon-plus"></i>
                                </el-upload>
                                <el-dialog :visible.sync="dialogVisible">
                                  <img width="100%" :src="dialogImageUrl" alt="">
                                </el-dialog>
                                </div>
                                </el-form-item>
                                </div>
                              </el-col>
                            </el-row>
                            
                            <el-row :gutter="16" style="margin-bottom: 15px;">
                            <el-col :span="6"><div>&nbsp;</div></el-col>
                            <el-col :span="12">
                              <div >
                                <el-button  type="primary" @click="updatePol">{{$t('table.confirm')}}</el-button>
                                <el-button @click="handleref()">{{$t('navbar.reset')}}</el-button>
                              </div>
                            </el-col>
                            <el-col :span="6"><div>&nbsp;</div></el-col>
                          </el-row>
                        </el-form>
                    <el-dialog title="Reading statistics" :visible.sync="dialogPvVisible">
                      <el-table :data="pvData" border fit highlight-current-row style="width: 100%">
                        <el-table-column prop="key" label="Channel"></el-table-column>
                        <el-table-column prop="pv" label="Pv"></el-table-column>
                      </el-table>
                      <span slot="footer" class="dialog-footer">
                        <el-button type="primary" @click="dialogPvVisible = false">{{$t('table.confirm')}}</el-button>
                      </span>
                    </el-dialog>
                  </div>
                </template>
                <script>
                  import { getBranches, createArticle } from '@/api/article'
                  import { getOnePromotionOfflines, updatePromotionOffline, deletePromotionImg } from '@/api/promotion'
                  import waves from '@/directive/waves' // 水波纹指令
                  import { parseTime } from '@/utils'
                  import config from '@/utils/config'
                  // arr to obj ,such as { CN : "China", US : "USA" }
                  export default {
                    name: 'promotionofflineedit',
                    directives: {
                      waves
                    },
                    data() {
                      return {
                        userMkt: null,
                        mktEnable: null,
                        url: config.BASE_URL + '/uploadPromotionImg',
                        dialogVisible: false,
                        dialogImageUrl: undefined,
                        tableKey: 0,
                        list: null,
                        total: null,
                        page: 1,
                        limit: 10,
                        queryType: 'list',
                        listLoading: true,
                        polQuery: {
                          id: undefined
                        },
                        removePhoto: {
                          cv_filename: null
                        },
                        checkCouponCreate: 0,
                        checkCouponbatch: {
                          id: null
                        },
                        updatepolinfo: {
                          id: undefined,
                          po_name: undefined,
                          po_desc: undefined,
                          po_headimg: null,
                          po_icon: undefined,
                          po_startdate: undefined,
                          po_enddate: undefined,
                          po_mkt: undefined,
                          po_createtime: undefined,
                          po_createuser: undefined
                        },
                        updatepolinfoconf: {
                          po_name: undefined,
                          po_desc: undefined,
                          po_headimg: null,
                          po_icon: undefined,
                          po_startdate: undefined,
                          po_enddate: undefined,
                          po_mkt: undefined,
                          po_createtime: undefined,
                          po_createuser: undefined
                        },
                        sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
                        branchesOptions: [],
                        fileListvoucher: [],
                        citysOptions: [],
                        img: {
                          name: null,
                          url: null
                        },
                        packageoptions: [],
                        showReviewer: false,
                        ethnicgroupptions: '',
                        provinceoptions: '',
                        dialogFormVisible: false,
                        checkidcard: {
                          idcard: undefined
                        },
                        dialogStatus: '',
                        dialogPvVisible: false,
                        pvData: [],
                        updateStatus: {
                          id: undefined,
                          status: undefined
                        },
                        pickerOptions1: {
                          shortcuts: [{
                            text: '今天',
                            onClick(picker) {
                              picker.$emit('pick', new Date())
                            }
                          }, {
                            text: '昨天',
                            onClick(picker) {
                              const date = new Date()
                              date.setTime(date.getTime() - 3600 * 1000 * 24)
                              picker.$emit('pick', date)
                            }
                          }, {
                            text: '一周前',
                            onClick(picker) {
                              const date = new Date()
                              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
                              picker.$emit('pick', date)
                            }
                          }]
                        },
                        rules: {
                          po_name: [{ required: true, min: 1, max: 20, message: '请正确录入活动名称', trigger: 'blur' }],
                          po_startdate: [{ required: true, message: '请正确录入活动开始时间', trigger: 'blur' }],
                          po_enddate: [{ required: true, message: '请正确录入活动结束时间', trigger: 'blur' }],
                          po_mkt: [{ required: true, message: '请正确选择活动门店', trigger: 'blur' }]
                        },
                        downloadLoading: false
                      }
                    },
                    created() {
                      this.userMkt = this.$store.getters.mkt
                      this.getList()
                      getBranches().then(response5 => {
                        this.branchesOptions = response5.data
                        this.citysOptions = response5.data
                      })
                    },
                    methods: {
                      getList() {
                        this.updatepolinfo.id = this.$route.params.promotionofflineid
                        this.polQuery.id = this.$route.params.promotionofflineid
                        getOnePromotionOfflines(this.polQuery).then(response => {
                          this.updatepolinfoconf.po_name = response.data.po_name
                          this.updatepolinfoconf.po_desc = response.data.po_desc
                          this.updatepolinfoconf.po_headimg = response.data.po_headimg
                          this.updatepolinfoconf.po_startdate = response.data.po_startdate
                          this.updatepolinfoconf.po_enddate = response.data.po_enddate
                          this.updatepolinfoconf.po_mkt = response.data.po_mkt
                          this.updatepolinfo.po_name = response.data.po_name
                          this.updatepolinfo.po_desc = response.data.po_desc
                          this.updatepolinfo.po_headimg = response.data.po_headimg
                          this.updatepolinfo.po_startdate = response.data.po_startdate
                          this.updatepolinfo.po_enddate = response.data.po_enddate
                          this.updatepolinfo.po_mkt = response.data.po_mkt
                          this.img.name = 'ttt'
                          this.img.url = response.data.po_headimg
                          this.fileListvoucher.push(this.img)
                          const aa = response.data.po_headimg.split('/')
                          const aatemp = aa[aa.length - 1]
                          this.removePhoto.cv_filename = aatemp
                        })
                        if (this.userMkt !== 'ALL') {
                          this.updatepolinfo.po_mkt = this.userMkt
                          this.mktEnable = true
                        }
                      },
                      updatePol() {
                        this.$refs['addForm'].validate((valid) => {
                          if (valid) {
                            if (this.updatepolinfo.po_headimg !== null) {
                              const aa = this.updatepolinfo.po_headimg.split('/')
                              const aatemp = aa[aa.length - 1]
                              if (aatemp !== this.removePhoto.cv_filename) {
                                deletePromotionImg(this.removePhoto).then(response9 => {
                                })
                              }
                              const date1 = Date.parse(this.updatepolinfo.po_startdate)
                              const date2 = Date.parse(this.updatepolinfo.po_enddate)
                              const diffDays = date2 - date1
                              if (diffDays < 0) {
                                this.$message({
                                  message: '请正确录入活动日期',
                                  type: 'warning'
                                })
                              } else {
                                const aa = this.updatepolinfo.po_headimg.split('/')
                                const aatemp = aa[aa.length - 1]
                                this.updatepolinfo.po_headimg = aatemp
                                this.updatepolinfo.id = this.$route.params.promotionofflineid
                                updatePromotionOffline(this.updatepolinfo).then(response => {
                                  if (response.data !== 1) {
                                    createArticle(this.temp).then(() => {
                                      this.$notify({
                                        title: '失败',
                                        message: '更新失败',
                                        type: 'warning',
                                        duration: 2000
                                      })
                                    })
                                  } else {
                                    createArticle(this.temp).then(() => {
                                      this.$notify({
                                        title: '成功',
                                        message: '更新成功',
                                        type: 'success',
                                        duration: 2000
                                      })
                                    })
                                  }
                                })
                              }
                            } else {
                              this.$message({
                                message: '请上传活动图片！',
                                type: 'warning'
                              })
                            }
                          }
                        })
                      },
                      handleref() {
                        this.updatepolinfo.po_name = this.updatepolinfoconf.po_name
                        this.updatepolinfo.po_desc = this.updatepolinfoconf.po_desc
                        this.updatepolinfo.po_icon = this.updatepolinfoconf.po_icon
                        this.updatepolinfo.po_startdate = this.updatepolinfoconf.po_startdate
                        this.updatepolinfo.po_enddate = this.updatepolinfoconf.po_enddate
                        this.updatepolinfo.po_mkt = this.updatepolinfoconf.po_mkt
                        if (this.userMkt !== 'ALL') {
                          this.updatepolinfo.po_mkt = this.userMkt
                        }
                      },
                      handlePictureCardPreview(file) {
                        this.dialogImageUrl = file.url
                        this.dialogVisible = true
                      },
                      beforeUpdate(file) {
                        if (file.size > 1000000) {
                          this.$message.error('上传照片大于1M！')
                          return false
                        }
                      },
                      uploadSuccess(response, file, fileList) {
                        const aa = response.split('/')
                        const aatemp = aa[aa.length - 1]
                        this.updatepolinfo.po_headimg = aatemp
                      },
                      handleRemovevoucher(file, fileList) {
                        this.updatepolinfo.po_headimg = null
                      },
                      formatJson(filterVal, jsonData) {
                        return jsonData.map(v => filterVal.map(j => {
                          if (j === 'timestamp') {
                            return parseTime(v[j])
                          } else {
                            return v[j]
                          }
                        }))
                      }
                    }
                  }
                </script>
                <style>
                .el-form--label-left .el-form-item__label{
                  text-align:right
                }
                .el-rate{
                  line-height: 0;
                  margin-top:8px;
                }
                .el-checkbox.el-checkbox {
                    margin-left: 30px;
                }
                </style>