<template>
  <basic-container>
   <el-dialog  :modal-append-to-body="false" :visible.sync="userAgenterVisible" title="推广用户" width="70%">
     <useragenter ref="useragenterRef" :agenterId="selectAgenterId"></useragenter>
   </el-dialog>
   <el-dialog @close="closeOrderCommission"  append-to-body :close-on-click-modal="false" :visible.sync="viewOrderCommissionVisible" title="分佣记录" width="70%">
     <ordercommissioncomp  ref="ordercommissioncompRef" :agenterId="selectAgenterId"></ordercommissioncomp>
   </el-dialog>
    </el-dialog>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.agenter_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>

      <template slot="menu" slot-scope="scope">
        <el-button
                  type ="text"
                  size="mini"
                   icon="el-icon-s-promotion"
                   plain
                   v-if="permission.useragenter_view"
                   @click="viewUserAgenter(scope.row)">推广用户
        </el-button>
        <el-button
                  type ="text"
                  size="mini"
                   icon="el-icon-s-promotion"
                   plain
                   v-if="permission.ordercommission_view"
                   @click="viewOrderCommission(scope.row)">分佣记录
        </el-button>
      </template>

    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/guosen/agenter";
  import {mapGetters} from "vuex";
  import useragenter from "./useragenter.vue"
  import ordercommissioncomp from "@/components/ordercommission/ordercommissionComp"

  export default {
    components:{
      useragenter,
      ordercommissioncomp
    },
    data() {
      return {
        selectAgenterId: 0,
        userAgenterVisible: false,
        viewOrderCommissionVisible: false,
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          dialogWidth: "30%",
          column: [

            {
              label: "代理人手机号码",
              prop: "userPhone",
              search: true,
              searchLabelWidth: 150,
              searchSpan: 8,
              span: 22,
              row: true,
              labelWidth: 150,
              rules: [{
                required: true,
                message: "请输入代理人手机号码",
                trigger: "blur"
              }]
            },
            {
              label: "代理人级别",
              prop: "level",
              search: true,
              type: "select",
              searchLabelWidth: 150,
              searchSpan: 8,
              dataType: "number",
              span: 22,
              row: true,
              labelWidth: 150,
              dicUrl: "/api/cyzh-guosen/agentertype/list",
              props:{
                label: "name",
                value: "id"
              },
              dicFormatter: (res) =>{
                return res.data.records;
              },
              rules: [{
                required: true,
                message: "请输入代理人级别： 1-独家代理 2-战略合作伙伴",
                trigger: "blur"
              }]
            },
            {
              label: "是否启用",
              prop: "enabled",
              span: 22,
              search: true,
              row: true,
              value: 1,
              labelWidth: 150,
              type: "select",
              dicData: [
                {
                  label: "禁用",
                  value: -1
                },
                {
                  label: "启用",
                  value: 1
                }
              ],
              rules: [{
                required: true,
                message: "请输入-1: 禁用 1: 启用",
                trigger: "blur"
              }]
            },
            {
              label: "代理省份",
              prop: "province",
              search: true,
              span: 22,
              row: true,
              type: "select",
               cascaderItem: ["city"],
              dicUrl: "/api/cyzh-system/sysarea/list?size=1000&layerNumber=1",
              props:{
                label: "name",
                value: "pinyin"
              },
              dicFormatter: (res) =>{
                return res.data.records;
              },

              labelWidth: 150,
              display: true,

              rules: [{
                required: true,
                message: "请输入代理省份",
                trigger: "blur"
              }]
            },
            {
              label: "代理城市",
              search: true,
              prop: "city",
              type: "select",
              dicUrl: `/api/cyzh-system/sysarea/getCityByProvincePinyin?provincePinyin={{key}}`,
              props:{
                label: "name",
                value: "pinyin"
              },
              dicFormatter: (res) =>{
                return res.data;
              },
              span: 22,
              row: true,
              labelWidth: 150,
              display: true,
              rules: [{
                required: true,
                message: "请输入代理城市",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.agenter_add, false),
          viewBtn: this.vaildData(this.permission.agenter_view, false),
          delBtn: this.vaildData(this.permission.agenter_delete, false),
          editBtn: this.vaildData(this.permission.agenter_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    watch:{
     "form.level"(){


        if(this.form.level == 1){
           this.option.column[3].display = true;
           this.option.column[4].display = true;
        }else{
           this.option.column[3].display = false;
           this.option.column[4].display = false;
        }
     }
    },
    methods: {
      closeOrderCommission(){
        debugger
        this.viewOrderCommissionVisible = false;
      },
      viewOrderCommission(row){
          this.selectAgenterId = row.id;
          this.viewOrderCommissionVisible = true;
          this.$nextTick(()=>{
            this.$refs.ordercommissioncompRef.init();
          })
      },
      viewUserAgenter(row){
        this.selectAgenterId = row.id;
        this.userAgenterVisible = true;
        this.$nextTick(()=>{
          this.$refs.useragenterRef.init()
        });

      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
