<!--
 * @Descripttion:
 * @version:
 * @Author: kBank
 * @Date: 2022-10-28 16:50:29
-->
<template>
  <div class="page">
    <div class="main">
      <div class="form_all">
        <el-form ref="select" :model="select" label-width="auto">
          <el-row :gutter="10">
            <!-- 一层 -->
            <el-col :span="8">
              <el-form-item :label="'产品名称'">
                <el-input clearable v-model="select.productName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'产品编号'">
                <el-input clearable v-model="select.productCode"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
              <el-form-item :label="'产品属性'">
                <el-select clearable
                           v-model="select.productAttr">
                  <el-option v-for="item in productTypeList"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :span="4">
              <el-form-item :label="'所属分类'">
                <el-select clearable v-model="select.pkClassify">
                  <el-option
                    v-for="item in classifyList"
                    :key="item.pkId"
                    :label="item.name"
                    :value="item.pkId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- <el-col :span="4">
              <el-form-item :label="'发货类型'">
                <el-select clearable
                           v-model="select.shippingChannel">
                  <el-option v-for="item in fhTypeList"
                             :key="item.value"
                             :label="item.label"
                             :value="item.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->

            <el-col :span="4">
              <el-form-item :label="'单位'">
                <el-select clearable v-model="select.unit">
                  <el-option
                    v-for="item in dwList"
                    :key="item.pkId"
                    :label="item.name"
                    :value="item.pkId"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <!-- 二层 -->
            <!-- <el-col :span="4">
              <el-form-item :label="'特惠礼品'">
                <el-select clearable
                           v-model="select.isGift">
                  <el-option v-for="item in agreeList"
                             :key="item.val"
                             :label="item.text"
                             :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'提货商品'">
                <el-select clearable
                           v-model="select.isPickUp">
                  <el-option v-for="item in agreeList"
                             :key="item.val"
                             :label="item.text"
                             :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'抽奖礼品'">
                <el-select clearable
                           v-model="select.isPrize">
                  <el-option v-for="item in agreeList"
                             :key="item.val"
                             :label="item.text"
                             :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'合并发货'">
                <el-select clearable
                           v-model="select.isMerge">
                  <el-option v-for="item in agreeList"
                             :key="item.val"
                             :label="item.text"
                             :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item :label="'追溯编号'">
                <el-select clearable
                           v-model="select.isBarCode">
                  <el-option v-for="item in agreeList"
                             :key="item.val"
                             :label="item.text"
                             :value="item.val"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="4">
                  <el-form-item :label="'运输方式'">
                    <el-select clearable
                               v-model="select.shippingType">
                      <el-option v-for="item in transportTypeList"
                                 :key="item.value"
                                 :label="item.label"
                                 :value="item.value"></el-option>
                    </el-select>
                  </el-form-item>
                </el-col> -->

            <el-col :span="8">
              <el-form-item :label="'创建日期'">
                <el-date-picker
                  v-model="creationTime"
                  @change="changeTime"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  :range-separator="'至'"
                  :start-placeholder="'开始日期'"
                  :end-placeholder="'结束日期'"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="getDataList">
                {{ '搜索' }}</el-button
              >
              <el-button type="" class="resetBtn" @click="reset">
                {{ '重置' }}</el-button
              >
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="main_a">
        <div class="mainbtn">
          <!-- v-hasButtons="['SupplierGoodsAdd']" -->
          <el-button
            size="small"
            @click="openDig"
           
            class="addBtn"
            >{{ '添加' }}</el-button
          >
          <!-- v-hasButtons="['SupplierGoodsExport']" -->
          <el-button
            size="small"
            @click="handleExport"
           
            class="exportBtn"
          >
            {{ '导出' }}</el-button
          >
          <!-- v-hasButtons="['SupplierGoodsCopy']" -->
          <el-button
            size="small"
            :disabled="isCopy"
            @click="toCopy"
            
            class="copyBtn"
            >{{ '复制' }}</el-button
          >
        </div>
      </div>
      <div class="maintable">
        <el-table
          :data="tableData"
          v-loading="loading"
          style="width: 100%"
          height="100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column
            align="center"
            prop="cover"
            :label="'产品主图'"
          >
            <template slot-scope="scope">
              <img class="bgImg" :src="scope.row.cover" alt="" />
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="productName"
            :label="'产品名称'"
          >
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="classifyName"
                           width="120"
                           :label="'所属分类'">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="productCode"
            :label="'产品编号'"
          >
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="shippingChannelVal"
                           width="120"
                           :label="'发货类型'">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="length"
            :label="'长' + '(CM)'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="width"
            :label="'宽' + '(CM)'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="height"
            :label="'高' + '(CM)'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="weight"
            :label="'重量' + '(KG)'"
          >
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="postageWeight" width="120"
                           :label="'邮费重量'+'(KG)'">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="unitVal"
            :label="'单位'"
          >
          </el-table-column>
          <!-- <el-table-column align="center"
                           prop="productAttrName" width="120"
                           :label="'请选择产品属性'">
          </el-table-column> -->
          <el-table-column
            align="center"
            prop="retailPrice"
            label="零售价"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="shelfLife"
            :label="'保质期(天)'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="productSpecType"
            :label="'产品规格'"
          >
          </el-table-column>
          <!-- <el-table-column
            align="center"
            prop="isGift"
            width="120"
            :label="'特惠礼品'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isGift ? 'isRed' : 'isGreen'">
                {{ scope.row.isGift | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isPickUp"
            width="120"
            :label="'提货商品'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isPickUp ? 'isRed' : 'isGreen'">
                {{ scope.row.isPickUp | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isPrize"
            width="120"
            :label="'抽奖礼品'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isPrize ? 'isRed' : 'isGreen'">
                {{ scope.row.isPrize | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isBarCode"
            width="120"
            :label="'追溯编号'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isBarCode ? 'isRed' : 'isGreen'">
                {{ scope.row.isBarCode | isAgree(that) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            prop="isMerge"
            width="120"
            :label="'合并发货'"
          >
            <template slot-scope="scope">
              <div :class="scope.row.isMerge ? 'isRed' : 'isGreen'">
                {{ scope.row.isMerge | isAgree(that) }}
              </div>
            </template>
          </el-table-column> -->
          <!-- <el-table-column align="center"
                           prop="isFresh" width="120"
                           :label="'冷链运输'">
            <template slot-scope="scope">
              <div :class="scope.row.isFresh ? 'isRed' : 'isGreen'">
                {{ scope.row.isFresh | isAgree(that) }}
              </div>
            </template>
          </el-table-column> -->
          <!-- <el-table-column
            align="center"
            prop="typeShippingMerge"
            width="120"
            :label="'运输方式'"
          >
          </el-table-column> -->

          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="time"
            width="120"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <div class="btnHeng">
                <!-- v-hasButtons="['SupplierGoodsEdit']" -->
                <el-button
                  @click="toFixed(scope.row.pkId, 2)"
                  type="text"
                  style="color: #f3a900"
                  size="small"
                >
                  {{ '修改' }}
                </el-button>
                <!-- v-hasButtons="['SupplierGoodsSee']" -->
                <el-button
                  @click="toFixed(scope.row.pkId, 0)"
                  type="text"
                  style="color: #48b2fd"
                  size="small"
                >
                  {{ '查看' }}
                </el-button>
                <!-- v-hasButtons="['SupplierGoodsDel']" -->
                <el-button
                  @click="toFixed(scope.row.pkId, 3)"
                  type="text"
                  size="small"
                  style="color: #c8161d"
                >
                  {{ '删除' }}
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import * as api from "@/api/product.js";
export default {
  name: "ProductList",
  components: {
    topBar,
  },
  filters: {
    isAgree(val, that) {
      if (!val) {
        return that.$t("ENU_POPUP_TYPE_1");
      } else {
        return that.$t("ENU_POPUP_TYPE_2");
      }
    },
  },
  data() {
    return {
      that: this,
      // 所属分类
      classifyList: [],
      // 产品属性枚举
      productTypeList: [],
      // 发货类型
      fhTypeList: [],
      // 单位
      dwList: [],
      // 运输方式
      transportTypeList: [],
      // 折叠搜索框
      isActive: false,
      select: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      addOrEdit: "",
      total: 0,

      tableData: [],
      loading:false,
      countryList: [],
      agreeList: [
        {
          text: '允许',
          val: 0,
        },
        {
          text: '禁止',
          val: 1,
        },
      ],
      form: {},
      creationTime: [],
      isCopy: true,
      selPkId: "",
    };
  },
  mounted() {
    // 获取列表
    this.getDataList();
    // 发货类型
    this.getShipping();
    // 单位
    this.getUnit();
    // 所属分类
    this.getClassifyAll();
    // 运输方式
    this.getTransportType();
    //  产品属性枚举
    this.getProductType();
  },
  methods: {
    reset() {
      this.select = {};
      this.creationTime = [];
      this.getDataList();
    },
    changeTime(val) {
      this.select.startCreateDate = val[0];
      this.select.endCreateDate = val[1];
    },
    // 产品属性枚举
    getProductType() {
      api.productType().then((res) => {
        this.productTypeList = res.data;
      });
    },
    // 运输方式
    getTransportType() {
      api.transportType().then((res) => {
        this.transportTypeList = res.data;
      });
    },
    // 发货类型
    getShipping() {
      api.getShipping().then((res) => {
        this.fhTypeList = res.data;
      });
    },
    // 单位
    getUnit() {
      api.unitAll().then((res) => {
        this.dwList = res.data;
      });
    },
    // 所属分类
    getClassifyAll() {
      api.classifyAll().then((res) => {
        this.classifyList = res.data;
      });
    },

    // 点击修改
    toFixed(id, index) {
      this.$router.push({
        path: "supplierGoodsAdd",
        query: {
          productId: id,
          methods: index,
        },
      });
    },
    // 点击修改
    toChecked(id, index) {
      this.$router.push({
        path: "supplierGoodsAdd",
        query: {
          productId: id,
          methods: index,
        },
      });
    },
    // 删除
    handleDelete(id) {
      api.productDel(id).then((res) => {
        // 获取列表
        this.getDataList();
      });
    },
    openDig() {
      this.$router.push({
        path: "supplierGoodsAdd",
        query: {
          methods: 1,
        },
      });
    },
    // 获取列表
    getDataList() {
      this.loading = true
      api
        .thProductList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          res.rows.forEach((item) => {
            this.productTypeList.forEach((child) => {
              if (child.value == item.productAttr) {
                item.productAttrName = child.label;
              }
            });
          });
          this.tableData = res.rows;
          this.total = res.total;
          this.loading = false
        });
    },
    toCopy() {
      this.toChecked(this.selPkId, 4);
    },
    handleSelectionChange(val) {
      if (val.length > 0) {
        this.selPkId = val[0].pkId;
        this.isCopy = false;
      } else {
        this.isCopy = true;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then((_) => {
        this.download(
          "/sale/manage/product/export",
          Object.assign({}, this.queryParams, this.select),
          `产品列表-${new Date().getTime()}.xlsx`
        );
      });
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-select {
  width: 100%;
}
::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}
.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important; /* 上传按钮隐藏 */
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .main {
    display: flex;
    flex-direction: column;
    // background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);
    .form_all {
      padding: 0 20px;
      margin-bottom: 10px;
      background: #fff;
      border-radius: 8px;
    }
    .main_a {
      display: flex;
      padding: 0px 20px 3px 0;
      justify-content: space-between;
      align-items: center;
      background: #fff;
      border-radius: 8px;
      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }
        .thebtn2 {
          background: #4391fd;
          color: #ffffff;
        }
        .thebtn3 {
          background: #6962f6;
          color: #ffffff;
        }
      }
      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
    .maintable {
      flex: 1;
      overflow-y: auto;
    }
  }
  .tem {
    display: flex;
    align-items: center;
    justify-content: center;
    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }
  .isRed {
    color: #ed1d25;
  }
  .isGreen {
    color: #1ab62b;
  }
  .bgImg {
    width: 48px;
    height: 48px;
  }
}
.openClose {
  margin-right: 10px;
  color: #666666;
  text-align: center;
}
.btnHeng {
  display: flex;
}
.btnHeng ::v-deep .el-button {
  padding: 0;
}
</style>
