<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="screenForm"
        :model="screenForm"
        label-width="70px"
        size="mini"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="物料编码" prop="materialNumber">
              <el-input
                v-model="screenForm.materialNumber"
                placeholder="请输入物料编码"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="产品名称" prop="materialName">
              <el-input
                v-model="screenForm.materialName"
                placeholder="请输入产品名称"
              />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="产品编码" prop="materialOldNumber">
              <el-input
                v-model="screenForm.materialOldNumber"
                placeholder="请输入产品编码"
              />
            </el-form-item>
          </el-col>
          <!--          <el-col :xs="24" :sm="12" :lg="6">-->
          <!--            <el-form-item label="生效日期" prop="startDate">-->
          <!--              <el-date-picker-->
          <!--                v-model="screenForm.startDate"-->
          <!--                type="datetime"-->
          <!--                placeholder="请输入生效日期"-->
          <!--                value-format="yyyy-MM-dd HH:mm:ss"-->
          <!--              >-->
          <!--              </el-date-picker>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <!--          <el-col :xs="24" :sm="12" :lg="6">-->
          <!--            <el-form-item label="失效日期" prop="endDate">-->
          <!--              <el-date-picker-->
          <!--                v-model="screenForm.endDate"-->
          <!--                type="datetime"-->
          <!--                placeholder="请输入生效日期"-->
          <!--                value-format="yyyy-MM-dd HH:mm:ss "-->
          <!--              >-->
          <!--              </el-date-picker>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="销售类型" prop="saleTypeId">
              <el-select
                v-model="screenForm.saleTypeId"
                filterable
                placeholder="选择销售类型"
                style="width: 100%"
              >
                <el-option label="全部" value="" />
                <el-option
                  v-for="item in ztypeList"
                  :key="item.id"
                  :label="item.saleName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="产品类别" prop="categoryId">
              <el-select
                v-model="screenForm.mainId"
                placeholder="请选择产品类别"
              >
                <el-option label="全部" value="" />
                <el-option
                  v-for="item in dictList"
                  :label="item.dictValue"
                  :value="item.dictCode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="规格型号" prop="specification">
              <el-input
                v-model="screenForm.specification"
                placeholder="请输入规格型号"
              />
            </el-form-item>
          </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="备注" prop="specification">
              <el-input
                v-model="screenForm.remark"
                placeholder="请输入备注"
              />
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="24" :lg="24" class="tr">
            <el-form-item label="">
              <el-button size="mini" @click="resetScreenForm">清空</el-button>
              <el-button
                size="mini"
                type="primary"
                @click="submitScreenForm"
              >搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div >
           <el-popconfirm
           v-if="$checkBtnRole('del', $route.meta.roles)"
            confirm-button-text="好的"
            cancel-button-text="不用了"
            icon="el-icon-info"
            icon-color="red"
            title="内容确定删除吗？"
            @onConfirm="handleDelete"
          >
            <el-button
              type="primary"
              size="mini"
                icon="el-icon-delete"
              slot="reference"
              class="el-popover-left"
              >删除</el-button
            >
          </el-popconfirm>
<!--        <el-button type="primary" size="mini">导出</el-button>-->
        <!-- <el-button type="primary" size="mini">打印</el-button> -->
      </div>
    </div>
    <div class="mymain-container">
      <!-- 列表 -->
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          stripe
             @select-all="hanleSelectAll"
            @select="hanleSelectAll"
        >
           <el-table-column
              type="selection"
              align="left"
              width="50"
            ></el-table-column>
