<template>
  <div>
    <detail-page
      :tableColumns="columns"
      :title="title"
      :tableRemoteUrl="tableRemoteUrl"
      :tableRowKey="tableRowKey"
      :btnOptions="btnOptions"
      :tableAutoLoad="false"
      :loading="loading"
      :queryStatus="true"
      :items="items"
      :typeRule="typeRule"
      :businessData="businessData"
      :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"
      :typeRule="typeRule"
      :businessData="businessData"
      :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 { ruleMoveList, ruleMoveDel, getOptions, 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 } from '@/config/serviceSettings'
import { getBusiness } from '@/views/kzyWMS/api/business' // 系统参数接口

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: 'priority', title: '执行顺序', key: 'priority' },
    { dataIndex: 'warehouseName', title: '仓库', key: 'warehouseName' },
    {
      dataIndex: 'srcWarehouseCategoryName',
      title: '源库别',
      key: 'srcWarehouseCategory',
      scopedSlots: { customRender: 'srcWarehouseCategory' },
    },
    {
      dataIndex: 'destWarehouseCategoryName',
      title: '目标库别',
      key: 'destWarehouseCategory',
      scopedSlots: { customRender: 'destWarehouseCategory' },
    },
    { dataIndex: 'stockStatus', title: '库存状态', key: 'stockStatus', scopedSlots: { customRender: 'stockStatus' } },
    {
      dataIndex: 'auditFlag',
      title: '是否需要审核',
      key: 'auditFlag',
      scopedSlots: { customRender: 'auditFlag' },
    },
    {
      dataIndex: 'syncFlag',
      title: '是否回传ERP',
      key: 'syncFlag',
      scopedSlots: { customRender: 'syncFlag' },
    },
    {
      dataIndex: 'quantityMode',
      title: '包装属性',
      key: 'quantityMode',
      scopedSlots: { customRender: 'quantityMode' },
    },
    {
      dataIndex: 'ruleType',
      title: '规则类型',
      key: 'ruleType',
      scopedSlots: { customRender: 'ruleType' },
    },

    { dataIndex: 'action', title: '操作', align: 'center', key: 'action', scopedSlots: { customRender: 'action' } },
  ]
}

export default {
  components: { DetailPage, addRule },
  mixins: [setwarehouse],
  data() {
    return {
      items,
      columns: [],
      title: '移库规则',
      titleName: '新增移库规则',
      loading: false,
      visibleDetial: false,
      setRecord: {},
      optionsData: [],
      businessData: '',
      typeRule: [],
      codeDatas: [],
      tableRowKey: 'moveRuleId',
      selectedRowKeys: [],
      dataSource: [],
      selectedRows: [],
      tableRemoteUrl: {
        url: upcloud_base_strategy + '/rule/move/v1/list',
        method: 'get',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
      },
      btnOptions: [
        {
          name: '新增',
          icon: '',
          action: 'addRule',
        },
      ],
    }
  },
  created() {
    // this.queryData()
    getBusiness().then((res) => {
      this.businessData = res // 系统参数
    })
    this.$nextTick(() => {
      this.$refs.detial.remoteParams.warehouseId = this.$store.state.user.warehouse.warehouseId
    })
    this.getOptions()
    this.queryCode()
    this.queryTypeRule()
    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 = {
        moveRuleId: record.moveRuleId,
      }
      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)
        }
      })
    },
    queryTypeRule() {
      // 字典查询
      let params = {
        type: 'move_rule_types',
      }
      getCodes(params).then((res) => {
        if (res.success) {
          this.typeRule = []
          res.data.map((itm) => {
            let item = {
              label: itm.dictName,
              value: itm.dictCode,
            }
            this.typeRule.push(item)
          })
        } else {
          this.$exception(res)
        }
      })
    },
    getOptions() {
      // 移库状态查询
      getOptions().then((res) => {
        if (res.success) {
          this.optionsData = []
          res.data.map((itm) => {
            let item = {
              label: itm.name,
              value: itm.code,
            }
            this.optionsData.push(item)
          })
        } 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 = { ...values }
      this.loading = true
      ruleMoveList(params).then((res) => {
        this.loading = false
        if (res.success) {
          this.dataSource = res.data
          // this.$message.success('生成盘点计划成功！')
        } else {
          this.$exception(res)
        }
      })
    },
    selectChange(selectedRowKeys, selectedRows) {
      // 勾选框
      this.selectedRows = selectedRows
      this.selectedRowKeys = selectedRowKeys
    },
  },
}
</script>

<style></style>
