<template>
  <el-dialog
    title="请选择台账信息"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="true"
    width="80%"
    append-to-body
    :destroy-on-close="true"
    :close-on-click-modal="false"
  >
    <!-- <el-form :model="queryParams" @submit.native.prevent ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="工作单元名称" prop="workunitName">
        <el-input
          v-model="queryParams.taskid"
          placeholder="请输入工作单元名称"
          style="width:220px;"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->
    <el-table
      v-loading="loading"
      :data="itemList"
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick"
      @row-click="handleRowChange"
    >
      <el-table-column width="55" align="center">
        <template v-slot="scope">
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio class="radio" :disabled="scope.row.disabled" v-model="selectedItemId" :label="scope.row.toolId"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column  label="物料名称" align="center" prop="itemName" />
      <el-table-column label="SN号" align="center" prop="toolCode" />
      <el-table-column label="剩余寿命" align="center" prop="lifeTime" />
      <!-- <el-table-column label="型号" align="center" prop="spec" /> -->
      <el-table-column label="位置" align="center" prop="location" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mes_tool_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false, visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getTmToolSnList } from "@/api/mes/tm/tmToolRequestUse";
import { genCode } from "@/api/system/autocode/rule";
import { getToken } from "@/utils/auth";
import { treeselect } from "@/api/mes/md/itemtype";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "MdItemSingle",
  dicts: ["mes_tool_status"],
  components: { Treeselect },
  props: {
    id: {
      type: [Number||String]
    },
    data: {
      type: Array
    }
  },
  data() {
    return {
      selectedItemId: '',
      showFlag: false,
      // 选中数组
      ids: [],
      selectedRows: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料产品表格数据
      itemList: null,

      lifetime: null,
      // 部门树选项
      itemTypeOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        lifeTimeStatus: true,
        status: 'STORE'
      },
    };
  },
  watch: {
    // 根据名称筛选分类树
    showFlag: {
      handler (newName, oldName) {
        if (newName) {
          this.selectedItemId = ''
          this.getList();
        }
      },
      immediate: true
    }
  },
  created() {

  },
  mounted() {

  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      this.queryParams.itemId = this.id
      const ids = this.data.flatMap(it => it.isCheck)
      getTmToolSnList(this.queryParams).then((response) => {
        this.itemList = []
        response.rows.forEach(item => {
          ids.forEach(el => {
            if (item.toolId === el) {
              this.$set(item, 'disabled', true)
            }
          });
          this.itemList.push(item)
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
     // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
        this.selectedItemId = row.taskId;
        this.lifetime = row.lifeTime;
      }
    },
     // 单选选中数据
    handleRowDbClick(row) {
      if (row.disabled) {
        return
      }
      if (row) {
        this.selectedRows = row;
        this.selectedItemId = row.taskId
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect(){
      if(this.lifetime<0){
        const materialRequestIds = this.lifetime
      this.$modal.confirm('当前刀模板具寿命为"' + materialRequestIds + '",确定选择吗？').then(function () {
        return;
      }).then(() => {
        
      }).catch(() => { 
        this.showFlag=true;
      });
      }
      if (!this.selectedItemId) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });

        return;
      }

      console.log(this.selectedRows)
      this.$emit('onSelected',this.selectedRows);
      this.showFlag = false;
    }
  },
};
</script>
