<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="特性类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择特新类型" clearable>
          <el-option v-for="item in dict.type.sap_property_type" :key="item.value" v-bind="item"/>
        </el-select>
      </el-form-item>
      <el-form-item label="关联ID" prop="relationId">
        <el-input
          v-model="queryParams.relationId"
          placeholder="请输入关联ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="sap上传状态" prop="uploadFlag" label-width="8em">
        <!--        <el-input-->
        <!--          v-model="queryParams.uploadFlag"-->
        <!--          placeholder="请输入是否上传过sap"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <el-select v-model="queryParams.uploadFlag" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sap_upload_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="上传时间" prop="uploadTime" label-width="8em">
        <el-date-picker clearable
                        v-model="queryParams.uploadTime"
                        type="date"
                        value-format="yyyy-MM-dd hh:mm:ss"
                        placeholder="请选择上传sap时间">
        </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-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['md:sapPropertyData:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['md:sapPropertyData:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['md:sapPropertyData:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['md:sapPropertyData:export']"
        >导出
        </el-button>
      </el-col>
      <!--      批量上传-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-upload"-->
      <!--          size="mini"-->
      <!--          @click="handleUpload"-->
      <!--          v-hasPermi="['md:sapPropertyData:export']"-->
      <!--        >上传</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sapdataList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" />-->
      <el-table-column label="id" align="center" prop="id"/>
      <el-table-column label="特性类型" align="center" prop="type" :formatter="formatType"/>
      <el-table-column label="相关id" align="center" prop="relationId"/>
      <!--      <el-table-column label="客户SAP特性info" align="center" prop="sapData" />-->
      <el-table-column label="sap上传状态" align="center" prop="uploadFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sap_upload_status" :value="scope.row.uploadFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="上传sap时间" align="center" prop="uploadTime" width="180">
        <!--        <template slot-scope="scope">-->
        <!--          <span>{{ parseTime(scope.row.uploadTime, '{y}-{m}-{d}') }}</span>-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['md:sapPropertyData:edit']"-->
          <!--          >修改</el-button>-->
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['md:sapPropertyData:remove']"-->
          <!--          >删除</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload"
            :title="scope.row.uploadFlag == 1 ? '该数据已同步' : ''"
            :disabled="scope.row.uploadFlag == 1"
            @click="handleUpload(scope.row)"
            v-hasPermi="['md:sapPropertyData:upload']"
          >上传
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户SAP特性信息对话框 -->
    <!--    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>-->
    <!--      <el-form ref="form" :model="form" :rules="rules" label-width="80px">-->
    <!--        <el-form-item label="类型ID" prop="type">-->
    <!--          <el-select v-model="form.type" placeholder="请选择类型ID">-->
    <!--            <el-option label="请选择字典生成" value="" />-->
    <!--          </el-select>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="关联ID" prop="relationId">-->
    <!--          <el-input v-model="form.relationId" placeholder="请输入关联ID" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="客户SAP特性info" prop="sapData">-->
    <!--          <el-input v-model="form.sapData" type="textarea" placeholder="请输入内容" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="是否上传过sap" prop="uploadFlag">-->
    <!--          <el-input v-model="form.uploadFlag" placeholder="请输入是否上传过sap" />-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="上传sap时间" prop="uploadTime">-->
    <!--          <el-date-picker clearable-->
    <!--                          v-model="form.uploadTime"-->
    <!--                          type="date"-->
    <!--                          value-format="yyyy-MM-dd"-->
    <!--                          placeholder="请选择上传sap时间">-->
    <!--          </el-date-picker>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--      <div slot="footer" class="dialog-footer">-->
    <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
    <!--        <el-button @click="cancel">取 消</el-button>-->
    <!--      </div>-->
    <!--    </el-dialog>-->
  </div>
</template>

<script>
import {listSapdata, uploadSapdata} from "@/api/mes/md/sapdata";

export default {
  name: "Sapdata",
  dicts: ['sap_property_type', 'sap_upload_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户SAP特性信息表格数据
      sapdataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null, relationId: null, sapData: null, uploadFlag: null, uploadTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formatType(row) {
      let item = this.dict.type.sap_property_type.find(item => item.value == row.type)
      return item ? item.label : ''
    },
    /** 查询客户SAP特性信息列表 */
    getList() {
      this.loading = true;
      listSapdata(this.queryParams).then(response => {
        this.sapdataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        relationId: null,
        sapData: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        uploadFlag: null,
        uploadTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // /** 新增按钮操作 */
    // handleAdd() {
    //   this.reset();
    //   this.open = true;
    //   this.title = "添加客户SAP特性信息";
    // },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getSapdata(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改客户SAP特性信息";
    //   });
    // },
    /** 上传给sap操作 */
    handleUpload(row) {
      const type = row.type;
      const id = row.id;
      const relationId = row.relationId;
      switch (type) {
        case 1:
          this.$modal.confirm('是否确认上传客户SAP特性信息编号为"' + id + '"的数据项？').then(function () {
            return uploadSapdata(id, relationId);
          }).then((response) => {
            this.$modal.msgSuccess(response.msg || "上传成功");
          }).catch(() => {
          }).finally(() => {
            this.getList();
          })
        case 2:
        case 3:
        case 4:
        case 5:
      }


    },
    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateSapdata(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addSapdata(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    // /** 删除按钮操作 */
    // handleDelete(row) {
    //   const ids = row.id || this.ids;
    //   this.$modal.confirm('是否确认删除客户SAP特性信息编号为"' + ids + '"的数据项？').then(function() {
    //     return delSapdata(ids);
    //   }).then(() => {
    //     this.getList();
    //     this.$modal.msgSuccess("删除成功");
    //   }).catch(() => {});
    // },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/sapdata/export', {
        ...this.queryParams
      }, `sapdata_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
