<template>
  <div>
    <div>
      <h4>政策延期</h4>
      <el-divider />
      <el-button class="btn" type="primary" size="mini" @click="delayVisible = true">一键延期</el-button>
      <zj-table
        :table-attributes="tableAttributes"
        :is-drop="true"
        :columns="columns"
        :table-data="tableData"
      />
    </div>
    <div style="margin: 20px 0;text-align: right;">
      <el-button type="primary" size="mini" @click="onClose">取消</el-button>
      <el-button type="primary" size="mini" @click="onSubmit">确定</el-button>
    </div>
    <el-dialog
      title="延期日期"
      :visible.sync="delayVisible"
      width="30%"
      :append-to-body="true"
      :close-on-click-modal="false"
      :before-close="()=>delayVisible = false"
    >
      <el-date-picker
        v-model="delayTime"
        style="width:100%"
        type="datetime"
        size="mini"
        default-time="00:00:00"
        value-format="yyyy-MM-dd HH:mm:ss"
        placeholder="选择日期"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="delayVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleOneTouch">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { updateDelayPolicy } from '@/api/policy_list'
export default {
  props: {
    recordSelected: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      delayTime: null,
      delayVisible: false,
      tableData: [],
      tableAttributes: {}
    }
  },
  computed: {
    columns() {
      return [
        {
          columnAttributes: {
            label: '销售政策编码',
            prop: 'code'
          }
        },
        {
          columnAttributes: {
            label: '销售政策名称',
            prop: 'title'
          }
        },
        {
          columnAttributes: {
            label: '销售政策类型',
            prop: 'type'
          },
          render: (h, { column, row, index }) => {
            return (
              <div>
                {row.type === 'PROVISION' ? '配提' : '限量'}
              </div>
            )
          }
        },
        {
          columnAttributes: {
            label: '生效日期',
            prop: 'startTime'
          }
        },
        {
          columnAttributes: {
            label: '失效日期',
            prop: 'endTime'
          }
        },
        {
          columnAttributes: {
            label: '延期日期',
            prop: 'materialName'
          },
          render: (h, { column, row, index }) => {
            return <div style='margin:0 10px'>
              <el-date-picker
                style='width:100%'
                value={row.date}
                onInput={e => { row.date = e }}
                type='datetime'
                size='mini'
                default-time='00:00:00'
                picker-options={ this.setDisabled(row.startTime) }
                value-format='yyyy-MM-dd HH:mm:ss'
                placeholder='选择日期'>
              </el-date-picker>
            </div>
          }
        },
        {
          columnAttributes: {
            label: '操作',
            prop: ''
          },
          render: (h, { column, row, index }) => {
            return (
              <div>
                <el-button
                  type='text'
                  size='default'
                  onClick={() => {
                    this.tableData.splice(index, 1)
                  }}
                >
                      删除
                </el-button>
              </div>
            )
          }
        }
      ]
    }
  },
  mounted() {
    this.recordSelected.forEach(k => {
      this.$set(k, 'date', '')
    })
    this.tableData = this.recordSelected
  },
  methods: {
    setDisabled(e) {
      return {
        disabledDate: time => {
          return time.getTime() < new Date(e).getTime() + 24 * 3600 * 1000
        }
      }
    },
    onSubmit() {
      if (!this.tableData.every(k => !!k.endTime)) {
        this.$errorMsg('政策没有失效时间不能延迟')
        return
      }
      if (!this.tableData.every(k => !!k.date)) {
        this.$errorMsg('请选择日期时间')
        return
      }
      const list = this.tableData.map(k => {
        return {
          endTime: k.date,
          policyId: k.id
        }
      })
      updateDelayPolicy(list).then(res => {
        this.$successMsg('批量修改成功')
        this.onClose()
      })
    },
    onClose() {
      this.$emit('close')
    },
    handleOneTouch() {
      this.tableData.forEach(k => {
        k.date = this.delayTime
      })
      this.delayTime = null
      this.delayVisible = false
    }
  }
}
</script>

  <style lang="scss" scoped>
.btn{
  float:right;
  margin-bottom: 20px;
}
  </style>

