<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :operation="operation()"
    :optionsEvensGroup="optionsEvensGroup"
    :exportList="exportList"
    :columnParsing="columnParsing"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
  >
    <div>
      <el-dialog :visible.sync="dialogVisible" width="30%" @close="hanelclose" title="返利类型">
        <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="120px" :inline="false" size="normal">
          <el-form-item label="返利使用钱包" prop="walletRebateId">
            <!-- <el-input
              v-model="dialogForm.name"
              :readonly="type == 3"
            ></el-input> -->
            <el-select v-model="dialogForm.walletRebateId" :disabled="type == 3" filterable placeholder="请选择钱包">
              <el-option
                v-for="item in walletList"
                :key="item.walletRebateId"
                :label="item.name"
                :value="item.walletRebateId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售类型名称" prop="saleTypeId">
            <el-select
              v-model="dialogForm.saleTypeId"
              :disabled="type == 3"
              filterable
              @change="handleChange"
              placeholder="请选择销售类型名称"
            >
              <el-option v-for="item in typeList" :key="item.id" :label="item.saleName" :value="item.id"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="销售类型编码" prop="saleTypeCode">
            <el-input
              disabled
              type="text"
              v-model="dialogForm.saleTypeCode"
              placeholder="选择销售类型自动获取销售编码"
            />
          </el-form-item>

          <el-form-item label="返利折扣比例" prop="rebateRate">
            <el-input
              type="number"
              @mousewheel.native.prevent
              placeholder="例如0.1=1折"
              :disabled="type == 3"
              @change="handleNumber"
              v-model.number="dialogForm.rebateRate"
            ></el-input>
          </el-form-item>
          <!-- <el-form-item label="销售类型编码" prop="saleTypeCode">
            <el-input
              v-model="dialogForm.saleTypeCode"
              :readonly="true"
            ></el-input>
          </el-form-item> -->
          <!-- <el-form-item label="产品使用品类" prop="mainName">
            <el-input v-model="dialogForm.mainName" :readonly="true"></el-input>
          </el-form-item> -->

          <el-form-item label="状态" prop="status">
            <el-switch
              v-model="dialogForm.status"
              :disabled="type == 3"
              :active-value="true"
              :inactive-value="false"
              :active-text="dialogForm.status ? '启用' : ''"
            >
            </el-switch>
          </el-form-item>
        </el-form>
        <template v-if="type !== 3">
          <span slot="footer" class="dialog-footer">
            <el-button @click="hanleCancel">取 消</el-button>
            <el-button type="primary" @click="handelInfo">确 定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
// import add_callback_mixin from '@/components/template/add_callback_mixin.js'

import Mixin from '@/mixin/index'
// import Pagination from '@/components/Pagination'
import {
  getDictList,
  getTypeList,
  getWalletRebateList,
  getWalletRebateSaletypeAdd,
  getWalletRebateSaletypeDelete,
  getWalletRebateSaletypelist,
  getWalletRebateSaletypeUpdate,
  walletRebateSaletypeListV2,
  walletRebateSaletypeListV2Export,
  getWalletRebateSaletypeDetail
} from '@/api/policy_list'

