<template>
    <div class="common-page-content">
      <div class="common-search-container" ref="contentPanel">
        <el-form
          ref="form"
          :model="searchForm"
          label-width="auto"
          label-position="right "
          :inline="true"
          type="flex"
          justify="left"
        >
          <el-form-item label="企业联系人">
            <el-input
              size="small"
              v-model="searchForm.enterpriseContact"
              clearable
              placeholder="请输入企业联系人"
            ></el-input>
          </el-form-item>
        </el-form>
        <div class="container-btn">
          <el-button
            size="small"
            icon="el-icon-search"
            type="danger"
            class="btn btn-plus"
            @click="handleSearch"
          >
            搜索
          </el-button>
          <el-button
            size="small"
            icon="el-icon-refresh-right"
            class="btn btn-plus"
            @click="clearSearchForm"
          >
            重置
          </el-button>
        </div>
      </div>
      <el-row
        class="common-table-container flex searchPanel"
        :style="SearchPanel"
      >
        <el-col :span="24" class="common-table-wrapper">
          <div class="common-btn-group">
            <el-button
              size="small"
              icon="el-icon-plus"
              type="primary"
              class="btn btn-plus"
              @click="handleAdd"
            >
              新增
            </el-button>
            <el-button
              size="small"
              slot="reference"
              icon="el-icon-delete"
              type="danger"
              class="btn btn-delete"
              @click="handleDelete"
            >
              {{ this.$t('common.delete') }}
            </el-button>
            <!-- <el-button
              size="small"
              icon="el-icon-bottom"
              type="primary"
              plain
              class="btn btn-plus"
            >
              导出数据
            </el-button> -->
          </div>
          <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
          <div class="common-table-content btn-pagenation">
            <el-table
              :border="true"
              stripe
              fit
              size="mini"
              v-loading="tableLoading"
              ref="multipleSelection"
              :data="tableData"
            >
              <el-table-column type="selection" min-width="5%" align="center">
              </el-table-column>
              <el-table-column
                prop="enterpriseName"
                show-overflow-tooltip
                min-width="130px"
                align="center"
                label="企业名称"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="enterpriseContact"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="企业联系人"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="enterpriseContactPhone"
                show-overflow-tooltip
                min-width="110px"
                align="center"
                label="企业联系电话"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="code"
                show-overflow-tooltip
                min-width="150px"
                align="center"
                label="送货地址编号"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="enterpriseContactAddress"
                show-overflow-tooltip
                min-width="150px"
                align="center"
                label="送货地址"
                resizable
              >
              </el-table-column>
              <el-table-column
                prop="defaultStatus"
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="是否默认"
                resizable
              >
              <template v-slot="{row}">
                {{ getDictName('qyshdzmrzt', row.defaultStatus) }}
              </template>
              </el-table-column>
              <!-- <el-table-column
                show-overflow-tooltip
                min-width="100px"
                align="center"
                label="启用状态"
                prop="enablingStatus"
                resizable
              >
                <template v-slot="{row}">
                  <el-switch
                    @change="(val) => switchChange(val, row)"
                    v-model="row.enablingStatus"
                    active-color="#13ce66"
                    inactive-color="#ff4949"
                    active-text="启用"
                    inactive-text="禁用"
                    :active-value="1"
                    :inactive-value="0"
                  >
                  </el-switch>
                </template>
              </el-table-column> -->
              <el-table-column min-width="120px" align="center" label="操作">
                <template v-slot="{row}">
                  <template>
                    <el-button
                      icon="el-icon-edit"
                      type="primary"
                      plain
                      size="small"
                      @click="handleEdit(row)"
                      >编辑</el-button
                    >
                  </template>
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="common-pagenation">
            <el-pagination
              ref="paginationPanel"
              background
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="params.pageBean.page"
              :page-sizes="[20, 30, 40, 50]"
              :page-size="params.pageBean.pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="params.pageBean.total"
            >
            </el-pagination>
          </div>
        </el-col>
      </el-row>
    </div>
  </template>
  
  <script>
  import supplierManger from '@/api/supplierManger.js'
  import pagination from '@/components/pagination'
  import {TableMixin} from '@/mixins/tableMixin'
  import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
  import req from '@/request.js'
  import utils from '@/utils.js'
  export default {
    name: 'DemandEntry',
    components: {
      pagination
    },
    mixins: [TableMixin],
    data() {
      return {
        tableMaxHeight: 400,
        loading: false,
        tableLoading: false,
        tableData: [],
        multipleSelection: [],
        params: {
          pageBean: {
            page: 1,
            pageSize: 20,
            total: 0
          }
        },
        queryParam: {
          pageBean: {
            page: 1,
            pageSize: 20,
            total: 0
          }
        },
        searchForm: {}, // 顶部搜索对象
      }
    },
    created() {
      if (this.$route.params.pageBean) {
        this.params = this.$route.params.pageBean
        this.pageResult = this.$route.params.pageBean.pageBean
      }
      this.loadData(this.params)
    },
    methods: {
      getDictName(code, value) {
        return utils.getDictName(code, value)
      },
      //获取数据
      loadData(params) {
        this.tableLoading = true
        this.$http.post('${portal}/bizDeliveryAddress/v1/queryByPageOrg',params).then(res=>{
          this.tableLoading = false
          const data = (res && res.data) || {}
          this.tableData = data.rows || []
          this.params.pageBean = {
            page: data.page,
            pageSize: data.pageSize,
            total: data.total
          }
        })
      },
      //添加
      handleAdd() {
        this.$router.push({
          path: '/addressDialog'
        })
      },
      // 编辑
      handleEdit(rows) {
        this.$router.push({
          path: `/addressDialog/${rows.id}`,  
          query: {pageBean: this.params}
        })
      },
      // 详情
      handleDetail(rows) {
        this.$router.push({
          path: `/onboardingDetail/${rows.id}`,
          query: {pageBean: this.params}
        })
      },
      switchChange(val, row) {
      console.log(row)
      let statusValue = {
        ids: [row.id],
        status: val,
      }
      function recursivefilter(arr) {
        for (let item of arr) {
          if (item.id) {
            statusValue.ids.push(item.id)
          }
          if (item.children.length > 0) {
            recursivefilter(item.children)
          }
        }
        return arr
      }
      recursivefilter(row.children)
      basicData.bizMaterialCategoryUpdateStatus(statusValue).then((res) => {
        if (res.state) {
          this.$message.success(res.message)
          this.loadData()
        }
      })
    },
      // 删除
      handleDelete() {
        let selection = this.$refs.multipleSelection.selection
        if (selection.length === 0) {
          this.$alert('请选择要删除的数据！', this.$t('common.tips'), {
            confirmButtonText: this.$t('common.confirm'),
            type: 'warning'
          })
        } else {
          let ids = []
          selection.forEach(item => {
            let str = item.id
            ids.push(str)
          })
          ids = ids.join(',')
          this.$confirm(this.$t('common.sureDelete'), this.$t('common.tips'), {
            confirmButtonText: this.$t('common.confirm'),
            cancelButtonText: this.$t('common.cancel'),
            type: 'warning'
          }).then(() => {
            this.tableLoading = true
            this.$http.remove('${portal}/bizDeliveryAddress/v1/?ids='+ids).then(res=>{
              this.tableLoading = false
              if (res.data.state) {
                this.$message.success('删除成功！')
                this.handleSearch()
              }
            })
          })
        }
      },
      //重置
      clearSearchForm() {
        this.searchForm = {}
        this.params.pageBean.page=1
        this.handleSearch()
      },
      // 顶部搜索
      handleSearch() {
        let obj = this.searchForm
        let querys = []
        for (let i in obj) {
          if (obj[i]) {
            querys.push({
              group: 'main',
              operation:
                i == 'enterpriseContact' || i == 'supplier_name_' ? 'LIKE' : 'EQUAL',
              parentGroup: '',
              property: i,
              relation: 'AND',
              value: obj[i]
            })
          }
        }
        this.params.pageBean.page=1
        this.params.querys = querys
        this.loadData(this.params)
      },
      handleCurrentChange(page) {
        this.params.pageBean = {...this.params.pageBean, page}
        this.loadData(this.params)
      },
      handleSizeChange(pageSize) {
        this.params.pageBean = {...this.params.pageBean, pageSize}
        this.loadData(this.params)
      }
    },
    computed: {
      SearchPanel() {
        return {
          '--searchPanel': this.searchPanel + 'px'
        }
      }
    }
  }
  </script>
  
  <style lang="scss" scoped>
  .cloumn-btn {
    border: 1px solid #2761ff;
    padding: 3px 10px;
    border-radius: 5px;
    &:nth-child(1) {
      margin-right: 5px;
    }
    &:nth-child(2) {
      margin-right: 5px;
    }
    &:nth-child(1):hover {
      background-color: #2761ff;
      color: #fff;
    }
    &:nth-child(2):hover {
      background-color: #2761ff;
      color: #fff;
    }
    /deep/ i,
    span {
      font-size: 16px;
    }
  }
  
  /deep/ .el-tree-node__content {
    padding: 5px 0;
  }
  
  /deep/ .el-table thead tr th {
    background-color: #eee !important;
    border-right: 1px solid #dfe2e8 !important;
  }
  /deep/ .el-table {
    border-collapse: collapse !important;
  }
  ::v-deep .el-switch__core {
      width: 44px;
      height: 24px;
      border-radius: 100px;
      border: none;
    }
    ::v-deep .el-switch__core::after {
      width: 20px;
      height: 20px;
      top: 2px;
    }
    ::v-deep .el-switch.is-checked .el-switch__core::after {
      margin-left: -21px;
    }
    /*关闭时文字位置设置*/
    ::v-deep .el-switch__label--right {
      position: absolute;
      z-index: 1;
      right: 6px;
      margin-left: 0px;
      color: rgba(255, 255, 255, 0.9019607843137255);
      span {
        font-size: 12px;
      }
    }
    /* 激活时另一个文字消失 */
    ::v-deep .el-switch__label.is-active {
      display: none;
    }
    /*开启时文字位置设置*/
    ::v-deep .el-switch__label--left {
      position: absolute;
      z-index: 1;
      left: 5px;
      margin-right: 0px;
      color: rgba(255, 255, 255, 0.9019607843137255);
      span {
        font-size: 12px;
      }
    }
  </style>
  