<!--          <el-table-column type="selection" align="left" />-->
          <!-- <el-table-column
            align="left"
            label="产品类别"
            prop="mainName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column> -->
          <el-table-column
            align="left"
            label="物料编码"
            prop="materialNumber"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.materialNumber" />
              <span>{{scope.row.materialNumber}}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="产品编码"
            prop="materialOldNumber"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.materialOldNumber" />
              <span>{{scope.row.materialOldNumber}}</span>
            </template>

          </el-table-column>
          <el-table-column
            align="left"
            label="产品名称"
            prop="materialName"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <CopyButton :copyText="scope.row.materialName" />
              <span>{{scope.row.materialName}}</span>
            </template>

          </el-table-column>
          <el-table-column
            align="left"
            label="规格型号"
            prop="specification"
            min-width="200"
            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
            align="left"
            label="单位"
            prop="unit"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="数量下限"
            prop="qty"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="返利类型（钱包）"
            prop="wallets"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <template v-for="(item, index) in scope.row.wallets">
                <template v-if="item.type === 'REBATE'">
                  <el-tag
                    :key="index"
                    type="success"
                    style="margin: 0 10px"
                    size="mini"
                  >
                    {{ item.walletName }}
                  </el-tag>
                </template>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="现金钱包"
            prop="modifyPriceDepartment"
            min-width="200"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <template v-for="(item, index) in scope.row.wallets">
                <template v-if="item.type === 'COMMONLY'">
                  <el-tag
                    :key="index"
                    type="success"
                    style="margin: 0 10px"
                    size="mini"
                  >
                    {{ item.walletName }}
                  </el-tag>
                </template>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="生效日期"
            prop="startDate"
            min-width="160"
            show-overflow-tooltip
          />

          <el-table-column
            align="left"
            label="失效日期"
            prop="endDate"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="是否促销价"
            prop="isPromote"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag size="mini" v-if="scope.row.isPromote" type="success">是</el-tag>
              <el-tag size="mini" v-else type="danger">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="left"
            label="是否促作废"
            prop="isRevoke"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-tag size="mini" v-if="scope.row.isRevoke" type="success">是</el-tag>
              <el-tag size="mini" v-else type="danger">否</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            align="right"
            label="批发价1"
            prop="batchPrice"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="right"
            label="格力折扣"
            prop="discAmount"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            align="left"
            label="备注"
            prop="remark"
            min-width="160"
            show-overflow-tooltip
          />
          <el-table-column
            fixed="left"
            width="150"
            label="操作"
            align="center"
          >
            <template slot-scope="scope">
              <el-popconfirm
                v-if="!scope.row.isRevoke"
                style="margin-right: 10px"
                title="作废吗？"
                @click="handleRevoke(scope.row.id)"
              >
                <el-button
                  slot="reference"
                  type="text"
                  size="mini"
                >作废</el-button>
              </el-popconfirm>

              <el-button
                v-if="$checkBtnRole('del', $route.meta.roles)"
                type="text"
                size="mini"
                @click="handleDelete(scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="fr">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import Mixin from '@/mixin/index'
import { getProductDelete, getProductPriceList, getTypeList, handlePriceRevoke } from '@/api/basic_data/material'
import { getDictList } from '@/api/common'

export default {
  mixins: [Mixin],
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      screenForm: {
        mainId: '',
        endDate: '',
        materialName: '',
        materialNumber: '',
        materialOldNumber: '',
        saleTypeId: '',
        specification: '',
        startDate: '',
        remark:''
      },
      ztypeList: [],
      dictList: {}
    }
  },
  mounted() {
    getDictList({ sysDictEnum: 'PRODUCT_TYPE' }).then((res) => {
      this.dictList = res.data
      console.log(this.dictList)
    })
  },
  methods: {
    addFn() {
      this.showDialogForm = true
    },

    handleDelete(id) {
      this.hanleDeleteAllPromise(id).then((ids) => {
        getProductDelete(ids).then((res) => {
          this.$successMsg('删除成功')
          this.getList()
        })
      })
    },
    getList() {
      this.listLoading = true
      const params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        mainId: this.screenForm.mainId,
        endDate: this.screenForm.endDate,
        materialName: this.screenForm.materialName,
        materialNumber: this.screenForm.materialNumber,
        materialOldNumber: this.screenForm.materialOldNumber,
        saleTypeId: this.screenForm.saleTypeId,
        specification: this.screenForm.specification,
        startDate: this.screenForm.startDate,
        remark: this.screenForm.remark
      }
      getProductPriceList(params).then((res) => {
        this.dataList = res.data.records
        console.log(res.data.records)
        this.listTotal = res.data.total
        this.listLoading = false
      })
      getTypeList(params).then((res) => {
        this.ztypeList = res.data.records
        // console.log(this.ztypeList, "type1111");
      })
    },
    handleRevoke(id) {
      handlePriceRevoke({ id }).then((res) => {
        this.$successMsg('操作成功')
        this.getList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-select--small {
  width: 100%;
}
::v-deep .el-date-editor {
  width: 100%;
}
</style>
