<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <Collapse :is-collapse="isCollapse" :screen-form="screenForm">
        <template #right_btn>
          <el-button size="mini" @click="resetScreenForm">清空</el-button>
          <el-button size="mini" type="primary" @click="submitScreenForm">搜索</el-button>
        </template>
        <template #search>
          <el-form ref="screenForm" :model="screenForm" label-width="70px" size="mini" label-position="left">
            <el-row :gutter="20">
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="仓库名称" prop="name">
                  <el-input placeholder="请输入仓库名称" v-model="screenForm.name"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :lg="6">
                <el-form-item label="仓位名称" prop="stockName">
                  <el-input placeholder="请输入仓位名称" v-model="screenForm.stockName"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>
      </Collapse>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="newData(), (type = 1)"
          v-if="$checkBtnRole('add', $route.meta.roles)"
          >新增</el-button
        >
        <el-popconfirm
          confirm-button-text="好的"
          cancel-button-text="不用了"
          icon="el-icon-info"
          icon-color="red"
          title="内容确定删除吗？"
          @onConfirm="handleDeleteAll"
          v-if="$checkBtnRole('del', $route.meta.roles)"
        >
          <el-button slot="reference" type="primary" icon="el-icon-delete" size="mini" class="textColor el-popover-left"
            >批量删除</el-button
          >
        </el-popconfirm>
      </div>
    </div>
    <div class="mymain-container">
      <!-- 列表 -->
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          @select-all="hanleSelectAll"
          @select="hanleSelectAll"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column align="left" type="selection" width="55"> </el-table-column>
          <el-table-column
            align="left"
            label="仓库名称"
            prop="name"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="left" label="仓位名称" prop="kingDeeStocks" min-width="400" show-overflow-tooltip>
            <template slot-scope="scope">
              <template v-if="scope.row.kingDeeStocks.length > 0">
                <el-tag size="mini" type="danger" style="margin-right: 10px" v-for="item in scope.row.kingDeeStocks">
                  {{ item.name }}
                </el-tag>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="库存警戒线"
            prop="stockCordon"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <!-- <el-table-column
            align="left"
            label="库存充足"
            prop="status"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status === 1">充足</el-tag>
              <el-tag type="danger" v-else>不充足</el-tag>
            </template>
          </el-table-column> -->
          <el-table-column
            align="left"
            label="创建人"
            prop="createBy"
            min-width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="创建时间"
            prop="createTime"
            min-width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="left"
            label="更新人"
            prop="updateBy"
            min-width="200"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column align="left" label="更新时间" prop="updateTime" min-width="200" show-overflow-tooltip>
          </el-table-column>

          <el-table-column align="center" fixed="right" label="操作" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <!-- <el-button
                type="text"
                class="textColor"
                @click="hanleDetail(scope.row)"
                >详情</el-button
              > -->
              <el-button
                v-if="$checkBtnRole('edit', $route.meta.roles)"
                type="text"
                class="textColor"
                @click="editFn(scope.row.id, scope.row), (type = 2)"
                >编辑</el-button
              >
              <el-popconfirm
                v-if="$checkBtnRole('del', $route.meta.roles)"
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                @onConfirm="hanleDelete(scope.row.id)"
              >
                <el-button slot="reference" type="text" class="textColor el-popover-left">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>

    <!-- 弹窗 -->
    <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="仓库名称">
          <el-input v-model="diaLogForm.name"></el-input>
        </el-form-item>
        <el-form-item label="仓位名称">
          <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="库存警戒线">
          <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="备注">
          <el-input type="textarea" v-model="diaLogForm.remark"></el-input>
        </el-form-item>
        <el-form-item label="虚拟仓">
          <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="showDialogForm = false">取 消</el-button>
        <el-button type="primary" @click="hanleInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Mixin from '@/mixin/index'
import { addStock, deleteStock, getList, getListStock, updateStock } from '@/api/basic_data/warehouse'

export default {
  mixins: [Mixin],
  data() {
    return {
      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: {},
      isCollapse: true
    }
  },
  methods: {
    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, row) {
      this.type = 2
      const arr = []
      if (row.kingDeeStocks.length > 0) {
        row.kingDeeStocks.forEach(el => {
          arr.push(el.id)
        })
      }
      this.newData()
      console.log(row.type, '444')
      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.getList()
        })
      } else if (this.type == 2) {
        const params = {
          ...this.diaLogForm
        }
        updateStock(params).then(res => {
          this.$successMsg('编辑成功')
          this.showDialogForm = false
          this.getList()
        })
      }
      this.diaLogForm = {
        name: '',
        remark: '',
        type: '1',
        status: 0,
        stockCordon: 0,
        stockIds: [],
        updateBy: '',
        updateTime: ''
      }
    },
    getList() {
      this.listLoading = true
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        name: this.screenForm.name,
        stockName: this.screenForm.stockName
      }
      getListStock(params).then(res => {
        this.dataList = res.data.records
        this.listTotal = res.data.total
        this.listLoading = false
        console.log(this.dataList, 1233)
      })
    },
    // 删除数据
    hanleDelete(id) {
      deleteStock({ id: [id].join('') }).then(res => {
        this.$successMsg('删除成功')
        this.getList()
      })
    },
    handleDeleteAll() {
      console.log()
      deleteStock({ id: this.ids.join(',') }).then(res => {
        this.$successMsg('删除成功')
        this.getList()
      })
    }
  }
}
</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>
