<template>
  <el-dialog
    :title="$t('请选择任务单')"
    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="$t('任务单编码')" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('生产工单')" prop="workOrderCode">
        <el-input
          v-model="queryParams.workOrderCode"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('编排单号')" prop="arrangeCode">
        <el-input
          v-model="queryParams.arrangeCode"
         
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('common.status')" prop="statusArr">
        <el-select
          v-model="queryParams.statusArr"
          clearable
          multiple
         
        >
          <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"
          >{{ $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"
      @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.taskWorkunitId"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('任务单编码')"
        width="110"
        align="center"
        key="taskCode"
        prop="taskCode"
      />
      <!-- <el-table-column
        label="排产数量"
        min-width="100"
        align="center"
        key="quantity"
        prop="quantity"
      /> -->
      <el-table-column
        :label="$t('单位')"
        align="center"
        width="80"
        key="unitOfMeasure"
        prop="unitOfMeasure"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('排产数量')"
        align="center"
        key="quantity"
        width="100"
        prop="quantity"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        :label="$t('开始生产时间')"
        align="center"
        prop="startTime"
        width="180"
        :show-overflow-tooltip="true"
      >
      
      </el-table-column>
      <el-table-column
        :label="$t('结束生产时间')"
        align="center"
        prop="endTime"
        width="180"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('common.status')" align="center" prop="statusName" />
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('common.workunit_name')" align="center" prop="workunitName" />
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('common.workunit_code')" align="center" prop="workunitCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('工站编码')" align="center" prop="workstationCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('工站名称')" align="center" prop="workstationName" />
      <el-table-column :show-overflow-tooltip="true" min-width="150" :label="$t('工序编码')" align="center" prop="processCode" />
      <el-table-column :show-overflow-tooltip="true" width="100" :label="$t('工序名称')" 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">{{ $t('common.confirm') }}</el-button>
      <el-button @click="showFlag = false">{{ $t('common.cancel') }}</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,
        // statusArr: '',
        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.taskWorkunitId
      }
    },
    //确定选中
    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>
