    <template>
      <div class="app-container calendar-list-container">
        <el-form label-position="right" label-width="100px"  style='width:100%;'>
          <el-row :gutter="24">
            <el-col :xs="23" :sm="23" :md="12" :lg="6" :xl="6">
              <div class="grid-content bg-purple" >
                <el-form-item  :label="$t('promotion.name')" >
                  <div class="block">
                    <el-input style="width: 100%;" class="filter-item"
                        v-model="seachQuery.po_name" :maxlength = 20>
                    </el-input>
                  </div>
                </el-form-item>
              </div> 
        </el-col>
          <el-col :xs="23" :sm="23" :md="11" :lg="6" :xl="6">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('adminstock.startdate')" >
                <div class="block">
                  <el-date-picker v-model="seachQuery.po_startdate" value-format="yyyy-MM-dd" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1" style="width: 100%;">
                  </el-date-picker>
                </div>
              </el-form-item>
            </div> 
          </el-col>
          <el-col :xs="23" :sm="23" :md="12" :lg="6" :xl="6">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('adminstock.enddate')" >
                <div class="block">
                  <el-date-picker v-model="seachQuery.po_enddate" value-format="yyyy-MM-dd" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1" style="width: 100%;">
                  </el-date-picker>
                </div>
              </el-form-item>
            </div> 
          </el-col>
          <el-col :xs="23" :sm="23" :md="11" :lg="5" :xl="5">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('promotion.mkt')" >
                <div class="block">
                  <el-select v-if="mktEnable" disabled class="filter-item" v-model="seachQuery.po_mkt" default-first-optionplaceholder="请选择城市" style="width: 100%;">
                    <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="seachQuery.po_mkt" default-first-optionplaceholder="请选择城市" style="width: 100%;">
                    <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="24">
            <el-col :xs="23" :sm="23" :md="12" :lg="6" :xl="6">
              <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('promotion.createuser')" >
                <div class="block">
                  <el-input style="width:100%;" class="filter-item"
                      v-model="seachQuery.po_createuser" :maxlength = 20>
                  </el-input>
                </div>
              </el-form-item>
            </div> 
        </el-col>
          <el-col :xs="23" :sm="23" :md="11" :lg="6" :xl="6">
            <div class="grid-content bg-purple" >
              <el-form-item  :label="$t('promotion.createtime')" >
                <div class="block">
                  <el-date-picker v-model="seachQuery.po_createtime" value-format="yyyy-MM-dd" align="right" type="date" placeholder="选择日期" :picker-options="pickerOptions1" style="width: 100%;">
                  </el-date-picker>
                </div>
              </el-form-item>
            </div> 
          </el-col>
          </el-row>
          <el-row :gutter="24" >
            <div  align="center" > <el-button class="filter-item" type="primary" :loading ="buttonloading" v-waves icon="el-icon-search" @click="handleFilterfirst">
            {{$t('table.search')}}
          </el-button>
          <el-button class="filter-item" type="primary" v-waves icon="el-icon-delete" @click="handleref">
            {{$t('table.cancel')}}
          </el-button></div>
          </el-row>
          <el-row :gutter="24">
            <div align="right">
              <el-button class="filter-item"  @click="handleCreate" type="primary"
                    icon="el-icon-edit">{{$t('table.add')}}
            </el-button>&nbsp;&nbsp;&nbsp;</div>    
          </el-row>
        </el-form>
        <el-table :key='tableKey' :data="list" v-loading="listLoading" element-loading-text="加载中......" border fit
                  highlight-current-row
                  style="width: 100%">
          <el-table-column align="center" :label="$t('promotion.name')">
            <template slot-scope="scope">
              <span>{{scope.row.po_name}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('promotion.desc')">
            <template slot-scope="scope">
              <span>{{scope.row.po_desc}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('adminstock.startdate')">
            <template slot-scope="scope">
              <span>{{scope.row.po_startdate}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('adminstock.enddate')">
            <template slot-scope="scope">
              <span>{{scope.row.po_enddate}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('promotion.mkt')">
            <template slot-scope="scope">
              <span>{{scope.row.po_mkt}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" :label="$t('table.actions')" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <a href="#" @click="hrePromotionOLhedit(scope.row.id)"><el-tag>{{$t('table.edit')}}</el-tag></a> 
            <a href="#" @click="delPromotionOL(scope.row.id)"><el-tag>{{$t('table.delete')}}</el-tag></a> 
          </template>
        </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                        :current-page="page" :page-sizes="[10,20,30,50]" :page-size="limit"
                        layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>
        </div>
        <!--添加批次-->
        <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :before-close="handleClose">
          <el-form :rules="rules" ref="addForm" :model="addpol" label-position="left" label-width="80px"
                  style='width: 600px;margin-left:20px'>
                <el-row :gutter="20" 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="addpol.po_name"> </el-input>
                        </div>
                      </el-form-item>
                    </div>
                  </el-col>
                  <el-col :span="8">
                    <div class="grid-content bg-purple" style="width: 300px;margin-left:120px;">
                      <el-form-item  :label="$t('promotion.desc')" style="width: 310px;">
                        <div class="block">
                      <el-input v-model="addpol.po_desc"> </el-input>
                        </div>
                      </el-form-item>
                    </div>
                  </el-col>
                </el-row>

                <el-row :gutter="20" 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="addpol.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;margin-left:120px;">
                      <el-form-item  :label="$t('adminstock.enddate')" prop="po_enddate" style="width: 310px;">
                        <div class="block">
                      <el-date-picker v-model="addpol.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="24" 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="addpol.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="addpol.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="24" 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-form>
          <div slot="footer" class="dialog-footer">
            <el-button  type="primary" @click="addPromotionOL">{{$t('table.confirm')}}</el-button>
            <el-button @click="resetForm('addForm')">{{$t('navbar.reset')}}</el-button>
          </div>
        </el-dialog>

        <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 { fetchPv, updateArticle, seachList, createArticle, getBranches } from '@/api/article'
      import { getPackageSelect } from '@/api/couponapi'
      import { getPromotionOfflines, delPromotionOffline, insertPromotionOffline, 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: 'promotionoffline',
        directives: {
          waves
        },
        data() {
          return {
            userMkt: null,
            mktEnable: null,
            url: config.BASE_URL + '/uploadPromotionImg',
            dialogVisible: false,
            dialogImageUrl: undefined,
            fileListvoucher: [],
            tableKey: 0,
            list: null,
            total: null,
            page: 1,
            removePhoto: {
              cv_filename: null
            },
            limit: 10,
            queryType: 'list',
            listLoading: true,
            buttonloading: false,
            listQuery: {
              po_mkt: null,
              importance: undefined,
              title: undefined,
              type: undefined,
              sort: '+id'
            },
            seachQuery: {
              po_name: null,
              po_startdate: null,
              po_enddate: null,
              po_mkt: null,
              po_createtime: null,
              po_createuser: null,
              page: null,
              limit: null
            },
            reqFileUplaod: {
              file: undefined
            },
            idcardquery: {
              idcard: null
            },
            promotionofflinedel: {
              id: undefined
            },
            checksalt: {
              salt: undefined
            },
            sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
            branchesOptions: [],
            citysOptions: [],
            packageoptions: [],
            showReviewer: false,
            addpol: {
              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
            },
            ethnicgroupptions: '',
            provinceoptions: '',
            dialogFormVisible: false,
            checkidcard: {
              idcard: undefined
            },
            dialogStatus: '',
            textMap: {
              update: 'Edit',
              create: '添加月嫂'
            },
            dialogPvVisible: false,
            pvData: [],
            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)
                }
              }]
            },
            updateStatus: {
              id: undefined,
              status: undefined
            },
            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
          if (this.userMkt !== 'ALL') {
            this.seachQuery.po_mkt = this.userMkt
            this.listQuery.po_mkt = this.userMkt
            this.mktEnable = true
          }
          this.getList()
          this.list = []
          this.listLoading = false
          getBranches().then(response5 => {
            this.branchesOptions = response5.data
            this.citysOptions = response5.data
          })
          getPackageSelect().then(response6 => {
            this.packageoptions = response6.data
          })
        },
        methods: {
          getList() {
            this.listQuery.page = this.page
            this.listQuery.limit = this.limit
            this.queryType = 'list'
            this.listLoading = true
            getPromotionOfflines(this.listQuery).then(response => {
              this.list = response.data.items
              this.total = response.data.total
              this.listLoading = false
            })
          },
          handleFilter() {
            if (this.seachQuery.experience === 0) {
              this.seachQuery.experience = null
            }
            this.seachQuery.page = this.page
            this.seachQuery.limit = this.limit
            this.queryType = 'query'
            this.listLoading = true
            seachList(this.seachQuery).then(response => {
              this.list = response.data.items
              this.total = response.data.total
              this.listLoading = false
            })
          },
          handleFilterfirst() {
            this.seachQuery.page = 1
            this.seachQuery.limit = 10
            this.page = 1
            this.limit = 10
            this.queryType = 'query'
            this.listLoading = true
            this.buttonloading = true
            getPromotionOfflines(this.seachQuery).then(response => {
              this.buttonloading = false
              this.list = response.data.items
              this.total = response.data.total
              this.listLoading = false
            })
          },
          handleSizeChange(val) {
            this.limit = val
            if (this.queryType === 'list') {
              this.listQuery.limit = val
              this.listQuery.page = this.page
              this.getList()
            } else {
              this.seachQuery.limit = val
              this.seachQuery.page = this.page
              this.handleFilter()
            }
          },
          handleCurrentChange(val) {
            this.page = val
            if (this.queryType === 'list') {
              this.listQuery.limit = this.limit
              this.listQuery.page = val
              this.getList()
            } else {
              this.seachQuery.limit = this.limit
              this.seachQuery.page = val
              this.handleFilter()
            }
          },
          handleModifyStatus(row, status) {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            row.status = status
          },
          handleCreate() {
            this.addpol.dialogStatus = 'create'
            this.addpol.po_name = null
            this.addpol.po_desc = null
            this.addpol.po_headimg = null
            this.addpol.po_icon = null
            this.addpol.po_startdate = null
            this.addpol.po_enddate = null
            this.addpol.po_mkt = null
            this.addpol.po_createtime = null
            this.addpol.po_createuser = null
            this.fileListvoucher = []
            this.dialogFormVisible = true
            if (this.userMkt !== 'ALL') {
              this.addpol.po_mkt = this.userMkt
            }
            this.$nextTick(() => {
              this.$refs['addForm'].clearValidate()
            })
          },
          handleClose(done) {
            this.$confirm('确认关闭？').then(_ => {
              if (this.addpol.po_headimg !== null) {
                this.removePhoto.cv_filename = this.addpol.po_headimg
                deletePromotionImg(this.removePhoto).then(response9 => {
                })
              }
              done()
            }).catch(_ => {})
          },
          addPromotionOL() {
            this.$refs['addForm'].validate((valid) => {
              if (valid) {
                if (this.addpol.po_headimg !== null) {
                  this.addpol.po_createuser = this.$store.getters.introduction
                  this.listLoading = true
                  const date1 = Date.parse(this.addpol.po_startdate)
                  const date2 = Date.parse(this.addpol.po_enddate)
                  const diffDays = date2 - date1
                  if (diffDays < 0) {
                    this.$message({
                      message: '请正确录入活动日期',
                      type: 'warning'
                    })
                  } else {
                    insertPromotionOffline(this.addpol).then(response => {
                      if (response.data === 1) {
                        this.list = response.data.items
                        this.total = response.data.total
                        this.listLoading = false
                        this.$refs['addForm'].validate((valid) => {
                          if (valid) {
                            createArticle(this.temp).then(() => {
                              this.dialogFormVisible = false
                              this.$notify({
                                title: '成功',
                                message: '创建成功',
                                type: 'success',
                                duration: 2000
                              })
                            })
                          }
                          this.getList()
                        })
                      } else {
                        this.listLoading = false
                        this.$refs['addForm'].validate((valid) => {
                          if (valid) {
                            createArticle(this.temp).then(() => {
                              this.dialogFormVisible = false
                              this.$notify({
                                title: '失败',
                                message: '生成失败！',
                                type: 'error',
                                duration: 2000
                              })
                            })
                          }
                        })
                      }
                    })
                  }
                } else {
                  this.$message({
                    message: '请上传活动图片！',
                    type: 'warning'
                  })
                }
              }
            })
          },
          handleUpdate(row) {
            this.temp = Object.assign({}, row) // copy obj
            this.temp.timestamp = new Date(this.temp.timestamp)
            this.dialogStatus = 'update'
            this.dialogFormVisible = true
            this.$nextTick(() => {
              this.$refs['dataForm'].clearValidate()
            })
          },
          hrePromotionOLhedit(id) {
            this.$router.push({ name: 'promotionofflineedit', params: { promotionofflineid: id }})
          },
          UploadUrl() {
            return this.url
          },
          delPromotionOL(cb_code) {
            createArticle(this.temp).then(() => {
              this.$confirm('此操作将永久删除该单据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
              }).then(() => {
                this.promotionofflinedel.id = cb_code
                this.listLoading = true
                delPromotionOffline(this.promotionofflinedel).then(response => {
                  this.list = response.data.items
                  this.total = response.data.total
                  this.listLoading = false
                  if (response.data === 1) {
                    createArticle(this.temp).then(() => {
                      this.$notify({
                        title: '成功',
                        message: '删除成功',
                        type: 'success',
                        duration: 2000
                      })
                    })
                  } else {
                    createArticle(this.temp).then(() => {
                      this.$notify({
                        title: '错误',
                        message: '错误',
                        type: 'error',
                        duration: 2000
                      })
                    })
                  }
                  this.getList()
                })
              }).catch(() => {})
            })
          },
          updateData() {
            this.$refs['dataForm'].validate((valid) => {
              if (valid) {
                const tempData = Object.assign({}, this.temp)
                tempData.timestamp = +new Date(tempData.timestamp) // change Thu Nov 30 2017 16:41:05 GMT+0800 (CST) to 1512031311464
                updateArticle(tempData).then(() => {
                  for (const v of this.list) {
                    if (v.id === this.temp.id) {
                      const index = this.list.indexOf(v)
                      this.list.splice(index, 1, this.temp)
                      break
                    }
                  }
                  this.dialogFormVisible = false
                  this.$notify({
                    title: '成功',
                    message: '更新成功',
                    type: 'success',
                    duration: 2000
                  })
                })
              }
            })
          },
          handleDelete(row) {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          },
          handleFetchPv(pv) {
            fetchPv(pv).then(response => {
              this.pvData = response.data.pvData
              this.dialogPvVisible = true
            })
          },
          beforeUpdate(file) {
            if (file.size > 1000000) {
              this.$message.error('上传照片大于1M！')
              return false
            }
          },
          handleDownload() {
            this.downloadLoading = true
            import('@/vendor/Export2Excel').then(excel => {
              const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
              const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
              const data = this.formatJson(filterVal, this.list)
              excel.export_json_to_excel(tHeader, data, 'table-list')
              this.downloadLoading = false
            })
          },
          handleref() {
            this.seachQuery.po_name = null
            this.seachQuery.po_startdate = null
            this.seachQuery.po_enddate = null
            this.seachQuery.po_mkt = null
            this.seachQuery.po_createtime = null
            this.seachQuery.po_createuser = null
            if (this.userMkt !== 'ALL') {
              this.seachQuery.po_mkt = this.userMkt
            }
          },
          resetForm(formName) {
            if (this.addpol.po_headimg !== null) {
              this.removePhoto.cv_filename = this.addpol.po_headimg
              deletePromotionImg(this.removePhoto).then(response9 => {
              })
            }
            this.$refs[formName].resetFields()
            if (this.userMkt !== 'ALL') {
              this.addpol.po_mkt = this.userMkt
            }
          },
          handleRemovevoucher(file, fileList) {
            this.removePhoto.cv_filename = file.name
            deletePromotionImg(this.removePhoto).then(response9 => {
            })
          },
          uploadSuccess(response, file, fileList) {
            const aa = response.split('/')
            const aatemp = aa[aa.length - 1]
            this.addpol.po_headimg = aatemp
          },
          handlePictureCardPreview(file) {
            this.dialogImageUrl = file.url
            this.dialogVisible = true
          },
          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>
    