<template>
  <el-dialog
    title="用途选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal="false"
    width="57%"
    center
  >
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="用途编码" prop="usageCode">
        <el-input
          v-model="queryParams.usageCode"
          placeholder="请输入用途编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用途名称" prop="usageName">
        <el-input
          v-model="queryParams.usageName"
          placeholder="请输入用途名称"
          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="usageList"
      @current-change="handleCurrent"
      @row-dblclick="handleRowDbClick"
    >
      <el-table-column width="200" align="center">
        <template v-slot="scope">
          <el-radio
            v-model="selectedusageId"
            :label="scope.row.usageId"
            @change="handleRowChange(scope.row)"
            >{{ "" }}</el-radio
          >
        </template>
      </el-table-column>
      <el-table-column label="用途编码" width="200" prop="usageCode">
      </el-table-column>
      <el-table-column
        label="用途名称"
        width="100"
        align="center"
        prop="usageName"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="备注" width="400" align="center" prop="remark">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mes_usage_sourcetype"
            :value="scope.row.orderSource"
          />
        </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">取 消</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { listUsage } from "@/api/mes/pro/usage";
export default {
  name: "UsageSelectSingle",
  components: {},
  dicts: ["mes_order_status", "mes_workorder_sourcetype"],
  // props:{
  //   workorder: {
  //     type: Object,
  //     default: function(){
  //       return {'workorderType': 'SELF'}
  //     }} //外部传入的用途过滤信息
  // },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedUsageId: undefined,
      selectedRows: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 生产用途表格数据
      usageList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        usageCode: null,
        usageName: null,
      },
      // 列信息
      columns: [
        { key: 0, label: `用途编码`, visible: true },
        { key: 1, label: `用途名称`, visible: true },
        { key: 2, label: `备注`, visible: true },
      ],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询生产用途列表 */
    getList() {
      this.loading = true;
      listUsage(this.queryParams).then((response) => {
        this.usageList = 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;
      }
    },
    // 单选选中数据
    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.selectedUsageId == null || this.selectedUsageId == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!",
        });
        return;
      }
      this.$emit("onSelected", this.selectedRows);
      this.showFlag = false;
    },
  },
};
</script>
