<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="screenForm"
        :model="screenForm"
        label-width="70px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="名称" prop="name">
              <el-input
                placeholder="请输入名称"
                v-model="screenForm.name"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="型号" prop="type">
              <el-input
                placeholder="请输入型号"
                v-model="screenForm.type"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="编码" prop="number">
              <el-input
                placeholder="请输入编码"
                v-model="screenForm.number"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="体积" prop="volume">
              <el-input
                placeholder="请输入体积"
                v-model="screenForm.volume"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="24" :lg="24" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="resetScreenForm">清空</el-button>
              <el-button size="small" type="primary" @click="submitScreenForm"
                >搜索</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button
          type="primary"
          size="small"
          @click="(showDialogForm = true), (type = 1)"
          >新增</el-button
        >
        <el-popconfirm
          confirm-button-text="好的"
          cancel-button-text="不用了"
          icon="el-icon-info"
          icon-color="red"
          title="内容确定删除吗？"
          @onConfirm="hanleDeleteAll"
        >
          <el-button
            slot="reference"
            type="primary"
            size="small"
            class="textColor el-popover-left"
            >删除</el-button
          >
        </el-popconfirm>
      </div>
      <div class="fr">
        <el-upload
          class="import-btn"
          :action="baseURL + 'student/import'"
          :http-request="handleImport"
          :file-list="importFileList"
        >
          <el-button type="primary" size="small">导入</el-button>
        </el-upload>
        <el-button type="primary" size="small" @click="handleExport"
          >导出</el-button
        >
        <el-button type="primary" size="small">打印</el-button>
      </div>
    </div>
    <div class="mymain-container">
      <!-- 列表 -->
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          @select-all="hanleSelectAll"
          @select="hanleSelectAll"
          stripe
        >
          <el-table-column type="selection" align="center"></el-table-column>
          <el-table-column
            align="center"
            label="类型"
            prop="type"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="名称"
            prop="name"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="型号"
            prop="model"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="编码"
            prop="number"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="体积"
            prop="volume"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column
            align="center"
            label="创建人"
            prop="createBy"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="创建时间"
            prop="createTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新人"
            prop="updateBy"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-button
                type="text"
                class="textColor"
                @click="editFn(scope.row.id, scope.row)"
                >编辑</el-button
              >
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                @onConfirm="hanleDeleteAll(scope.row.id)"
              >
                <el-button
                  slot="reference"
                  type="text"
                  class="textColor el-popover-left"
                  >删除</el-button
                >
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      title="产品信息"
      :visible.sync="showDialogForm"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="diaLogForm"
        :model="diaLogForm"
        label-width="70px"
        size="small"
        label-position="left"
      >
        <el-form-item label="类型">
          <el-select v-model="diaLogForm.type" placeholder="请选择">
            <el-option
              v-for="item in distList"
              :key="item.dictCode"
              :label="item.dictValue"
              :value="item.dictCode"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="diaLogForm.name" size="normal"></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input v-model="diaLogForm.model" size="normal"></el-input>
        </el-form-item>
        <el-form-item label="编码">
          <el-input v-model="diaLogForm.number" size="normal"></el-input>
        </el-form-item>
        <el-form-item label="体积">
          <el-input v-model="diaLogForm.volume" size="normal"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="(showDialogForm = false), resetInfo()"
          >取 消</el-button
        >
        <el-button type="primary" @click="hanleInfo">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Mixin from "@/mixin/index";
