<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form ref="searchForm" :model="searchForm" 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="categoryId">
              <el-input v-model="searchForm.categoryId" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="searchForm.materialName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="物料编码" prop="materialNumber">
              <el-input v-model="searchForm.materialNumber" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="销售类型" prop="saleTypeId">
              <el-input v-model="searchForm.saleTypeId" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="规格" prop="specification">
              <el-input v-model="searchForm.specification" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="18" class="tr">
            <el-form-item label="">
              <el-button size="small" @click="clearFn">清空</el-button>
              <el-button size="small" type="primary" @click="searchFormFn">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fr">
        <el-button type="primary" size="small" @click="downLoadFn">模板</el-button>
        <ExportButton class="import-btn" :exUrl="'/bus/price/export'" :exParams="exParams" />
        <el-upload class="import-btn" action="" :http-request="handleImport" :file-list="importFileList" :show-file-list="false">
          <el-button type="primary" size="small">批量导入</el-button>
        </el-upload>
      </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 stripe>
          <el-table-column align="left" label="厂产品编码" prop="productCode" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.productCode" />
              <span>{{scope.row.productCode}}</span>
            </template>
          </el-table-column>
          <el-table-column align="left" label="零售价格" prop="retailPrice" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="批发价格" prop="batchPrice" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="规格型号" prop="specification" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.specification" />
              <span>{{scope.row.specification}}</span>
            </template>
          </el-table-column>
          <el-table-column align="left" label="销售类型" prop="saleTypeName" min-width="160" show-overflow-tooltip></el-table-column>

          <el-table-column align="left" label="金蝶物料ID" prop="materialId" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="金蝶物料编码" prop="materialNumber" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="金蝶物料名称" prop="materialName" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="金蝶物料旧编码" prop="materialOldNumber" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="是否促销价" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.isPromote">是</el-tag>
              <el-tag type="danger" v-else>否</el-tag>
            </template>
          </el-table-column>
          <el-table-column align="left" label="是否促作废" min-width="160" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.isRevoke">是</el-tag>
              <el-tag type="danger" v-else>否</el-tag>
            </template>
          </el-table-column>

          <el-table-column align="left" label="格力折扣价" prop="discAmount" min-width="160" show-overflow-tooltip></el-table-column>
          <el-table-column align="left" label="备注" prop="remark" min-width="160" show-overflow-tooltip></el-table-column>

          <el-table-column align="center" label="操作" prop="caozuo" min-width="160" show-overflow-tooltip fixed="right">
            <template slot-scope="scope">
              <el-button type="text" class="textColor" @click="seeFn(scope.row)">查看</el-button>
              <el-popconfirm @onConfirm="deleFn(scope.row.id)" v-if="$checkBtnRole('del', $route.meta.roles)" title="这是一段内容确定删除吗？">
                <el-button type="text" class="textColor" slot="reference">删除</el-button>
              </el-popconfirm>
              <el-button type="text" class="textColor" @click="stopFn(scope.row.id)">修改</el-button>
            </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>
  </div>
</template>

<script>
import { getBusPriceList } from "@/api/supply/commercial_list";
import { downloadFiles, handleImport } from "@/utils/util";
export default {
  data() {
    return {
      listLoading: false, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      importFileList: [],
      searchForm: {
        categoryId: "",
        materialName: "",
        materialNumber: "",
        saleTypeId: "",
        specification: "",
      },
    };
  },
  computed: {
    exParams() {
      return {};
    },
  },
  created() {
    this.getDataList();
  },
  methods: {
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.getDataList();
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList();
    },
    //清除
    clearFn() {
      this.$refs.searchForm.resetFields();
    },
    //搜索
    searchFormFn() {
      this.getDataList();
    },
    //导入
    async handleImport(param) {
      const file = param.file;
      const formData = new FormData();
      formData.append("file", file);
      let result = await handleImport("/bus/price/import", formData);
      //   console.log(result);
      this.importFileList = [];
      if (result.code == 200) {
        this.$message.success("导入成功");
      } else {
        this.$message.error(result.message);
      }
    },
    //下载模板
    downLoadFn() {
      downloadFiles("/bus/price/download");
    },
    //获取列表数据
    async getDataList() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        categoryId: this.searchForm.categoryId,
        materialName: this.searchForm.materialName,
        materialNumber: this.searchForm.materialNumber,
        saleTypeId: this.searchForm.saleTypeId,
        specification: this.searchForm.specification,
      };
      let res = await getBusPriceList(params);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
  },
};
</script>

<style lang="scss" scoped>
.import-btn {
  display: inline-block;
  margin-left: 10px;
}
</style>
