<template>
  <el-dialog
    title="刀模版申请单选择"
    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="作业单元" prop="workunitName">
            <el-input
              v-model="queryParams.workunitName"
              placeholder="请输入作业单元"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <!-- <el-form-item label="申请时间" prop="requestDate">
            <el-date-picker
              clearable
              v-model="queryParams.requestDate"
              type="date"
              value-format="yyyy-MM-dd"
              placeholder="请选择申请时间"
            >
            </el-date-picker>
          </el-form-item> -->
          <el-form-item label="申请时间">
        <el-date-picker v-model="daterangePurchaseDate" value-format="yyyy-MM-dd" type="daterange" range-separator="-"
          start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </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="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="申请单编码"
            min-width="130"
            align="center"
            key="toolRequestCode"
            prop="toolRequestCode"
            v-if="columns[0].visible"
          >
          </el-table-column>

          <el-table-column
            label="工序"
            min-width="120"
            align="center"
            key="processName"
            prop="processName"
            v-if="columns[1].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="作业单元"
            width="120"
            align="center"
            key="workunitName"
            prop="workunitName"
            v-if="columns[2].visible"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="申请时间"
            align="center"
            key="requestDate"
            prop="requestDate"
            v-if="columns[3].visible"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="预计开始时间"
            align="center"
            key="startTime"
            prop="startTime"
            v-if="columns[4].visible"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="预计结束时间"
            align="center"
            key="endTime"
            prop="endTime"
            v-if="columns[5].visible"
            :show-overflow-tooltip="true"
          ></el-table-column>
          <el-table-column
            label="申请数量"
            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">确 定</el-button>
      <el-button @click="showFlag = false">取 消</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: [],
      daterangePurchaseDate: [],
      // // 品牌名称
      // brandName: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandCode: null,
        brandName: null,
        brandId: null,
      },
      // 列信息
      columns: [
        { key: 0, label: `申请单编码`, visible: true },
        { key: 1, label: `工序`, visible: true },
        { key: 2, label: `作业单元`, visible: true },
        { key: 3, label: `申请时间`, visible: true },
        { key: 4, label: `预计开始时间`, visible: true },
        { key: 5, label: `预计结束时间`, visible: true },
        { key: 6, label: `申请数量`, 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;
      this.queryParams.params = {};
      if (null != this.daterangePurchaseDate && '' != this.daterangePurchaseDate) {
        this.queryParams.params["beginPurchaseDate"] = this.daterangePurchaseDate[0];
        this.queryParams.params["endPurchaseDate"] = this.daterangePurchaseDate[1];
      }
      listTmToolRequestUsed(this.queryParams).then((response) => {
        this.toolRequestList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangePurchaseDate=[];
      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: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