export default {
  mixins: [],
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: () => {
                this.dialogVisible = true
                this.type = 1
                this.getDictList()
              },
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async () => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                await this.delfn()
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              },
              isRole: this.$checkBtnRole('del', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      ids: [],
      walletList: [],
      type: 0, // 0 1
      screenForm: {
        // mainName: "",
        saleTypeCode: '',
        saleTypeName: '',
        status: ''
      },
      dialogVisible: false,
      dialogForm: {
        walletRebateId: '',
        rebateRate: '',
        saleTypeId: '',
        status: '',
        saleTypeCode: ''
      },
      dataList: [],
      columns: [
        {
          prop: 'saleTypeCode',
          lable: '销售类型编码',
          widht: 160
        },
        {
          prop: 'saleTypeName',
          lable: '销售类型名称',
          widht: 160
        },

        {
          prop: 'name',
          lable: '返利类型',
          widht: 160
        },
        {
          prop: 'rebateRate',
          lable: '返利折扣比例',
          widht: 160
        },
        {
          prop: 'status',
          lable: '状态',
          widht: 160
        },
        {
          prop: 'createBy',
          lable: '创建人',
          widht: 160
        },
        {
          prop: 'createTime',
          lable: '创建时间',
          widht: 160
        },
        {
          prop: 'updateBy',
          lable: '更新人',
          widht: 160
        },
        {
          prop: 'updateTime',
          lable: '更新时间',
          widht: 160
        }
      ],
      options: [
        {
          value: '',
          label: '全部'
        },
        {
          value: 1,
          label: '已启用'
        },
        {
          value: 0,
          label: '已停用'
        }
      ],
      dictListData: [],
      typeList: [],
      rules: {
        walletRebateId: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        saleTypeId: [{ required: true, message: '请选择销售类型', trigger: 'blur' }],

        rebateRate: [{ required: true, message: '请输入比例', trigger: 'blur' }]
      }
    }
  },
  computed: {
    exParams() {
      return {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        // mainName: this.screenForm.mainName,
        saleTypeCode: this.screenForm.saleTypeCode,
        saleTypeName: this.screenForm.saleTypeName,
        status: this.screenForm.status
      }
    }
  },
  methods: {
    // 列表请求函数
    getList: walletRebateSaletypeListV2,
    // 列表导出函数
    exportList: walletRebateSaletypeListV2Export,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            <el-button
              size="mini"
              type="text"
              onClick={async () => {
                this.hanleDateil(row)
              }}
            >
              查看
            </el-button>

            {this.$checkBtnRole('edit', this.$route.meta.roles) ? (
              <el-button
                size="mini"
                type="text"
                onClick={async () => {
                  this.hanleEdit(row)
                }}
              >
                编辑
              </el-button>
            ) : (
              ''
            )}
          </div>
        )
      }
    },

    //
    async getDetail(walletRebateSaleTypeId) {
      return await getWalletRebateSaletypeDetail({ walletRebateSaleTypeId })
    },

    //
    selectionhangeFn(value) {
      console.log(value)
      this.ids = value.map(v => v.walletRebateSaleTypeId)
    },
    //删除
    async delfn() {
      // if (this.ids.length == 0) {
      //   this.$message.error("请选择列表数据");
      //   return;
      // }
      let res = this.recordSelected.map(v => v.walletRebateSaleTypeId).toString()
      await getWalletRebateSaletypeDelete({ walletRebateSaleTypeIds: res })
    },
    //获取钱包列表
    async getWalletList() {
      const res = await getWalletRebateList({
        pageNum: 1,
        pageSize: -1
      })
      this.walletList = res.data.records
    },
    // getList() {
    //   this.listLoading = true
    //   const params = {
    //     pageNum: this.currentPage,
    //     pageSize: this.pageSize,
    //     // mainName: this.screenForm.mainName,
    //     saleTypeCode: this.screenForm.saleTypeCode,
    //     saleTypeName: this.screenForm.saleTypeName,
    //     status: this.screenForm.status
    //   }

    //   getWalletRebateSaletypelist(params).then(res => {
    //     this.dataList = res.data.records
    //     this.listTotal = res.data.total
    //     this.listLoading = false
    //   })
    //   // getProductList({
    //   //   productCategoryName: "",
    //   //   productCategoryNumber: "",
    //   // }).then((res) => {
    //   //   this.productList = res.data;
    //   // });
    // },

    handleChange(e) {
      this.typeList.find(k => {
        if (e == k.id) {
          this.dialogForm.saleTypeName = k.saleName
          this.dialogForm.saleTypeCode = k.saleCode
          // this.dialogForm.mainId = k.mainId;
          // this.dialogForm.mainName = k.mainName;
        }
      })
    },
    handleNumber(e) {
      console.log(e)
      if (Number(e) < 0) {
        this.dialogForm.rebateRate = ''
        this.$errorMsg('不能小于0，输入数值0至1之间')
      } else if (Number(e) > 1) {
        this.dialogForm.rebateRate = ''
        this.$errorMsg('不能大于1，输入数值0至1之间')
      } else {
        this.dialogForm.rebateRate = e
      }
    },
    handleSwitch(e, walletRebateSaleTypeId) {
      getWalletRebateSaletypeUpdate({
        walletRebateSaleTypeId,
        status: e
      }).then(res => {
        this.$successMsg('状态已更改')
        // this.hanleReset();
      })
    },
    async hanleEdit(item) {
      let { data } = await this.getDetail(item.walletRebateSaleTypeId)
      this.dialogForm = {
        // adminCompanyId: item.adminCompanyId,

        // name: item.name,
        rebateRate: data.rebateRate,
        // rabateSort: item.rabateSort,
        // saleTypeCode: item.saleTypeCode,
        saleTypeId: data.saleTypeId,
        // saleTypeName: item.saleTypeName,
        status: data.status,
        walletRebateId: data.walletRebateId,
        walletRebateSaleTypeId: data.walletRebateSaleTypeId
      }
      await this.getDictList()
      this.handleChange(data.saleTypeId)
      this.dialogVisible = true
    },
    getDictList() {
      getDictList({
        sysDictEnum: 'PRODUCT_TYPE'
      }).then(res => {
        this.dictListData = res.data
      })
      const params = {
        pageNum: 1,
        pageSize: -1,
        saleCode: '',
        saleName: '',
        status: ''
      }
      getTypeList(params).then(res => {
        this.typeList = res.data.records
      })
    },
    async hanleDateil(item) {
      let { data } = await this.getDetail(item.walletRebateSaleTypeId)
      this.type = 3
      this.dialogVisible = true
      this.dialogForm = {
        // ...res,
        // saleTypeId: res.saleTypeName
        walletRebateId: data.walletRebateId,
        walletRebateSaleTypeId: data.walletRebateSaleTypeId,
        rebateRate: data.rebateRate,
        saleTypeId: data.saleTypeId,
        status: data.status,
        saleTypeCode: data.saleTypeCode
      }
      await this.getDictList()
      this.handleChange(data.saleTypeId)
    },
    hanelclose() {
      this.type = ''
      this.hanleReset()
      this.$refs.dialogForm.resetFields()
    },
    handelInfo() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          if (this.type) {
            // this.hanleScreen(this.dialogForm.mainId);
            const params = {
              ...this.dialogForm
            }
            getWalletRebateSaletypeAdd(params).then(res => {
              this.$successMsg('添加成功')
              this.hanleReset()
              this.$refs.pageRef.refreshList()
            })
          } else {
            const upParams = {
              ...this.dialogForm
            }
            getWalletRebateSaletypeUpdate(upParams).then(res => {
              this.$successMsg('修改成功')
              this.hanleReset()
              this.$refs.pageRef.refreshList()
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    hanleReset() {
      this.dialogForm = {
        name: '',
        // mainId: "",
        // mainName: "",
        // saleTypeCode: "",
        // saleTypeName: "",
        // rabateRate: "",
        status: true
      }
      // this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
      // this.getList()
    },
    hanleCancel() {
      this.hanleReset()
      this.dialogVisible = false
    },
    hanleScreen(code) {
      // this.dictListData.find((k) => {
      //   if (k.dictCode == code) {
      //     this.dialogForm.mainId = k.dictCode;
      //     this.dialogForm.mainName = k.dictValue;
      //     return;
      //   }
      // });
    }
  },
  created() {
    this.getWalletList()
  },
  components: {
    TemplatePage
  }
}
</script>

<style scoped>
.el-switch.is-disabled {
  opacity: inherit;
}
::v-deep .el-select {
  width: 100%;
}
.delClass {
  margin: 0 10px;
}
</style>
