<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"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="任务单编码" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务单编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编排单号" prop="arrangeCode">
        <el-input v-model="queryParams.arrangeCode" placeholder="请输入编排单号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="statusArr">
        <el-select
          v-model="queryParams.statusArr"
          clearable
          multiple
          placeholder="请选择入库方式"
        >
          <el-option
            v-for="dict in dict.type.TASK_STATUS"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </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="50" align="center">
        <template v-slot="scope">
          <!-- <el-radio
            v-model="selectedItemId"
            :label="scope.row.itemId"
            @change="handleRowChange(scope.row)"
            >{{ "" }}</el-radio
          > -->
          <!-- 可以手动的修改label的值，从而控制选择哪一项 -->
          <el-radio class="radio" v-model="selectedItemId" :label="scope.row.itemId + scope.row.processId + scope.row.taskId + scope.row.taskWorkunitId" @change="handleRowChange(scope.row)"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column
        label="任务单编码"
        width="110"
        align="center"
        key="taskCode"
        prop="taskCode"
      />
      <el-table-column
        label="编排单号"
        min-width="100"
        align="center"
        key="arrangeCode"
        prop="arrangeCode"
      />
      <el-table-column
        label="单位"
        align="center"
        width="80"
        key="unitOfMeasure"
        prop="unitOfMeasure"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="排产数量"
        align="center"
        key="quantity"
        width="100"
        prop="quantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="开始生产时间"
        align="center"
        prop="startTime"
        width="180"
        :show-overflow-tooltip="true"
      >
      
      </el-table-column>
      <el-table-column
        label="结束生产时间"
        align="center"
        prop="endTime"
        width="180"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" width="100" label="状态" align="center" prop="statusName" />
      <el-table-column :show-overflow-tooltip="true" width="100" label="工作单元名称" align="center" prop="workunitName" />
      <el-table-column :show-overflow-tooltip="true" width="100" label="工作单元编码" align="center" prop="workunitCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" label="工站编码" align="center" prop="workstationCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" label="工站名称" align="center" prop="workstationName" />
      <el-table-column :show-overflow-tooltip="true" min-width="150" label="工序编码" align="center" prop="processCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" label="工序名称" align="center" prop="processName" />
    </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">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listTaskListDetail } from "@/api/mes/pro/protask";
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: ["TASK_STATUS"],
  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: {
        pageNum: 1,
        pageSize: 10,
        arrangeCode: '',
        workorderType: 'SELF',
        // statusStr: 'PREPARE,BEGINNING,ISSUEGENERATED'
      },
    };
  },
  watch: {
    // 根据名称筛选分类树
    itemTypeName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {

  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 查询物料编码列表 */
    getList() {
      this.loading = true;
      listTaskListDetail(this.queryParams).then((response) => {
        this.itemList = response.rows;
        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;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
        this.selectedItemId = row.itemId + row.processId + row.taskId + row.taskWorkunitId
      }
    },
    //确定选中
    confirmSelect() {
      if (this.selectedItemId == null || this.selectedItemId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
