<!--
@name: 质检放行
@author: 李恒
@time: 2022-06-13 15:27:17
-->
<template>
  <div>
    <p-card v-if='!editType' title='质检放行' class='overflow-card'>
      <div slot='extra'>
        <p-button icon='filter' @click='toggleFormTag = !toggleFormTag'>{{ toggleFormTag ? '隐藏' : '显示' }}</p-button>
        <p-button class='ml-10' type='primary' @click='toEdit("add")'>图片上传</p-button>
        <p-button class='ml-10' type='primary' @click='toEdit("audit")'>审核</p-button>
      </div>
      <p-grid-form v-show="toggleFormTag" ref="formTarget" :span="6" :labelCol="{ span: 6 }" :items="items"
                   @submit="handleSubmit"
      />
      <p-table ref='dataSource' :data-source.sync='dataSource'
               :scroll='{x:true}'
               :autoLoad="false"
               :columns="tableColumns"
               :remote-url='listRemoteUrl'
               :remoteData='listParams'
               :selectedRowKeys.sync='selectedRowKeys'
               bordered dragle row-key='rqcPassCode'>
        <div slot="action" slot-scope="text,record">
          <a @click='cancel(record)'>冻结</a>
          <a class="ml-10" @click="toEdit('view',record)">详情</a>
        </div>
      </p-table>
    </p-card>
    <edit v-else :editData='editData' :type='editType' @back='back'/>
  </div>
</template>

<script>
import {items, tableColumns0, tableColumns1,} from './js/formConfig'
import {cancel, listRemoteUrl} from './api'
import edit from './edit'
import {getDefaultCargoOwner} from "@/views/kzyWMS/utils/setCargoOwner";

export default {
  components: {edit},
  data() {
    return {
      toggleFormTag: true,
      form: {},
      items: items(this),
      // 列表参数
      tableColumns: [],
      listRemoteUrl,
      listParams: {},
      dataSource: [],
      selectedRowKeys: [],

      editType: '',
      editData: {}
    }
  },
  mounted() {
    this.setTableColumns()
    this.setDefaultData()
  },
  methods: {
    // 切换业务类型
    setTableColumns() {
      const values = this.$refs.formTarget.form.getFieldsValue();
      const {businessType} = values
      if (businessType === '0') {
        this.tableColumns = tableColumns0
      } else {
        this.tableColumns = tableColumns1
      }
    },
    // 设置默认数据
    async setDefaultData() {
      // 设置仓库
      const {warehouseId, warehouseCode, warehouseName} = this.$store.getters.warehouse || {}
      this.$refs.formTarget.form.setFieldsValue({
        warehouseId: warehouseId,
        warehouseCode: warehouseCode,
        warehouseName: warehouseName
      })
      // 设置货主
      const cargoOwner = await getDefaultCargoOwner()
      if (!!cargoOwner) {
        const {companyCode, companyId, companyName} = cargoOwner
        this.$refs.formTarget.form.setFieldsValue({
          cargoOwnerId: companyId,
          cargoOwnerCode: companyCode,
          cargoOwnerName: companyName
        })
        this.$refs.formTarget.form.disableItem('cargoOwnerName')
      }
    },
    toEdit(type, editData = {}) {
      const selectedRowKeys = this.selectedRowKeys
      if (['add', 'audit'].includes(type)) {
        if (selectedRowKeys.length === 0) return this.$message.warn('请选择要处理的的数据')
        if (selectedRowKeys.length > 1) return this.$message.warn('一次只能操作一条数据')
        editData = this.dataSource.find(_ => _.rqcPassCode === selectedRowKeys[0])
        const {status} = editData
        // status === '1' 待判定
        // status === '0' 已判定,不可上传不可点审核
        if (status === '0') return this.$message.warn('当前数据已判定')
      }
      this.editType = type
      this.editData = editData
    },
    cancel(record) {
      const {rqcPassCode,status} = record
      if (!rqcPassCode) return
      if (status === '0') return this.$message.warn('当前数据已判定')
      const self = this
      this.$confirm({
        title: '确定是否冻结',
        onOk() {
          return new Promise((resolve, reject) => {
            cancel({...record}).then(res => {
              if (res.success) {
                self.$message.success('冻结成功')
                self.$refs.dataSource.reload()
                resolve()
              } else {
                self.$message.warn(res.message)
                resolve()
              }
            }).catch(() => {
              resolve()
            })
          })
        }
      })
    },
    // 查询事件
    handleSubmit(event, form) {
      event.preventDefault()
      this.detailsDataList = []
      form.validateFields((error, values) => {
        if (!!error) return
        this.listParams = {
          num: 'rqcLinePassPic',
          params: {
            ...values,
          },
        }
      })
    },
    back() {
      this.editType = ''
      this.$nextTick(() => this.$refs.dataSource.reload())
    },
    onFormChange(name, {selectData}, value) {
      if (!selectData) return
      selectData = Array.isArray(selectData) ? selectData[0] : selectData
      if (name === 'warehouse') {
        if (!value) {
          Object.assign(this.form, {warehouseName: '', warehouseCode: '', warehouseId: ''})
          return
        }
        const {warehouseCode, warehouseName, id: warehouseId} = selectData
        Object.assign(this.form, {warehouseName, warehouseCode, warehouseId})
        return
      }
      if (name === 'consignor') {
        if (!value) {
          Object.assign(this.form, {consignorName: '', consignorCode: '', consignorId: ''})
          return
        }
        const {consignorCode, consignorName, id: consignorId} = selectData
        Object.assign(this.form, {consignorName, consignorCode, consignorId})
        return
      }
    }
  }
}
</script>
