<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :optionsEvensGroup="optionsEvensGroup"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :operation="operation()"
    :operationColumnWidth="220"
    :moreParameters="moreParameters"
    :defaultSearchData="defaultSearchData"
  >
    <website-old-parts-worker-refund-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 WebsiteOldPartsWorkerRefundInformation from './components/website-old-parts-worker-refund-information.vue'
import {
  workerOldRefundList,
  workerOldRefundRefund,
  workerTemplateExcel,
  WorkerOldRefundListExport,
  workerOldRefundTransferFlag
} from '@/api/material-system/website/website-parts-worker-refund-manage'
import import_mixin from '@/components/template/import_mixin.js'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
  components: { TemplatePage, WebsiteOldPartsWorkerRefundInformation },
  mixins: [import_mixin, operation_mixin],
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [],
      // 表格属性
      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'
            }
          ]
        }
      ]
    }
  },
  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: 'INNER' })
        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: 'INNER'
      })
      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
          }
        },
        edit: {
          conditions: ({ row, index, column }) => {
            return row.flag == 'AGREE'
          },
          click: ({ row, index, column }) => {
            this.fromPm = {
              type: 1,
              item: { ...row }
            }
            this.showFromBool = true
          }
        },
        examine: {
          name: ({ row, index, column }) => {
            return row.flag == 'SAVE' ? '转换' : row.flag == 'AGREE' ? '确认返还' : ''
          },
          prompt: ({ row, index, column }) => {
            return row.flag == 'SAVE' ? '是否确定转保外操作？' : row.flag == 'AGREE' ? '是否确定操作？' : ''
          },
          conditions: ({ row, index, column }) => {
            return row.flag == 'SAVE' || row.flag == 'AGREE'
          },
          click: ({ row, index, column }) => {
            if (row.flag == 'SAVE') {
              this.transferFlag(row)
            } else if (row.flag == 'AGREE') {
              this.confirmBack(row)
            }
          }
        }
      })
    },

    // 确认返还
    confirmBack(row) {
      workerOldRefundRefund({
        applyNo: row.applyNo,
        flag: 'REFUNDED'
      })
        .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>
