<template>
  <div v-loading="loading">
    <div class="white-box">
      <div class="filter-box">
        <filter-user :filter-types="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table class="table-box" ref="multipleTable" :data="tableData" stripe style="width: 100%;" :height="tool.getTableHeight()">
        <el-table-column v-for="(tableHeader, key) in tableHeaders" :key="key" :label="tableHeader.header" :width="tableHeader.other.width ? tableHeader.other.width : ''" :prop="tableHeader.other.prop ? tableHeader.other.prop : null">
          <template slot-scope="scope">
            <template v-if="scope.row[tableHeader.index].other.tag" >
              <el-tag :type="scope.row[tableHeader.index].other.tag.type ? scope.row[tableHeader.index].other.tag.type : null" :size="scope.row[tableHeader.index].other.tag.size ? scope.row[tableHeader.index].other.tag.size : null">{{scope.row[tableHeader.index].value}}</el-tag>
            </template>
            <template v-else>
              <div v-html="scope.row[tableHeader.index].value"></div>
            </template>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <el-button type="primary" size="small" @click="visible = true" v-show="permission.hasPermission(`shop/order-adjust-period`)">调整订单期数</el-button>
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>

    <el-dialog title="调整订单期数" :visible.sync="visible" width="50%">
      <el-form ref="form" :model="form" label-width="250px" class="form-page">
        <el-form-item label="订单编号" required clearable>
          <el-input v-model="form.orderSn" @change="handleOrderSnChange"></el-input>
        </el-form-item>
        <el-form-item label="会员编号" v-show="order.USER_NAME">
          <el-input v-model="order.USER_NAME" disabled></el-input>
        </el-form-item>
        <el-form-item label="订单类型" v-show="order.ORDER_TYPE">
          <el-input v-model="order.ORDER_TYPE" disabled></el-input>
        </el-form-item>
        <el-form-item label="PV" v-show="order.PV">
          <el-input v-model="order.PV" disabled></el-input>
        </el-form-item>
        <el-form-item label="订单金额" v-show="order.PAY_AMOUNT">
          <el-input v-model="order.PAY_AMOUNT" disabled></el-input>
        </el-form-item>
        <el-form-item label="创建人编号" v-show="order.CREATE_USER">
          <el-input v-model="order.CREATE_USER" disabled></el-input>
        </el-form-item>
        <el-form-item label="期数" v-show="order.PERIOD_NUM">
          <el-input v-model="order.PERIOD_NUM" disabled></el-input>
        </el-form-item>
        <el-form-item label="调整期数" required>
          <el-select v-model="form.modernPeriod" placeholder="请选择要调整到的期数">
            <el-option v-for="item in availablePeriod" :key="item.PERIOD_NUM" :label="item.PERIOD_NUM" :value="item.PERIOD_NUM" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" :loading="submitButtonStat">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import FilterUser from '@/components/FilterUser'
  import permission from '@/utils/permission'
  import Pagination from '@/components/Pagination'
  import filterHelper from '@/utils/filterHelper'

  export default {
    name: 'shop_order-period-adjust',
    components: {FilterUser, Pagination},
    mounted() {
      this.getData()
    },
    data() {
      return {
        tableHeaders: null,
        tableData: null,
        tableHeight: window.innerHeight - 310,
        loading: true,
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: null,
        filterModel: {},

        submitButtonStat: false,
        visible: false,
        form: {
          orderSn: '',
          modernPeriod: '',
        },

        order: {},
        availablePeriod: null,
      }
    },
    methods: {
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
          network.getPageData(this, 'shop/order-period-adjust', page, pageSize, filterData, response => {
            this.filterTypes = response.filterTypes
            this.allData = response
          })
      },
      handleOrderSnChange() {
        if (this.form.orderSn) {
          this.submitButtonStat = true
          network.getData('shop/order-detail', { orderSn: this.form.orderSn }).then(response => {
            this.order = response.order
            this.availablePeriod = response.availablePeriod
            if (response.availablePeriod && response.availablePeriod[0]) {
              for (let period of response.availablePeriod) {
                if (!period['disabled']) {
                  this.form.modernPeriod = period['PERIOD_NUM']
                  break
                }
              }
            }

            this.submitButtonStat = false
          }).catch(error => {
            this.$message({
              message: error,
              type: 'error'
            })
            this.submitButtonStat = false
          })
        }
      },
      handlePeriodChange() {
        network.getData('shop/period-inspect', { modernPeriod: this.form.modernPeriod }).then(response => {

        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          })
        })
      },
      onSubmit() {
        this.$confirm('是否要修改当前订单期数？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this._handleSubmit()
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          })
          this.submitButtonStat = false
        })
      },
      _handleSubmit() {
        this.submitButtonStat = true
        network.postData('shop/order-period-adjust', this.form).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          this.submitButtonStat = false
          this._clearData()
        }).catch(error => {
          this.$message({
            message: error,
            type: 'error'
          })
          this.submitButtonStat = false
        })
      },
      _clearData(){
        this.form = {}
        this.order = {}
        this.visible = false
        this.getData()
      }
    }
  }
</script>

<style scoped>

</style>