import { downloadFiles, handleImport } from "@/utils/util";
import {
  getMachineLlist,
  getMachineAdd,
  getMachineDel,
  getMachineEdit,
  getDistList,
  getMaterialList,

} from "@/api/basic_data/material";
export default {
  mixins: [Mixin],
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      options: [
        {
          value: "选项1",
          label: "1",
        },
        {
          value: "选项2",
          label: "2",
        },
        {
          value: "选项3",
          label: "3",
        },
        {
          value: "选项4",
          label: "4",
        },
        {
          value: "选项5",
          label: "5",
        },
      ],
      diaLogForm: {
        id: null,
        name: "",
        number: "",
        model: "",
        volume: "",
        type: "",

      },
      showDialogForm: false,
      dataList: [
        {
          name: "新风机XXX内机",
          number: "DSAFSAF232312321",
          code: "DSAFSAF232312321",
          volume: "23M^3",
          outName: "新风机XXX外机",
          outNumber: "DSAFSAF232312321",
          updateUser: "DSAFSAF232312321",
          outVolume: "23M^3",
          partsOne: "",
          partsTwo: "",
          create: "LID",
          createDate: "2022-09-12 12:21",
          update: "LID",
          updateDate: "2022-09-12 12:21",
        },
        {
          name: "新风机XXX内机",
          number: "DSAFSAF232312321",
          code: "DSAFSAF232312321",
          volume: "23M^3",
          outName: "新风机XXX外机",
          outNumber: "DSAFSAF232312321",
          updateUser: "DSAFSAF232312321",
          outVolume: "23M^3",
          partsOne: "",
          partsTwo: "",
          create: "LID",
          createDate: "2022-09-12 12:21",
          update: "LID",
          updateDate: "2022-09-12 12:21",
        },
        {
          name: "新风机XXX内机",
          number: "DSAFSAF232312321",
          code: "DSAFSAF232312321",
          volume: "23M^3",
          outName: "新风机XXX外机",
          outNumber: "DSAFSAF232312321",
          updateUser: "DSAFSAF232312321",
          outVolume: "23M^3",
          partsOne: "",
          partsTwo: "",
          create: "LID",
          createDate: "2022-09-12 12:21",
          update: "LID",
          updateDate: "2022-09-12 12:21",
        },
        {
          name: "新风机XXX内机",
          number: "DSAFSAF232312321",
          code: "DSAFSAF232312321",
          volume: "23M^3",
          outName: "新风机XXX外机",
          outNumber: "DSAFSAF232312321",
          updateUser: "DSAFSAF232312321",
          outVolume: "23M^3",
          partsOne: "",
          partsTwo: "",
          create: "LID",
          createDate: "2022-09-12 12:21",
          update: "LID",
          updateDate: "2022-09-12 12:21",
        },
        {
          name: "新风机XXX内机",
          number: "DSAFSAF232312321",
          code: "DSAFSAF232312321",
          volume: "23M^3",
          outName: "新风机XXX外机",
          outNumber: "DSAFSAF232312321",
          updateUser: "DSAFSAF232312321",
          outVolume: "23M^3",
          partsOne: "",
          partsTwo: "",
          create: "LID",
          createDate: "2022-09-12 12:21",
          update: "LID",
          updateDate: "2022-09-12 12:21",
        },
      ],
      searchForm: {
        name: "",
        number: "",
        type: "",
        volume: "",
      },
      type: null,
    };
  },
  methods: {
    editFn(id, row) {
      this.type = 2;
      this.diaLogForm = {
        id,
        name: row.name,
        number: row.number,
        model: row.model,
        volume: row.volume,
        type: row.type,

      };
      this.showDialogForm = true;
    },
    hanleInfo() {
      if (this.type === 1) {
        getMachineAdd(this.diaLogForm).then((res) => {
          this.$successMsg("保存成功");
          this.getList();
        });
      } else if (this.type === 2) {
        const params = {
          ...this.diaLogForm,
        };
        getMachineEdit(params).then((res) => {
          this.$successMsg("编辑成功");
          this.getList();
        });
        this.diaLogForm.id = null;
      }
      this.resetInfo();
      this.showDialogForm = false;
    },
    // 初始化数据
    getList() {
      this.listLoading = true;
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        name: this.screenForm.name,
        number: this.screenForm.number,
        type: this.screenForm.type,
        volume: this.screenForm.volume,
      };
      getMachineLlist(params).then((res) => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
        console.log(this.dataList, 1233);
      });
      getDistList({ sysDictEnum: "MACHINE_CONFIGURE" }).then((res) => {
        this.distList = res.data;
        console.log(res, 123);
      });
    },
    // 导入
    async handleImport(param) {
      this.importLoading = true;
      const file = param.file;
      console.log(file, 123);
      const formData = new FormData();
      formData.append("file", file);
      let result = await handleImport("/stock/importToll", formData);
      this.importLoading = false;
      this.importFileList = [];
      if (result.code == 200) {
        this.$alert(result.message, "导入成功", {
          confirmButtonText: "确定",
        });
        this.getList();
      } else {
        this.$alert(result.message, "导入失败", {
          confirmButtonText: "确定",
        });
      }
    },
    handleExport() {
      let screenData = {
        customerName: this.screenForm.customerName,
        freeDay: this.screenForm.freeDay,
        toll: this.screenForm.toll,
      };
      downloadFiles("/machine-record/export", screenData);
    },
    hanleDeleteAll(id) {
      console.log(id);
      this.hanleDeleteAllPromise(id).then((ids) => {
        getMachineDel(ids).then((res) => {
          this.getList();
        });
      });
    },
    resetInfo() {
      this.diaLogForm = {
        id: null,
        innerMachineName: "",
        innerMachineNumber: "",
        innerMachineType: "",
        innerMachineVolume: 2,
        orgNumber: "4564",
        outsideMachineName: "",
        outsideMachineNumber: "",
        outsideMachineType: "",
        outsideMachineVolume: 2,
      };
    },

  },
};
</script>

<style lang="scss" scoped>
::v-deep .el-select {
  width: 100%;
}
.import-btn {
  display: inline-block;
  margin-right: 10px;
}
</style>
