<!--物料名称和物料种类关联-->
<template>
  <div class="systemBase">
    <el-form
      ref="searchData1"
      :inline="true"
      :model="searchMForm"
      style="padding: 8px 0 0 12px"
      label-width="70px"
    >
      <el-form-item label="物料名称">
        <el-input
          type="text"
          size="mini"
          v-model="searchMForm.matterName"
        ></el-input>
      </el-form-item>
      <el-form-item label-width="10px">
        <el-button type="primary" size="mini" @click="getMatterNameItem(1)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <el-row>
      <el-col :span="18">
        <div style="border: 1px solid #d5d5d5; padding: 10px 0 10px 10px">
          <div style="font-size: 17px">物料数据</div>
        </div>
        <div class="common-table-div">
          <el-table
            v-loading="dataLoading2"
            ref="dataTable"
            highlight-current-row
            border
            :height="tableHeight"
            @selection-change="handleSelectionChange2"
            :data="table.list2"
            :icore-filter-flag="icoreFilterFlag"
          >
            <el-table-column
              type="index"
              label="序号"
              fixed="left"
              align="center"
            ></el-table-column>
            <el-table-column
              type="selection"
              width="55"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100"
              prop="matterNo"
              label="物料编码"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              prop="matterName"
              min-width="300"
              label="物料名称"
              align="left"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100"
              prop="meterCode"
              label="计量码"
              align="left"
            ></el-table-column>
          </el-table>
        </div>
        <div class="common-foot-style">
          <el-pagination
            v-loading="dataLoading2"
            @size-change="pageSizeChange2"
            @current-change="pageCurrentChange2"
            :current-page="page2.pageNum"
            :page-sizes="[100, 500, 1000]"
            :page-size="page2.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page2.totalPage"
          >
          </el-pagination>
        </div>
      </el-col>
      <el-col :span="6">
        <div
          style="
            border: 1px solid #d5d5d5;
            padding: 10px 0 10px 10px;
            border-left: none;
          "
        >
          <div style="font-size: 17px">历史皮重类型数据</div>
        </div>
        <div class="common-table-div">
          <el-table
            v-loading="dataLoading"
            @row-click="rowChange"
            ref="dataTable"
            highlight-current-row
            border
            :height="tableHeight"
            :data="table.list"
            :icore-filter-flag="icoreFilterFlag"
          >
            <el-table-column
              type="index"
              label="序号"
              fixed="left"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100"
              prop="baseCode"
              label="类型编码"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              prop="baseName"
              min-width="300"
              label="类型名称"
              align="left"
            ></el-table-column>
          </el-table>
        </div>
        <div class="common-foot-style">
          <el-pagination
            v-loading="dataLoading"
            @size-change="pageSizeChange"
            @current-change="pageCurrentChange"
            :current-page="page.pageNum"
            :page-sizes="[100, 500, 1000]"
            :page-size="page.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page.totalPage"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <div style="border-right: 1px solid #d5d5d5">
          <div class="common-title-div">
            <el-form :inline="true">
              <el-form-item label-width="80px">历史皮重配置明细</el-form-item>
              <el-form-item label="历史皮重类型">
                <el-select v-model="searchForm.mainTypeName" clearable size="mini">
                  <el-option
                    v-for="item in table.list"
                    :key="item.baseCode"
                    :label="item.baseName"
                    :value="item.baseCode"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="物料名称">
                <el-input
                  type="text"
                  size="mini"
                  v-model="searchForm.countTypeName"
                ></el-input>
              </el-form-item>
              <el-form-item label-width="20px">
                <el-button
                  icon="el-icon-circle-plus-outline"
                  type="primary"
                  size="mini"
                  @click="getLinkData(1)"
                  >查询</el-button
                >
              </el-form-item>
              <el-form-item label-width="20px">
                <el-button
                  icon="el-icon-circle-plus-outline"
                  type="primary"
                  size="mini"
                  @click="addData"
                  v-privilege="activeMenu + 'RELATION'"
                  >关联</el-button
                >
              </el-form-item>
              <el-form-item label-width="20px">
                <el-button
                  size="mini"
                  type="danger"
                  plain
                  icon="el-icon-remove-outline"
                  @click="abolishData"
                  v-privilege="activeMenu + 'RELEASE'"
                  >解除</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
        <div class="common-table-div">
          <el-table
            v-loading="dataLoading3"
            ref="dataTable"
            highlight-current-row
            border
            :height="tableHeight"
            @selection-change="handleSelectionChange3"
            :data="table.list3"
            :icore-filter-flag="icoreFilterFlag"
          >
            <el-table-column
              type="index"
              label="序号"
              fixed="left"
              align="center"
            ></el-table-column>
            <el-table-column
              type="selection"
              width="55"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100"
              prop="mainTypeNo"
              label="历史皮重类型编号"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="100"
              prop="mainTypeName"
              label="历史皮重类型"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              prop="countTypeNo"
              min-width="300"
              label="物料编码"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              prop="countTypeName"
              min-width="300"
              label="物料名称"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              min-width="80"
              prop="createManName"
              label="创建人"
              align="center"
            ></el-table-column>
            <el-table-column
              sortable
              :show-overflow-tooltip="true"
              prop="createTime"
              label="创建时间"
              align="center"
              min-width="140"
            ></el-table-column>
          </el-table>
        </div>
        <div class="common-foot-style">
          <el-pagination
            v-loading="dataLoading3"
            @size-change="pageSizeChange3"
            @current-change="pageCurrentChange3"
            :current-page="page3.pageNum"
            :page-sizes="[100, 500, 1000]"
            :page-size="page3.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page3.totalPage"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getCookie, formatDate } from '@/utils/util.js';
