<template>
  <div>
    <detail-page
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :tableAutoLoad="false"
      :loading="loading"
      :queryStatus="true"
      :items="items"
      :optionsData="optionsData"
      :codeDatas="codeDatas"
      :dataSource="dataSource"
      @addRule="addRule"
      @selectChange="selectChange"
      @delData="delData"
      @changeClick="changeClick"
      ref="detial"
    >
    </detail-page>
    <addRule
      :title="titleName"
      :visibleDetial="visibleDetial"
      :optionsData="optionsData"
      :setRecord="setRecord"
      @cancelRule="cancelRule"
      @requestSuccess="requestSuccess"
    ></addRule>
  </div>
</template>

<script>
import DetailPage from './components/detial'
import addRule from './components/addRule'
import API from './api/default'
import { ruleQuery, ruleMoveDel, getCodes } from './api/api'
import { diffOption, forSelectText } from '@/views/kzyWMS/utils/index.js'
import { sorter } from '../../utils/sort'
import { setwarehouse } from '../config/warehousemixin.js'
import { upcloud_base_strategy, SERVICE_DATA_API_BASE } from '@/config/serviceSettings'

const items = [
  { type: 'input', name: 'warehouseId', label: '仓库ID', hidden: true },
  {
    type: 'search',
    name: 'warehouseName',
    label: '仓库',
    required: true,
    destroyOnClose: true,
    targetValueKey: 'warehouseName',
    targetClick: true,
    span: 6,
    value: '',
    toolBar: {
      items: [
        {
          type: 'input',
          name: 'warehouseName',
          label: '仓库名称',
          span: 8,
          value: '',
        },
        {
          type: 'input',
          name: 'warehouseCode',
          label: '仓库编码',
          span: 8,
          value: '',
        },
      ],
    },
    tableConfig: {
      rowKey: 'warehouseName',
      rowSelectType: 1,
      hasRowSelectCheck: true,
      columns: [
        { dataIndex: 'warehouseName', title: '仓库名称', key: 'warehouseName' },
        { dataIndex: 'warehouseCode', title: '仓库编码', key: 'warehouseCode' },
        { dataIndex: 'warehouseId', title: '仓库ID', key: 'warehouseId' },
      ],
    },
    remoteUrl: API.warehouse,
    on: {
      ok(event, value, rowData, form) {
        form.setFieldsValue({
          warehouseId: rowData[0].warehouseId,
        })
      },
      change(event, value, rowData, form) {
        if (!value) {
          form.setFieldsValue({
            warehouseId: '',
          })
        }
      },
    },
  },
]
const columns = function (self) {
  return [
    { dataIndex: 'warehouseName', title: '仓库', key: 'warehouseName' },
    { dataIndex: 'cargoOwnerName', title: '货主', key: 'cargoOwnerName' },
    {
      dataIndex: 'isEnabled',
      title: '是否启用',
      key: 'isEnabled',
      scopedSlots: { customRender: 'isEnabled' },
    },
    {
      dataIndex: 'wmsStatusName',
      title: 'WMS库存状态',
      key: 'wmsStatus',
      scopedSlots: { customRender: 'wmsStatus' },
    },

    {
      dataIndex: 'erpStatusName',
      title: 'ERP库存状态',
      key: 'erpStatus',
      scopedSlots: { customRender: 'erpStatus' },
    },
    { dataIndex: 'action', title: '操作', align: 'center', key: 'action', scopedSlots: { customRender: 'action' } },
  ]
}

export default {
  components: { DetailPage, addRule },
  mixins: [setwarehouse],
  data() {
    return {
      items,
      columns: columns(this),
      title: '库存状态调整规则',
      titleName: '新增库存状态调整规则',
      loading: false,
      visibleDetial: false,
      setRecord: {},
      optionsData: [],
      codeDatas: [],
      tableRowKey: 'invAdjustId',
      selectedRowKeys: [],
      dataSource: [],
      selectedRows: [],
      tableRemoteUrl: {
        url: SERVICE_DATA_API_BASE + '/search/page',
        method: 'post',
        params: {
          id: 'findInvAdjustRuleList',
        },
      },
      btnOptions: [
        {
          name: '新增',
          icon: '',
          action: 'addRule',
        },
      ],
    }
  },
  created() {
    // this.queryData()
    this.$nextTick(() => {
      this.$refs.detial.remoteParams.warehouseId = this.$store.state.user.warehouse.warehouseId
    })
    this.queryCode()
    diffOption(columns(this)).then((res) => {
      console.log(res)
      this.columns = sorter(res)
    })
  },
  methods: {
    addRule() {
      // 新增
      this.setRecord = {}
      this.titleName = '新增库存状态调整规则'
      this.visibleDetial = true
    },
    cancelRule(val) {
      this.visibleDetial = val
    },
    requestSuccess(val) {
      this.visibleDetial = false
      this.queryData()
    },
    delData(record) {
      // 删除
      let params = {
        invAdjustId: record.invAdjustId,
      }
      ruleMoveDel(params).then((res) => {
        this.loading = false
        if (res.success) {
          this.$message.success('删除成功！')
          this.queryData()
        } else {
          this.$exception(res)
        }
      })
    },
    queryCode() {
      // 字典查询
      let params = {
        type: 'wms_storeroom',
      }
      getCodes(params).then((res) => {
        if (res.success) {
          this.codeDatas = res.data
        } else {
          this.$exception(res)
        }
      })
    },
    changeClick(record) {
      // 修改
      this.titleName = '修改库存状态调整规则'
      this.setRecord = record
      this.visibleDetial = true
    },
    queryData(arrList) {
      // 查询
      let values = this.$refs.detial.$refs.gridform.form.getFieldsValue()
      let params = {
        num: 'findInvAdjustRuleList',
        params: {
          ...values,
        },
      }
      this.loading = true
      ruleQuery(params).then((res) => {
        this.loading = false
        if (res.success) {
          this.dataSource = res.data.list
          // this.$message.success('生成盘点计划成功！')
        } else {
          this.$exception(res)
        }
      })
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
  },
}
</script>

<style></style>
