<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <SuperSearch
        :searchForm="pageResult"
        :basisOption="basisOption"
        @is-show-advance="calcTableHeight()"
        @search="handleSearch"
      >
        <template slot="basic">
          <div>123</div>
        </template>
      </SuperSearch>

      <!-- <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        type="flex"
        justify="left"
      >
        <el-row :gutter="25">
          <el-col :span="7">
            <el-form-item label="路线编号">
              <el-input
                size="small"
                v-model="searchForm.code"
                clearable
                placeholder="请输入路线编号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item label="路线名称">
              <el-input
                size="small"
                v-model="searchForm.name"
                clearable
                placeholder="请输入路线名称"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="所属公司">
              <el-input
                size="small"
                v-model="searchForm.companyName"
                clearable
                placeholder="请输入所属公司"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button
              size="small"
              icon="el-icon-search"
              type="primary"
              class="btn btn-plus"
              @click="handleSearch"
            >
              {{ this.$t('common.search') }}
            </el-button>
          </el-col>
          <el-col :span="2">
            <el-button
              size="small"
              icon="el-icon-refresh-right"
              class="btn btn-plus"
              @click="clearSearchForm"
            >
              重置
            </el-button>
          </el-col>
        </el-row>
      </el-form> -->
    </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"
          >
            {{ this.$t('common.add') }}
          </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>
        </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"
            height="100%"
            width="100%"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleEdit"
          >
            <el-table-column type="selection" width="55" align="center">
            </el-table-column>
            <el-table-column
              prop="code"
              width="100"
              label="路线编号"
              align="left"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="name"
              show-overflow-tooltip
              width="100"
              align="left"
              label="路线名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="companyName"
              show-overflow-tooltip
              align="left"
              label="所属公司"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="start"
              show-overflow-tooltip
              align="left"
              label="起点地址"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="end"
              show-overflow-tooltip
              align="left"
              label="终点地址"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="startPeg"
              show-overflow-tooltip
              align="left"
              label="起点桩点"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="endPeg"
              show-overflow-tooltip
              align="left"
              label="终点桩点"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="mileage"
              show-overflow-tooltip
              width="160"
              align="left"
              label="里程（千米）"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="remarks"
              show-overflow-tooltip
              min-width="60px"
              align="left"
              label="备注"
              resizable
            >
            </el-table-column>
            <el-table-column width="120" align="left" 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="queryParam.pageBean.pageNum"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <!-- 弹层 -->
    <el-dialog
      :title="dialog.title"
      :visible.sync="dialog.visible"
      width="60%"
      top="8vh"
      :before-close="handleDialogClose"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="dialogForm"
        :rules="rules"
        label-width="100px"
        label-position="right"
        v-loading="loading"
        :disabled="disabledDetils"
        border
      >
        <RecordPopover
          v-show="dialogForm.bizId"
          :id="dialogForm.bizId"
        ></RecordPopover>
        <el-row :gutter="20" type="flex" class="flex-wrap">
          <el-col :span="11">
            <el-form-item label="路线编号" prop="code">
              <el-input
                v-model="dialogForm.code"
                :disabled="disabled"
                placeholder="请输入路线编号"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="路线名称" prop="name">
              <el-input
                v-model="dialogForm.name"
                :disabled="disabled"
                placeholder="请输入路线名称"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="11">
            <el-form-item label="所属公司" prop="companyName">
              <el-input
                v-model="dialogForm.companyName"
                :disabled="disabled"
                placeholder="请输入所属公司"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="里程" prop="mileage">
              <el-input-number
                v-model="dialogForm.mileage"
                :disabled="disabled"
                controls-position="right"
                placeholder="请输入里程"
                :min="0"
              ></el-input-number>
              千米
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="起点地址" prop="start">
              <el-input
                v-model="dialogForm.start"
                :disabled="disabled"
                placeholder="请输入起点地址"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="终点地址" prop="end">
              <el-input
                v-model="dialogForm.end"
                :disabled="disabled"
                placeholder="请输入终点地址"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="起点桩点" prop="startPeg">
              <el-input
                v-model="dialogForm.startPeg"
                :disabled="disabled"
                placeholder="请输入起点桩点"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="终点桩点" prop="endPeg">
              <el-input
                v-model="dialogForm.endPeg"
                :disabled="disabled"
                placeholder="请输入终点桩点"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="22">
            <el-form-item label="备注">
              <el-input
                type="textarea"
                v-model="dialogForm.remarks"
                placeholder="备注..."
                height="20%"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialog.visible = false" size="small">
          取消
        </el-button>
        <template v-if="!disabledDetils">
          <el-button type="primary" @click="submit(1)" size="small">
            提交
          </el-button>
        </template>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