import { cookieUserId, cookieUserName } from '@/config/config.js'

export default {
  data () {
    return {
      icoreFilterFlag: true,
      dataLoading: false,
      dataLoading2: false,
      dataLoading3: false,
      multipleSelection: {},
      multipleSelection2: [],
      multipleSelection3: [],
      table: {
        list: [],
        list2: [],
        list3: []
      },
      searchForm: {
        countTypeName: '',
        mainTypeName: ''
      },
      searchMForm: {
        matterName: ''
      },
      page: {
        pageSize: 100,
        pageNum: 1,
        totalPage: 0
      },
      page2: {
        pageSize: 100,
        pageNum: 1,
        totalPage: 0
      },
      page3: {
        pageSize: 100,
        pageNum: 1,
        totalPage: 0
      },
      activeMenu: '',
      orgTree: [],
      multipleSelection1: [],
      selectedData: {},
      userName: '',
      userId: ''
    }
  },
  async created () {
    this.tableHeight = (window.innerHeight - 290) / 2 > 250 ? (window.innerHeight - 290) / 2 : 250
    this.activeMenu = window.top.localStorage.getItem('activeMenu')
    await this.init()
  },
  mounted () {
    this.searchDatas();
    this.userName = getCookie(cookieUserName);
    this.userId = getCookie(cookieUserId);
  },
  methods: {
    async init () {
      // 查询所有机构信息数据
      const data = await this.axios.get('pass/v1/sysorgs/listtree?companyId=713710108567277568')
      this.orgTree = data.data
    },
    // 查询可用历史皮重类型数据
    getTareType (flag) {
      if (flag === 1) {
        this.page.pageNum = 1
      }
      this.axios.get('pass/systemBase/v1/combaseinfos/?pageNum=1&pageSize=500&pBaseCode=001033')
        .then(res => {
          this.table.list = [...res.data.list];
        })
    },
    // 分别查询三张表的数据
    async searchDatas () {
      this.getTareType(1)
      this.getMatterNameItem(1)
      // 历史皮重类型和物料数据关联数据
      this.getLinkData(1)
    },
    // 分页查询物料数据
    getMatterNameItem (flag) {
      if (flag === 1) {
        this.page2.pageNum = 1
      }
      this.axios.get('pass/systemBase/v1/meterbasematterinfos/like/' + `?validFlag=1&pageSize=${this.page2.pageSize}&pageNum=${this.page2.pageNum}&matterName=${this.searchMForm.matterName}`)
        .then(res => {
          if (res && res.data && res.data.list.length > 0) {
            this.table.list2 = [...res.data.list]
            this.page2.totalPage = res.data.total
          } else {
            this.table.list2 = []
            this.page2.totalPage = res.data.total
          }
        })
    },
    async getLinkData (flag) {
      if (flag === 1) {
        this.page3.pageNum = 1
      }
      const res = await this.axios.get(`pass/systemBase/v1/meterbasehistarecountds/like/?pageNum=${this.page3.pageNum}&pageSize=${this.page3.pageSize}&countTypeName=${this.searchForm.countTypeName}&mainTypeName=${this.searchForm.mainTypeName}`)
      if (res && res.data && res.data.list.length > 0) {
        this.table.list3 = [...res.data.list]
        this.page3.totalPage = res.data.total
      } else {
        this.table.list3 = []
        this.page3.totalPage = res.data.total
      }
    },
    rowChange (val) {
      this.multipleSelection = val
    },
    // 物料数据表格多选回调
    handleSelectionChange2 (val) {
      this.multipleSelection2 = val
    },
    // 关联数据多选回调
    handleSelectionChange3 (val) {
      this.multipleSelection3 = val
    },
    // 添加关联数据
    addData () {
      if (JSON.stringify(this.multipleSelection) === '{}') {
        this.$message.warning('请点选历史皮重类型数据！');
        return;
      }
      if (this.multipleSelection2.length < 1) {
        this.$message.warning('请勾选物料数据！');
        return;
      }
      let linkList = []
      // 将勾选的物料数据和历史皮重类型数据组合起来
      this.multipleSelection2.forEach(iItem => {
        iItem.id = ''
        iItem.countTypeName = this.multipleSelection.matterName
        iItem.countTypeNo = this.multipleSelection.matterName
        iItem.countNo = formatDate(new Date(), 'yyyyMMddHHAASS') + (Math.random() * 100000 + '')
        iItem.createManName = this.userName
        iItem.createManNo = this.userId
      })
      this.axios.post('pass/systemBase/v1/meterbasehistarecountds/', this.multipleSelection2, { individualType: 'json' }).then(res => {
        if (res.succeed) {
          this.$message.success(res.message);
          this.searchDatas()
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    // 作废数据
    abolishData () {
      let ids = ''
      if (this.multipleSelection3.length < 1) {
        this.$message.warning('请选择要解除的卸货配置！');
        return;
      }
      // 将所有选中的关联数据的id拼接成一个由逗号分隔的字符串
      this.multipleSelection3.forEach((item, index) => {
        if (index !== this.multipleSelection3.length - 1) {
          ids += item.id + ','
        } else {
          ids += item.id
        }
      })
      this.axios.delete(`pass/systemBase/v1/meterbasehistarecountds/${ids}`).then(res => {
        // console.log(res, '删除的返回结果')
        if (res.succeed) {
          this.$message.success(res.message);
          this.searchDatas()
        } else {
          this.$message.error(res.message)
        }
      }).catch(e => {
        this.$message.error(e.message)
      })
    },
    // 页大小变化回调
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.searchUserData(1);
    },
    // 页跳转变化回调
    pageCurrentChange (index) {
      this.page.pageNum = index;
      this.searchUserData();
    },
    // 页大小变化回调
    pageSizeChange2 (size) {
      this.page2.pageSize = size;
      this.searchUnloadData(1);
    },
    // 页跳转变化回调
    pageCurrentChange2 (index) {
      this.page2.pageNum = index;
      this.searchUnloadData();
    },
    // 页大小变化回调
    pageSizeChange3 (size) {
      this.page2.pageSize = size;
      this.getUnloadRoleData(1);
    },
    // 页跳转变化回调
    pageCurrentChange3 (index) {
      this.page2.pageNum = index;
      this.getUnloadRoleData();
    }
  }
}
</script>
<style scoped>
</style>