<template>
  <div class="materialInformation">
    <div class="left-tree">
      <el-tree
        accordion
        style="height: 100%; overflow-y: scroll"
        :data="dataList"
        :props="defaultProps"
        v-loading="treeing"
        @node-click="handleNodeClick"
      ></el-tree>
    </div>
    <div class="common-page-shadow">
      <div
        class="common-search-container"
        ref="contentPanel"
        :style="isExpend ? {height: '160px'} : ''"
      >
        <el-form
          ref="form"
          label-position="right "
          :inline="true"
          type="flex"
          justify="left"
        >
          <el-form-item label="平台物料编号">
            <el-input
              size="small"
              v-model="querys[5].value"
              clearable
              style="width: 120px"
              placeholder="平台物料编号"
            ></el-input>
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input
              size="small"
              style="width: 120px"
              v-model="querys[0].value"
              clearable
              placeholder="物料名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="规格型号">
            <el-input
              size="small"
              style="width: 120px"
              v-model="querys[1].value"
              clearable
              placeholder="规格型号"
            ></el-input>
          </el-form-item>
          <el-form-item label="物料状态">
            <dict-select
              dictCode="wlzt"
              :hasDefaultOption="true"
              clearable
              size="small"
              style="width: 130px"
              v-model="querys[4].value"
              placeholder="请选择是否集中采购"
            />
          </el-form-item>
        </el-form>
        <div class="container-btn">
          <el-button
            size="small"
            icon="el-icon-search"
            type="danger"
            class="btn btn-plus"
            @click="handleSearch"
          >
            搜索
          </el-button>
          <el-button
            size="small"
            icon="el-icon-refresh-right"
            class="btn btn-plus"
            @click="clearSearchForm"
          >
            重置
          </el-button>
          <el-link
            :underline="false"
            class="link-more-search"
            @click="handleMoreSearch"
          >
            {{ isExpend ? '收起搜索' : '更多搜索' }}
          </el-link>
        </div>
      </div>
      <el-row
        class="common-table-container flex searchPanel"
        :style="SearchPanel"
      >
        <el-col :span="24" class="common-table-wrapper">
          <div class="common-btn-group">
            <!-- <el-button
                size="small"
                icon="el-icon-plus"
                type="primary"
                class="btn btn-plus"
                @click="handleAdd"
              >
                新增
              </el-button> -->
            <!-- <el-button
                :disabled="handleDeleteActive"
                size="small"
                type="danger"
                icon="el-icon-delete"
                @click="handleDelete"
              >
                删除
              </el-button> -->
            <el-button
              size="small"
              icon="el-icon-thumb"
              type="primary"
              plain
              :disabled="handleDeleteActive"
              class="btn btn-plus"
              @click="handleEdit"
            >
              批量修改物料分类
            </el-button>
            <el-button
              size="small"
              icon="el-icon-bottom"
              type="primary"
              plain
              :loading="exporting"
              class="btn btn-plus"
              @click="dataExport"
            >
              导出数据
            </el-button>
          </div>
          <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
          <div class="common-table-content btn-pagenation" ref="tableContent">
            <el-table
              :border="true"
              stripe
              fit
              size="mini"
              :max-height="tableHeight"
              v-loading="tableLoading"
              ref="multipleSelection"
              :data="tableData"
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" min-width="20%" align="center">
              </el-table-column>
              <el-table-column
                prop="matCategory"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="物料分类"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matPlatcode"
                show-overflow-tooltip
                min-width="110px"
                align="center"
                label="平台物料编号"
                resizable
              >
              </el-table-column>

              <el-table-column
                prop="matName"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="物料名称"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matMaterial"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="物料牌号"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matSpec"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="规格型号"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matUnit"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="计量单位"
                resizable
              >
              </el-table-column>

              <el-table-column
                prop="matBrand"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="品牌"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matBaseExt"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="基础扩展"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matOtherExt"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="其他扩展"
                resizable
              >
              </el-table-column>

              <el-table-column
                prop="matTaxrate"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="税率（%）"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="matIfPlat"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="是否集采"
                resizable
              >
                <template v-slot="{row}">
                  {{ getDictName('cglx-ywlx', row.matIfPlat) }}
                </template>
              </el-table-column>
              <el-table-column
                show-overflow-tooltip
                min-width="100px"
                align="center"
                fixed="right"
                label="状态"
                resizable
              >
                <template v-slot="{row}">
                  <el-switch
                    @change="(val) => switchChange(val, row)"
                    v-model="row.matStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-text="启用"
                    inactive-text="禁用"
                    :active-value="1"
                    :inactive-value="0"
                  >
                  </el-switch>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="common-pagenation">
            <el-pagination
              ref="paginationPanel"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="queryParam.pageBean.page"
              :page-sizes="[20, 30, 40, 50]"
              :page-size="queryParam.pageBean.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="queryParam.pageBean.total"
            >
            </el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog
      title="修改物料分类"
      :visible.sync="dialogVisible"
      width="width"
      :before-close="dialogBeforeClose"
    >
      <el-form :model="form">
        <el-form-item label="物料分类" label-width="80px">
          <el-cascader
            :props="{
              value: 'code',
              label: 'name',
              checkStrictly: true,
              emitPath: false,
            }"
            v-model="form.matNo"
            :options="treeData"
            style="width: 100%"
            @change="handleMatCatChange"
            ref="cascader"
            placeholder="请选择物料分类"
            clearable
            :show-all-levels="false"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="saveMating" @click="saveMat"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import basicData from '@/api/basicData'
