<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operation="operation()"
    :operationColumnWidth="140"
    :moreParameters="moreParameters"
    :defaultSearchData="defaultSearchData"
  >
    <website-outside-new-parts-to-sales-information
      v-if="showFromBool"
      v-bind="fromPm"
      @success="$refs.pageRef.refreshList"
      @goBack="
        bool => {
          showFromBool = false
          if (bool === true) {
            this.$refs.pageRef.refreshList()
          }
        }
      "
    />
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import WebsiteOutsideNewPartsToSalesInformation from './components/website-outside-new-parts-to-sales-information.vue'
import {
  workerOldRefundList,
  workerOldRefundRefund,
  WorkerOldRefundListExport,
  workerOldRefundTransferFlag
} from '@/api/material-system/website/website-parts-worker-refund-manage'
import { importSettlementStateImport } from '@/api/material-system/center/center-shop-settlement-record'
import { commonTemplateDownload } from '@/api/common.js'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage, WebsiteOutsideNewPartsToSalesInformation },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 表格属性
      tableAttributes: {},
      // 表格事件
      tableEvents: {},
      showFromBool: false,
      fromPm: {},
      defaultSearchData: []
    }
  },
  computed: {
    moreParameters() {
      return [
        {
          name: '单据状态',
          key: 'flag',
          value: this?.$route?.params?.pageCode || '',
          conditions: [
            {
              label: '全部',
              value: ''
            },
            {
              label: '驳回',
              value: 'REJECT'
            },
            {
              label: '已复核',
              value: 'REVIEWED'
            },
            {
              label: '已返还',
              value: 'REFUNDED'
            },
            {
              label: '待复核',
              value: 'WAIT_REVIEW'
            },
            {
              label: '提交',
              value: 'SUBMIT'
            },
            {
              label: '未返还',
              value: 'NOTREFUND'
            },
            {
              label: '通过',
              value: 'AGREE'
            },
            {
              label: '保存',
              value: 'SAVE'
            }
          ]
        }
      ]
    },
    optionsEvensGroup() {
      return [
        [
          [
            this.optionsEvensAuth('add', ({ moduleName }) => {
              return {
                name: moduleName,
                render: () => {
                  return this.importButton(importSettlementStateImport, moduleName, {
                    flag: 1
                  })
                }
              }
            })
          ],
          [
            this.optionsEvensAuth('daorude', {
              click: () => {
                commonTemplateDownload({ name: '更新配件结算状态模板.xlsx' }, `更新配件结算状态模板`)
                  .then(res => {
                    this.$message({
                      message: '下载成功',
                      type: 'success'
                    })
                  })
                  .catch(err => {
                    this.$message.error('下载失败')
                  })
              }
            })
          ]
        ]
      ]
    }
  },
  created() {
    if (this?.$route?.params?.pageType == 'flag' && this?.$route?.params?.pageCode) {
      this.defaultSearchData = [
        {
          param: `a.${this?.$route?.params?.pageType}`,
          compare: '=',
          value: this?.$route?.params?.pageCode,
          label: this?.$route?.params?.pageName
        }
      ]
    }
  },
  methods: {
    // 列表请求函数
    getList(p, cb) {
      var pam = JSON.parse(JSON.stringify(p))
      try {
        if (pam.flag) {
          var item = pam.params.find(item => item.param == 'a.flag')
          if (item) {
            item.compare = '='
            item.value = pam.flag
          } else {
            pam.params.push({ param: 'a.flag', compare: '=', value: pam.flag })
          }
        }
        pam.params.push({ param: 'a.repair_flag', compare: '=', value: 'OUTSIDE' })
        cb && cb(pam)
        return workerOldRefundList(pam)
      } catch (err) {}
    },
    // 列表导出函数
    exportList(data, name) {
      var p = JSON.parse(JSON.stringify(data))
      p.params.push({
        compare: '=',
        param: 'a.repair_flag',
        value: 'OUTSIDE'
      })
      return WorkerOldRefundListExport(p, name)
    },
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },

    operation() {
      return this.operationBtn({
        view: {
          click: ({ row, index, column }) => {
            this.fromPm = {
              type: 2,
              item: { ...row }
            }
            this.showFromBool = true
          }
        },
        convert: {
          conditions: ({ row, index, column }) => {
            return ~['SAVE'].indexOf(row.flag)
          },
          click: ({ row, index, column }) => {
            this.transferFlag(row)
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return ~['SUBMIT', 'WAIT_REVIEW'].indexOf(row.flag)
          },
          click: ({ row, index, column }) => {
            this.fromPm = {
              type: 1,
              apiType: ~['SUBMIT'].indexOf(row.flag) ? 1 : ~['WAIT_REVIEW'].indexOf(row.flag) ? 2 : null,
              item: { ...row }
            }
            this.showFromBool = true
          }
        }
      })
    },

    // 确认返还
    confirmBack(row) {
      workerOldRefundRefund({
        applyNo: row.applyNo,
        flag: 'AGREE'
      })
        .then(res => {
          this.$message({
            type: 'success',
            message: `设置成功!`
          })
          this.$refs.pageRef.refreshList()
        })
        .catch(err => {
          console.log(err)
        })
    },
    transferFlag(row) {
      workerOldRefundTransferFlag({
        applyNo: row.applyNo
      })
        .then(res => {
          this.$message({
            type: 'success',
            message: `转换成功，请到“旧件返还管理”检查差价!`
          })
          this.$refs.pageRef.refreshList()
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
