<template>
  <div>
    <div class="sty">
      <el-page-header @back="goBack" content="销售返利单"> </el-page-header>
    </div>
    <br />
    <br />
    <span>返利单</span>
    <el-divider></el-divider>
    <!-- 表头 -->
    <div>
      <el-form ref="searchForm" :model="searchForm" label-width="100px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="返利单号" prop="">
              <el-input disabled v-model="searchForm.rebateOrderId" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="返利日期" prop="">
              <el-input disabled v-model="searchForm.theTime" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="备注" prop="">
              <el-input disabled v-model="searchForm.remark" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="制单人" prop="">
              <el-input disabled v-model="searchForm.createBy" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="制单日期" prop="">
              <el-input disabled v-model="searchForm.createTime" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="审批人" prop="">
              <el-input
                v-if="this.searchForm.secondExamineBy != null"
                disabled
                v-model="searchForm.secondExamineBy"
                placeholder=""
              ></el-input>
              <el-input v-else disabled v-model="searchForm.examineBy" placeholder=""></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="审批日期" prop="">
              <el-input
                v-if="this.searchForm.secondExamineTime != null"
                disabled
                v-model="searchForm.secondExamineTime"
                placeholder=""
              ></el-input>
              <el-input v-else disabled v-model="searchForm.examineTime" placeholder=""></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column label="序号" align="left" width="100" type="index" show-overflow-tooltip></el-table-column>
          <el-table-column
            align="left"
            label="返利类型"
            prop="walletName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="right" label="总返利金额" prop="amount" min-width="160" show-overflow-tooltip>
            <!-- <template slot-scope="scope">

            </template> -->
          </el-table-column>
          <el-table-column align="right" label="返利金额" prop="" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-input
                style="width: 100%"
                class="mountclass"
                :disabled="show"
                type="number"
                v-model="scope.row.rebateAmount"
              ></el-input>
            </template>
          </el-table-column>
          <!--          <el-table-column align="right" label="折让金额" prop="" min-width="160" show-overflow-tooltip>-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input-->
          <!--                class="mountclass"-->
          <!--                :disabled="show"-->
          <!--                type="number"-->
          <!--                @mousewheel.native.prevent-->
          <!--                v-model="scope.row.allowanceAmount"-->
          <!--              ></el-input>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column-->
          <!--            align="left"-->
          <!--            label="备注"-->
          <!--            prop="policyDocNo"-->
          <!--            min-width="160"-->
          <!--            show-overflow-tooltip-->
          <!--          ></el-table-column>-->
          <el-table-column
            align="left"
            label="备注1"
            prop="remark1"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="备注2"
            prop="remark2"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
      </div>
    </div>
    <br />
    <div v-if="!show">
      <el-button type="primary" size="small" @click="btnFn">确定</el-button>
      <el-button type="primary" size="small" @click="resetFn">重置</el-button>
    </div>
  </div>
</template>

<script>
import { getRebateOrderDetail, getRebateOrderAck, getRebateOrderAclUpdate } from '@/api/finance/rebate_list'
export default {
  props: {
    detailId: {
      type: String,
      required: true
    },
    isShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      state: null,
      examineStatus: '',
      searchForm: {
        rebateOrderId: '',
        theTime: '',
        createBy: '',
        createTime: '',
        remark: ''
      },
      dataList: [],
      saveDataList: [],
      listLoading: false // 列表加载loading
    }
  },
  computed: {
    isCustomer() {
      return this.$store.getters.customerId && this.$store.getters.customerNumber
    },
    show() {
      if (this.isShow && this.examineStatus == 'OK_ONE') {
        this.state = 1
        if (this.isCustomer) {
          return true
        }
        return false
      } else if (!this.isShow) {
        this.state = 2
        console.log(33)
        return false
      } else if (this.isShow && (this.examineStatus == 'OK' || this.examineStatus == 'FAIL')) {
        return true
      }
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    //深拷贝数据
    deepCopy(data) {
      //string,number,bool,null,undefined,symbol
      //object,array,date
      if (data && typeof data === 'object') {
        //针对函数的拷贝
        if (typeof data === 'function') {
          let tempFunc = data.bind(null)
          tempFunc.prototype = this.deepCopy(data.prototype)
          return tempFunc
        }

        switch (Object.prototype.toString.call(data)) {
          case '[object String]':
            return data.toString()
          case '[object Number]':
            return Number(data.toString())
          case '[object Boolean]':
            return Boolean(data.toString())
          case '[object Date]':
            return new Date(data.getTime())
          case '[object Array]':
            var arr = []
            for (let i = 0; i < data.length; i++) {
              arr[i] = this.deepCopy(data[i])
            }
            return arr

          //js自带对象或用户自定义类实例
          case '[object Object]':
            var obj = {}
            for (let key in data) {
              //会遍历原型链上的属性方法，可以用hasOwnProperty来控制 （obj.hasOwnProperty(prop)
              obj[key] = this.deepCopy(data[key])
            }
            return obj
        }
      } else {
        //string,number,bool,null,undefined,symbol
        return data
      }
    },
    //重置
    resetFn() {
      // console.log(this.dataList, this.saveDataList);
      this.dataList = this.saveDataList
      this.saveDataList = this.deepCopy(this.dataList)
    },
    //确定
    async btnFn() {
      // console.log(this.dataList);
      const res = this.dataList.map(v => {
        return {
          // allowanceAmount: v.allowanceAmount,
          allowanceAmount: 0,
          rebateAmount: v.rebateAmount,
          itemId: v.id
        }
      })

      if (this.state == 1) {
        await getRebateOrderAclUpdate(res)
        this.$message.success('编辑成功')
      } else {
        await getRebateOrderAck(res)
        this.$message.success('确认成功')
      }

      this.$emit('updateList')
      this.$emit('setShowPage', 1)
    },
    //获取详情数据
    async getDataList() {
      let res = await getRebateOrderDetail({ id: this.detailId })
      this.okId = res.data.id
      this.searchForm.rebateOrderId = res.data.id
      this.searchForm.theTime = res.data.theTime
      this.searchForm.createBy = res.data.createBy
      this.searchForm.createTime = res.data.createTime
      this.searchForm.remark = res.data.remark
      this.searchForm.secondExamineBy = res.data.secondExamineBy
      this.searchForm.secondExamineTime = res.data.secondExamineTime
      this.searchForm.examineBy = res.data.examineBy
      this.searchForm.examineTime = res.data.examineTime
      this.examineStatus = res.data.items[0].examineStatus
      this.saveDataList = this.deepCopy(res.data.items)
      this.dataList = res.data.items

      if (this.state != 1) {
        this.dataList.forEach(k => {
          k.rebateAmount = k.amount
        })
      }
    },
    goBack() {
      this.$emit('setShowPage', 1)
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .mountclass {
  input {
    text-align: right;
    width: 100%;
  }
}
</style>