import utils from '@/utils.js'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      treeData: [], //左边树
      defaultProps: {
        children: 'children',
        id: 'code',
        label: 'name',
      },
      tableHeight: 1000,
      tableData: [],
      dialogVisible: false,
      tableHeight: 1000,
      exporting: false,
      handleDeleteActive: true,
      isExpend: false,
      handleDeleteId: [],
      tableLoading: false,
      dataList: [],
      treeing:false,
      saveMating: false,
      form: {
        matNo: '',
      },
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
      },
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'matName',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'matSpec',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'RIGHT_LIKE',
          parentGroup: '',
          property: 'matCategory',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'matMaterial',
          relation: 'OR',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matStatus',
          relation: 'AND',
          value: '1',
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'matPlatcode',
          relation: 'AND',
          value: '',
        },
      ],
      currentUser: {},
    }
  },
  mounted() {
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.getTree()
    this.loadData()

    this.selectTree()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcTableHeight)
  },
  methods: {
    //导出数据
    dataExport() {
      let queryParam = {}
      queryParam.pageBean = {
        page: 0,
        pageSize: -1,
        showTotal: false,
      }
      queryParam.querys = this.queryParam.querys
      let portal = window.context.portal
      let query = {
        url: `${portal}/platMaterial/v1/exportExcelData`,
        method: 'post',
        data: queryParam,
        headers: {
          'Content-type': 'application/json',
        },
        responseType: 'blob', //将文件流转成blob对象
        noErrorMsg: true,
      }
      this.exporting = true
      this.$http
        .request(query)
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
            this.exporting = false
          } else {
            this.$message.error('导出失败')
            this.exporting = false
          }
        })
        .catch(() => {
          this.exporting = false
        })
    },
    // 动态计算表格的高度，自适应当前容器
    calcTableHeight() {
      this.tableHeight = this.$refs.tableContent.offsetHeight
    },
    //查询物料分类树结构
    selectTree() {
      this.treeing = true
      basicData.bizMaterialCategorySelectTree().then((res) => {
        this.treeing = false
        res.value.unshift({
          name: '全部',
          code: '',
        })
        this.dataList = []
        if (this.currentUser.org[0].orgCode == '00001513') {
          res.value.forEach((item) => {
            if (
              item.code == '01' ||
              item.code == '02' ||
              item.code == '03' ||
              item.code == '04' ||
              item.code == '07' ||
              item.code == ''
            ) {
              this.dataList.push(item)
            }
          })
        } else {
          res.value.forEach((item) => {
            if (
              item.code != '01' &&
              item.code != '02' &&
              item.code != '03' &&
              item.code != '04' &&
              item.code != '07'
            ) {
              this.dataList.push(item)
            }
          })
        }
      })
    },
    getTree() {
      basicData.bizMaterialCategorySelectTree().then((res) => {
        this.treeData = this.format(res.value)
        this.$nextTick(() => {
          this.assignValuesToTree(this.treeData, this.tableData)
        })
      })
    },
    format(list) {
      list.forEach((item) => {
        if (item.children.length == 0) {
          delete item.children
        } else {
          this.format(item.children)
        }
      })
      return list
    },
    //树形控价
    handleNodeClick(data) {
      this.querys[2].value = data.code
      this.loadData()
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    assignValuesToTree(nodes, objects) {
      nodes.forEach((node) => {
        objects.forEach((o) => {
          if (o.matCategory == node.code) {
            o.matCategory = node.name
          }
        })
        if (node.children) {
          this.assignValuesToTree(node.children, objects) // 递归处理子节点
        }
      })
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.querys[3].value = this.querys[1].value
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      if (this.querys[3].value) {
        this.queryParam.querys.push(this.querys[3])
      }
      if (this.querys[4].value) {
        this.queryParam.querys.push(this.querys[4])
      }
      if (this.querys[5].value) {
        this.queryParam.querys.push(this.querys[5])
      }
      basicData.bizMaterialQuery(this.queryParam).then((res) => {
        this.queryParam.pageBean.total = res.value.total
        this.tableLoading = false
        this.isExpend = false
        this.tableData = res.value.rows
        this.getTree()
        this.$nextTick(() => {
          this.assignValuesToTree(this.treeData, this.tableData)
        })
      })
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.querys[3].value = ''
      this.querys[4].value = '1'
      this.querys[5].value = ''
      this.queryParam.pageBean.page = 1
      this.loadData()
    },
    switchChange(val, row) {
      let statusValue = {
        ids: [row.id],
        status: val,
      }
      this.$http
        .post(
          '${portal}/platMaterial/v1/updateMatStatus?status=' +
            statusValue.status,
          statusValue.ids
        )
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.loadData()
          } else {
            this.loadData()
          }
        })
    },
    handleMatCatChange(val) {
      let node = this.$refs.cascader.getCheckedNodes()
      console.log(node, 'node')
      this.form.matNo = node[0].value
    },
    saveMat() {
      let form = {
        ids: this.handleDeleteId,
        categoryCode: this.form.matNo,
      }
      this.saveMating = true
      this.$http
        .post(
          '${portal}/bizMaterial/v1/updateMatCategory?categoryCode=' +
            this.form.matNo,
          form.ids
        )
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.loadData()
            this.dialogBeforeClose()
          }
          this.saveMating = false
        })
    },
    //添加
    handleAdd() {
      this.$router.push('informationAddDialog')
    },
    // 编辑
    handleEdit() {
      this.dialogVisible = true
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'informationDetail',
        query: {detailRows: rows, pageBean: this.queryParam},
      })
    },
    // 删除
    handleDelete() {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning',
      })
        .then(() => {
          basicData
            .bizMaterialRemove(this.handleDeleteId)
            .then((res) => {
              if (res.state) {
                this.$notify({
                  type: 'success',
                  message: '删除成功',
                  duration: 2000,
                })
                this.loadData()
              }
            })
            .catch((res) => {
              this.$notify({
                type: 'error',
                message: res.message,
                duration: 2000,
                offset: 80,
              })
            })
        })
        .catch(() => {})
    },

    // 顶部搜索
    handleSearch() {
      this.queryParam.pageBean.page = 1
      this.loadData()
    },
    handleSelectionChange(val) {
      if (val.length !== 0) {
        this.handleDeleteActive = false
        this.handleDeleteId = val.map((item) => {
          return item.id
        })
      } else {
        this.handleDeleteActive = true
      }
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    },
    handleMoreSearch() {
      this.isExpend = !this.isExpend
    },
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>
  
  <style lang="scss" scoped>
