<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :optionsEvensGroup="optionsEvensGroup"
    :exportList="exportList"
    :operation="operation()"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :columnParsing="columnParsing"
  >
    <!-- 弹窗 -->
    <el-dialog
      title="仓库仓位对应关系"
      :visible.sync="showDialogForm"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="diaLogForm" :model="diaLogForm" label-width="70px" size="small" label-position="left">
        <el-form-item label="仓库名称" prop="name">
          <el-input v-model="diaLogForm.name"></el-input>
        </el-form-item>
        <el-form-item label="仓位名称" prop="stockIds">
          <el-select v-model="diaLogForm.stockIds" multiple filterable placeholder="请选择">
            <el-option v-for="item in cList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存警戒线" prop="stockCordon">
          <el-input v-model="diaLogForm.stockCordon"></el-input>
        </el-form-item>
        <!-- <el-form-item label="库存状态">
          <el-radio v-model="diaLogForm.status" label="1">充足</el-radio>
          <el-radio v-model="diaLogForm.status" label="2">补充足</el-radio>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="diaLogForm.remark"></el-input>
        </el-form-item>
        <el-form-item label="虚拟仓" prop="type">
          <el-radio v-model="diaLogForm.type" label="2">是</el-radio>
          <el-radio v-model="diaLogForm.type" label="1">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="hanleInfo">确 定</el-button>
      </div>
    </el-dialog>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'
import {
  addStock,
  deleteStock,
  getList,
  getListStockV2,
  updateStock,
  exportListStockV2,
  getStockDetailStock
} from '@/api/basic_data/warehouse'
import { nextTick } from 'process'
export default {
  components: { TemplatePage, Popu },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.newData()
              }),
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async () => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                let ids = this.recordSelected.map(v => {
                  return v.id
                })
                let params = ids
                await deleteStock({ id: params.join(',') })
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              },
              isRole: this.$checkBtnRole('del', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      options: [
        {
          value: '选项1',
          label: '1'
        },
        {
          value: '选项2',
          label: '2'
        },
        {
          value: '选项3',
          label: '3'
        },
        {
          value: '选项4',
          label: '4'
        },
        {
          value: '选项5',
          label: '5'
        }
      ],
      diaLogForm: {
        id: null,
        type: '1',
        name: '',
        remark: '',
        status: 1,
        stockCordon: 0,
        stockIds: [],
        updateBy: '',
        updateTime: ''
      },
      showDialogForm: false,
      screenForm: {
        name: '',
        stockName: ''
      },
      screenForm2: {
        // 筛选表单数据
        name: '' // 名称
      },
      type: null,
      cList: {}
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getListStockV2(...p)
    },
    // 列表导出函数
    exportList: exportListStockV2,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {this.$checkBtnRole('edit', this.$route.meta.roles) ? (
              <el-button
                size="mini"
                type="text"
                onClick={() => {
                  this.editFn(row.id)
                }}
              >
                编辑
              </el-button>
            ) : null}
            {this.$checkBtnRole('del', this.$route.meta.roles) ? (
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                onOnConfirm={() => {
                  this.hanleDelete(row.id)
                }}
              >
                <el-button  size="mini" slot="reference" type="text" class="textColor el-popover-left">
                  删除
                </el-button>
              </el-popconfirm>
            ) : null}
          </div>
        )
      }
    },

    onClose() {
      this.diaLogForm = {
        id: null,
        type: '1',
        name: '',
        remark: '',
        status: 1,
        stockCordon: 0,
        stockIds: [],
        updateBy: '',
        updateTime: ''
      }
      this.addOff(() => {
        this.showDialogForm = false
      })()
    },
    newData() {
      this.showDialogForm = true
      this.type = 1
      const params = {
        pageNum: 1,
        pageSize: -1,
        name: ''
      }
      getList(params).then(res => {
        this.cList = res.data.records
        this.listTotal = res.data.total
      })
    },
    //显示编辑，编辑数据初始化
    editFn(id) {
      let row
      getStockDetailStock({ id }).then(res => {
        row = res.data
        this.type = 2
        const arr = []
        if (row.kingDeeStocks && row.kingDeeStocks.length > 0) {
          row.kingDeeStocks.forEach(el => {
            arr.push(el.id)
          })
        }
        this.newData()
        this.diaLogForm = {
          id,
          name: row.name,
          remark: row.remark,
          status: row.status,
          stockCordon: row.stockCordon,
          type: row.type + '',
          stockIds: row.stockIds === undefined ? arr : row.stockIds
        }
        this.showDialogForm = true
      })
    },

    hanleInfo() {
      console.log(this.type)
      if (this.type == 1) {
        addStock(this.diaLogForm).then(res => {
          this.$successMsg('保存成功')
          this.showDialogForm = false
          this.$refs.pageRef.refreshList()
        })
      } else if (this.type == 2) {
        const params = {
          ...this.diaLogForm
        }
        updateStock(params).then(res => {
          this.$successMsg('编辑成功')
          this.showDialogForm = false
          this.$refs.pageRef.refreshList()
        })
      }
      this.diaLogForm = {
        name: '',
        remark: '',
        type: '1',
        status: 0,
        stockCordon: 0,
        stockIds: [],
        updateBy: '',
        updateTime: ''
      }
    },
    // 删除数据
    hanleDelete(id) {
      deleteStock({ id: [id].join('') }).then(res => {
        this.$successMsg('删除成功')
        this.$refs.pageRef.refreshList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-date-editor {
  width: 100%;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-col-9 .el-button {
  padding: 5px;
}
::v-deep .el-dialog__header {
  background-color: #dddddd;
}
.base {
  padding: 20px 20px 0;
}
.table {
  margin-top: 12px;
}
.right {
  float: right;
}
</style>
