<template>
  <el-dialog
    :title="$t('产品分类选择')"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="true"
    width="80%"
    append-to-body
  >
    <el-row :gutter="20" style="max-height: 600px; overflow-y: auto;">
      <!--分类数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="itemTypeName"
           
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="itemTypeOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--物料数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
         <!-- <el-form-item label="排序" prop="orderNum">
            <el-input
              v-model="queryParams.orderNum"
             
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
           <el-form-item label="物料/产品" prop="itemOrProduct">
            <el-input
              v-model="queryParams.itemOrProduct"
             
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >{{ $t('common.search') }}</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >{{ $t('common.reset') }}</el-button
            >
          </el-form-item>  -->
        </el-form>
        <el-table
          v-loading="loading"
          :data="itemList"
          @current-change="handleCurrent"
          @row-dblclick="handleRowDbClick"
        >
          <el-table-column width="50" align="center">
            <template v-slot="scope">
              <el-radio
                v-model="selectedItemId"
                :label="scope.row.itemTypeId"
                @change="handleRowChange(scope.row)"
                >{{ "" }}</el-radio
              >
            </template>
          </el-table-column>
      <el-table-column
            :label="$t('分类')"
            align="left"
            key="itemTypeName"
            prop="itemTypeName"
            v-if="columns[0].visible"
            :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
                :label="$t('排序')"
            align="center"
            key="orderNum"
            prop="orderNum"
            v-if="columns[1].visible"
            :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
            :label="$t('物料/产品')"
            align="center"
            key="itemOrProduct"
            prop="itemOrProduct"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <dict-tag
                :options="dict.type.mes_item_product"
                :value="scope.row.itemOrProduct"
              />
            </template>
          </el-table-column>

        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">{{ $t('common.confirm') }}</el-button>
      <el-button @click="showFlag = false">{{ $t('common.cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>



import { treeselect,listItemType } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "MdItemSingle",
  dicts: ["sys_yes_no", "mes_item_product"],
  components: { Treeselect },
  data() {
    return {
      showFlag: false,
      // 选中数组
      selectedItemId: undefined,
      selectedRows: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,
      // 部门树选项
      itemTypeOptions: undefined,
      // 部门名称
      itemTypeName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        orderNum: null,
        itemTypeName: null,
        itemOrProduct: null
      },
          // 列信息
      columns: [
        { key: 0, label: this.$t('物料分类'), visible: true },
        { key: 1, label: this.$t('物料分类排序'), visible: true },
        { key: 2, label: this.$t('物料/产品'), visible: true },
      ],

    };
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
     listItemType(this.queryParams).then(response => {
        this.itemList = response.data;
        // this.total = response.total;

        this.loading = false;
      });
    },
    /** 查询分类下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.itemTypeOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.itemTypeId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
       this.form = {
        itemTypeName: null,
        orderNum: null,
        itemOrProduct: null
        };
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      debugger;
      if (row) {
        this.selectedRows = row;
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedItemId == null || this.selectedItemId == 0) {
        this.$notify({
          title: this.$t('common.sys_tip'),
          type: "warning",
          message: this.$t('请至少选择一条数据!'),
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