.materialInformation {
  display: flex;
  width: 100%;
  height: 100%;
  .common-page-shadow {
    width: calc(100% - 174px);
    height: calc(100% - 160px);
    box-sizing: border-box;
    .common-search-container {
      height: 80px;
    }
  }
  .left-tree {
    width: 200px;
    margin-right: 10px;
    padding: 10px;
    box-sizing: border-box;
    height: calc(100% - 80px);
    background-color: white;
    border-radius: 10px;
    box-shadow: 0 0.10417vw 0.625vw 0 rgba(0, 0, 0, 0.1);
  }
}

.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  // &:nth-child(2):hover {
  //   background-color: #f56c6c;
  //   color: #fff;
  //   border-color: #f56c6c;
  // }
  /deep/ i,
  span {
    font-size: 16px;
  }
}
/deep/.el-tree-node.is-current > .el-tree-node__content {
  background-color: #ebf3fa;
  color: #409eff;
  font-weight: bold;
}

/deep/ .el-tree-node__content {
  padding: 5px 0;
}

/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
.link-more-search {
  width: 68px;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
::v-deep .el-switch__core {
  width: 44px;
  height: 24px;
  border-radius: 100px;
  border: none;
}
::v-deep .el-switch__core::after {
  width: 20px;
  height: 20px;
  top: 2px;
}
::v-deep .el-switch.is-checked .el-switch__core::after {
  margin-left: -21px;
}
/*关闭时文字位置设置*/
::v-deep .el-switch__label--right {
  position: absolute;
  z-index: 1;
  right: 6px;
  margin-left: 0px;
  color: rgba(255, 255, 255, 0.9019607843137255);
  span {
    font-size: 12px;
  }
}
/* 激活时另一个文字消失 */
::v-deep .el-switch__label.is-active {
  display: none;
}
/*开启时文字位置设置*/
::v-deep .el-switch__label--left {
  position: absolute;
  z-index: 1;
  left: 5px;
  margin-right: 0px;
  color: rgba(255, 255, 255, 0.9019607843137255);
  span {
    font-size: 12px;
  }
}
</style>
  