import SuperSearch from '../components/superSearch.vue'
import req from '@/request.js'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'DemandEntry',
  components: {
    pagination,
    SuperSearch
  },
  mixins: [TableMixin],
  data() {
    return {
      disabledDetils: false,
      currentPage: 1,
      tableMaxHeight: 400,
      loading: false,
      tableLoading: false,
      tableData: [],
      multipleSelection: [],

      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        }
      },

      searchForm: {}, // 顶部搜索对象
      deleteIds: '', // 是否选中列表解锁删除按钮
      activeCollapse1: ['1', '2'],
      fileList: [], // 文件上传
      dialog: {
        title: '添加',
        visible: false
      },
      dialogForm: {},
      disabled: false, // 是否禁止输入输入
      edit: false, // 是否是修改功能
      currentDataDel: [], // 删除时排查是否有在审核的数据
      rules: {
        code: {
          required: true,
          message: '请输入路线编号',
          trigger: 'blur'
        },
        name: {
          required: true,
          message: '请选择路线名称',
          trigger: 'blur'
        }
      },
      basisOption: [
        {
          itemLabel: '证件类型',
          itemProp: 'cardType',
          itemType: 'radio',
          searchType: 'basic',
          labelWidth: '100px',
          option: [
            {label: '营业执照', value: '0'},
            {label: '身份证', value: '1'},
            {label: '驾驶证', value: '2'},
            {label: '军官证', value: '3'},
            {label: '护照', value: '4'}
          ]
        },
        {
          itemLabel: '路线编号',
          itemProp: 'code',
          itemType: 'input',
          labelWidth: '90px',
          searchType: 'advance'
        },
        {
          itemLabel: '路线名称',
          itemProp: 'name',
          itemType: 'input',
          labelWidth: '90px',
          searchType: 'advance'
        },
        {
          itemLabel: '所属公司',
          itemProp: 'companyName',
          itemType: 'input',
          labelWidth: '90px',
          searchType: 'advance'
        }
      ]
    }
  },
  mounted() {
    this.calcTableHeight()
    this.$root.$on('resize', () => {
      this.calcTableHeight(500)
    })
  },
  created() {
    this.loadData(this.params)
  },
  methods: {
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      routerManagement.queryEventsList(params).then(res => {
        console.log(res, 'res')
        this.tableLoading = false
        const data = (res && res.data) || {}
        this.tableData = data.value.rows || []
        console.log('返回的表格数据', this.tableData)
        this.pageResult = {
          page: data.value.page,
          pageSize: data.value.pageSize,
          total: data.value.total
        }
      })
    },
    //添加
    handleAdd() {
      this.edit = false
      this.disabledDetils = false
      this.dialogForm = {}
      this.dialog.title = '添加'
      this.dialog.visible = true
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.edit = true
      this.disabledDetils = false
      this.dialogForm = rows || {}
      this.dialogForm = {
        ...rows,
        approvalComments: this.dialogForm.approvalComments
      }
      this.dialog.title = '编辑'
      this.dialog.visible = true
    },

    // 删除
    handleDelete() {
      this.$confirm(this.$t('common.sureDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        routerManagement.batchDelete(this.deleteIds).then(res => {
          this.loadData(this.params)
        })
      })
    },

    // 顶部搜索
    handleSearch(searchForm) {
      debugger
      let obj = searchForm
      console.log(obj)
      let querys = []
      for (let i in obj) {
        if (obj[i]) {
          querys.push({
            group: 'main',
            operation:
              i == 'name' || i == 'code' || i == 'companyName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i]
          })
        }
      }
      console.log(querys, 'querys')
      this.params.querys = querys
      console.log(this.params, 'params')
      this.loadData(this.params)
    },
    // 弹层关闭
    handleDialogClose() {
      this.dialog.visible = false
    },
    // 表单提交保存
    submit(status) {
      this.dialogForm.status = status
      // 删除approvalComments参数确保能够保存
      Reflect.deleteProperty(this.dialogForm, 'approvalComments')
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          routerManagement
            .batchSubmit(this.dialogForm)
            .then(res => {
              console.log('res', res)
              if (res.data.state) {
                this.dialog.visible = false
                this.loadData(this.params)
              }
              if (!res.errorMsg) {
                this.$message({
                  message: !this.edit ? '添加成功' : '修改成功',
                  type: 'success'
                })
                this.editVisible = false
              }
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    },
    handleSelectionChange(currentData) {
      this.currentDataDel = currentData
      if (currentData.length === 0) return (this.deleteIds = '')
      let ids = []
      currentData.forEach(item => {
        ids.push(item.id)
      })
      // this.$message.error(`【${item.demandTopic}】已上报待审核不能删除！`)
      this.deleteIds = ids.join(',')
    },
    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;
  }
  // &:nth-child(2):hover {
  //   background-color: #f56c6c;
  //   color: #fff;
  //   border-color: #f56c6c;
  // }
  /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;
}
</style>
