<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :operation="operation()"
    :optionsEvensGroup="optionsEvensGroup"
    :columnParsing="columnParsing"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
  >
    <div>
      <el-dialog
        v-loading="addLoading"
        element-loading-text="Loading"
        :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="name">
            <el-input v-model="dialogForm.name" :readonly="type == 3"></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="rebateRate">
            <el-input
              type="number" @mousewheel.native.prevent
              placeholder="例如0.1=1折"
              :readonly="type == 3"
              @change="handleNumber"
              v-model.number="dialogForm.rebateRate"
            ></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>
          <span v-show="type !== 3" 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 { addWallet, getDictList, getTypeList, getWalletList, updateWallet, walletRebateListV2 } from '@/api/policy_list'

export default {
  mixins: [add_callback_mixin],
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: () => {
                this.dialogVisible = true
                this.type = 1
                this.getDictList()
              },
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: false
      },
      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],

      addLoading: false,
      listLoading: false,
      type: 0, // 0 1
      screenForm: {
        name: '',
        saleTypeCode: '',
        saleTypeName: '',
        status: ''
      },
      dialogVisible: false,
      dialogForm: {
        name: ''
        // mainName: "",
        // mainId: "",
        // saleTypeCode: "",
        // saleTypeName: "",
        // rebateRate: "",
        // status: "",
      },
      dataList: [],
      columns: [
        {
          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: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }]
        // saleTypeCode: [
        //   { required: true, message: "请选择编码", trigger: "blur" },
        // ],

        // rebateRate: [
        //   { required: true, message: "请输入比例", trigger: "blur" },
        // ],
      }
    }
  },
  methods: {
    // 列表请求函数
    getList: walletRebateListV2,
    // 列表导出函数
    // exportList: exportLevelList,
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {this.$checkBtnRole('edit', this.$route.meta.roles) ? (
              <el-button
                size="mini"
                type="text"
                onClick={async () => {
                  this.hanleEdit(row)
                }}
              >
                编辑
              </el-button>
            ) : (
              ''
            )}

            <el-button
              size="mini"
              type="text"
              onClick={async () => {
                this.hanleDateil(row)
              }}
            >
              查看
            </el-button>
          </div>
        )
      }
    },

    // getList() {
    //   this.listLoading = true
    //   const params = {
    //     pageNum: this.currentPage,
    //     pageSize: this.pageSize,
    //     name: this.screenForm.name,
    //     // saleTypeCode: this.screenForm.saleTypeCode,
    //     // saleTypeName: this.screenForm.saleTypeName,
    //     status: this.screenForm.status
    //   }

    //   getWalletList(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) {
      console.log(e, this.typeList)

      this.typeList.find(k => {
        if (e == k.saleCode) {
          this.dialogForm.saleTypeName = k.saleName
          // this.dialogForm.mainId = k.mainId;
          // this.dialogForm.mainName = k.mainName;
        }
      })
    },
    handleNumber(e) {
      console.log(e)
      if (Number(e) < 0) {
        this.dialogForm.rebateRate = 0.1
      } else if (Number(e) > 1) {
        this.dialogForm.rebateRate = 0.9
      } else {
        this.dialogForm.rebateRate = e
      }
    },
    handleSwitch(e, walletRebateId) {
      updateWallet({
        walletRebateId,
        status: e
      }).then(res => {
        this.$successMsg('状态已更改')
        // this.hanleReset();
      })
    },
    hanleEdit(item) {
      console.log(item)
      this.dialogForm = {
        adminCompanyId: item.adminCompanyId,
        // mainId: "",
        // mainName: "",
        name: item.name,
        rebateRate: item.rebateRate,
        rabateSort: item.rabateSort,
        saleTypeCode: item.saleTypeCode,
        saleTypeId: item.saleTypeId,
        saleTypeName: item.saleTypeName,
        status: item.status,
        walletRebateId: item.walletRebateId
      }
      this.getDictList()
      this.dialogVisible = true
    },
    getDictList() {
      getDictList({
        sysDictEnum: 'PRODUCT_TYPE'
      }).then(res => {
        console.log(res)
        this.dictListData = res.data
      })
      const params = {
        pageNum: 1,
        pageSize: 10,
        saleCode: '',
        saleName: '',
        status: ''
      }
      getTypeList(params).then(res => {
        this.typeList = res.data.records
      })
    },
    hanleDateil(item) {
      this.type = 3
      this.dialogVisible = true
      this.dialogForm = {
        ...item
      }
    },
    hanelclose() {
      this.$refs.dialogForm.resetFields()
      this.hanleReset()

      this.type = ''
    },
    handelInfo() {
      // console.log(this.$refs.dialogForm);
      // this.listLoading = true;\

      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          if (this.type) {
            this.addLoading = true
            // this.hanleScreen(this.dialogForm.mainId);
            const params = {
              ...this.dialogForm
            }

            addWallet(params).then(res => {
              // console.log(res);
              this.$successMsg('添加成功')
              this.hanleReset()
              this.addLoading = false
            })
          } else {
            const upParams = {
              ...this.dialogForm
            }
            updateWallet(upParams).then(res => {
              this.$successMsg('修改成功')
              this.hanleReset()
            })
          }
        } 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.$refs.pageRef.refreshList()
    },
    hanleCancel() {
      // this.type = "";
      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;
      //   }
      // });
    }
  },
  components: {
    TemplatePage
  }
}
</script>

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