<template>
  <el-dialog
    :title="$t('刀模版申请单选择')"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="80%"
    center
  >
    <el-row :gutter="20">
      <!--刀模版申请数据-->
      <el-col :span="20" :xs="20">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item :label="$t('作业单元')" prop="workunitName">
            <el-input
              v-model="queryParams.workunitName"
             
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item :label="$t('申请时间')" prop="requestDate">
            <el-date-picker
              clearable
              v-model="queryParams.requestDate"
              type="date"
              value-format="yyyy-MM-dd"
             
            >
            </el-date-picker>
          </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="toolRequestList"
          @current-change="handleCurrent"
          @row-dblclick="handleRowDbClick"
        >
          <el-table-column width="50" align="center">
            <template v-slot="scope">
              <el-radio
                v-model="selectedToolRequestId"
                :label="scope.row.toolRequestId"
                @change="handleRowChange(scope.row)"
                >{{ "" }}</el-radio
              >
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('申请单编码')"
            min-width="130"
            align="center"
            key="toolRequestCode"
            prop="toolRequestCode"
            v-if="columns[0].visible"
          >
          </el-table-column>

          <el-table-column
            :label="$t('工序')"
            min-width="120"
            align="center"
            key="processName"
            prop="processName"
            v-if="columns[1].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('作业单元')"
            width="120"
            align="center"
            key="workunitName"
            prop="workunitName"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            :label="$t('申请时间')"
            align="center"
            key="requestDate"
            prop="requestDate"
            v-if="columns[3].visible"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            :label="$t('预计开始时间')"
            align="center"
            key="startTime"
            prop="startTime"
            v-if="columns[4].visible"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            :label="$t('预计结束时间')"
            align="center"
            key="endTime"
            prop="endTime"
            v-if="columns[5].visible"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            :label="$t('申请数量')"
            align="center"
            key="requestNum"
            prop="requestNum"
            v-if="columns[6].visible"
            :show-overflow-tooltip="true"
          ></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 { listTmToolRequestUsed } from "@/api/mes/tm/tmToolRequest";
export default {
  name: "TmRequestSelectSingle",
  components: {},
  data() {
    return {
      showFlag: false,
      // 选中数组
      selectedToolRequestId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //申请表格数据
      toolRequestList: [],
      // // 品牌名称
      // brandName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandCode: null,
        brandName: null,
        brandId: null,
      },
      // 列信息
      columns: [
        { key: 0, label: this.$t('申请单编码'), visible: true },
        { key: 1, label: this.$t('工序'), visible: true },
        { key: 2, label: this.$t('作业单元'), visible: true },
        { key: 3, label: this.$t('申请时间'), visible: true },
        { key: 4, label: this.$t('预计开始时间'), visible: true },
        { key: 5, label: this.$t('预计结束时间'), visible: true },
        { key: 6, label: this.$t('申请数量'), visible: true },
      ],
    };
  },
  watch: {
    // 根据名称筛选分类树
    showFlag: {
      handler (newName, oldName) {
        console.log(newName, 'newName')
        if (newName) {
          this.getList();
        }
      },
      immediate: true
    }
  },
  created() {

  },
  methods: {
    // /** 查询物料编码列表 */
    // getList() {
    //   this.loading = true;
    //   listMdItem(this.queryParams).then((response) => {
    //     this.itemList = response.rows;
    //     this.total = response.total;
    //     this.loading = false;
    //   });
    // },
    /** 查询品牌列表 */
    getList() {
      this.loading = true;
      listTmToolRequestUsed(this.queryParams).then((response) => {
        this.toolRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    //双击选中
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    //确定选中
    confirmSelect() {
      if (
        this.selectedToolRequestId == null ||
        this.selectedToolRequestId == 0
      ) {
        this.$notify({
          title: this.$t('common.sys_tip'),
          type: "warning",
          message: this.$t('请至少选择一条数据!'),
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
