<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :operation="operation()"
    :tableAttributes="tableAttributes"
    :tableEvents="tableEvents"
    :optionsEvensGroup="optionsEvensGroup"
    :columnParsing="columnParsing"
  >
    <!-- 弹窗 -->
    <el-dialog
      title="物流公司"
      :visible.sync="showDialogForm"
      width="50%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :rules="rules" :model="diaLogForm" label-width="120px">
        <el-form-item label="物流公司名称" prop="logisticsCompany">
          <el-input v-model="diaLogForm.logisticsCompany"></el-input>
        </el-form-item>
        <el-form-item label="物流公司代码" prop="logisticsNumber">
          <el-input v-model="diaLogForm.logisticsNumber"></el-input>
        </el-form-item>
        <!-- <el-form-item label="提货人" prop="pickName">
          <el-input v-model="diaLogForm.pickName"></el-input>
        </el-form-item>
        <el-form-item label="提货人电话" prop="pickTel">
          <el-input v-model="diaLogForm.pickTel"></el-input>
        </el-form-item> -->
        <el-form-item label="客服电话" prop="customerTel">
          <el-input v-model="diaLogForm.customerTel"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleClose('form')">取 消</el-button>
        <el-button type="primary" @click="handelInfo('form')">确 定</el-button>
      </div>
    </el-dialog>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'
import {
  addInof,
  deleteInfo,
  getLogisticsList,
  updateInfo,
  getLogisticsV2,
  exportLogisticsV2
} from '@/api/basic_data/logistics'
import { downloadFiles } from '@/utils/util'
export default {
  components: { TemplatePage, Popu },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '新增',
              click: this.addOn(() => {
                this.hanleAdd()
              }),
              isRole: this.$checkBtnRole('add', this.$route.meta.roles)
            }
          ]
        ],
        [
          [
            {
              name: '批量删除',
              click: async () => {
                if (this.recordSelected.length === 0) {
                  this.$message.error('请选择需要删除的数据')
                  return
                }
                let ids = this.recordSelected.map(v => {
                  return v.id
                })
                let params = ids
                await deleteInfo(params)
                this.$refs.pageRef.refreshList()
                this.$message.success('批量删除成功')
              },
              isRole: this.$checkBtnRole('del', this.$route.meta.roles)
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: [],
      type: 1, // 1 ,2, 3
      diaLogForm: {
        id: null,
        customerTel: '',
        logisticsCompany: '',
        logisticsNumber: '',
        pickName: '',
        pickTel: ''
      },
      showDialogForm: false,
      screenForm: {
        name: '',
        username: '',
        telephone: '',
        customerTel: '',
        logisticsCompany: ''
      },
      ids: [],
      rules: {
        logisticsCompany: [{ required: true, message: '请输入物流公司名称', trigger: 'blur' }],
        logisticsNumber: [{ required: true, message: '请输入物流公司代码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getLogisticsV2(...p)
    },
    // 列表导出函数
    exportList: exportLogisticsV2,
    // 表格列解析渲染数据更改
    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 type="text" size="mini" class="textColor" onClick={() => this.editFn(row.id, row)}>
                编辑
              </el-button>
            ) : null}
            {this.$checkBtnRole('del', this.$route.meta.roles) ? (
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="内容确定删除吗？"
                onOnConfirm={() => this.hanleDelete(row.id)}
              >
                <el-button slot="reference" type="text" size="mini" class="textColor el-popover-left">
                  删除
                </el-button>
              </el-popconfirm>
            ) : null}
          </div>
        )
      }
    },
    handleClose() {
      this.$refs['form'].resetFields()

      this.addOff(() => {
        this.type = 2
        this.showDialogForm = false
      })()
    },
    //显示新增弹框
    hanleAdd() {
      this.type = 2
      this.showDialogForm = true
    },
    //显示编辑，编辑数据初始化
    editFn(id, row) {
      this.type = 1
      this.diaLogForm = {
        id,
        customerTel: row.customerTel,
        logisticsCompany: row.logisticsCompany,
        logisticsNumber: row.logisticsNumber,
        pickName: row.pickName,
        pickTel: row.pickTel
      }
      this.showDialogForm = true
    },

    //新增数据或者编辑数据请求接口  type 1 为编辑  type 2 新增
    handelInfo(formName) {
      console.log(this.type, '999')
      if (this.type === 1) {
        const params = {
          ...this.diaLogForm
        }
        updateInfo(params).then(res => {
          this.$successMsg('编辑成功')
          this.showDialogForm = false
          this.$refs.pageRef.refreshList()
          this.handleClose()
        })
        this.diaLogForm.id = null
      } else if (this.type === 2) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            addInof(this.diaLogForm).then(res => {
              this.$successMsg('保存成功')
              this.showDialogForm = false
              this.handleClose()
              this.$refs.pageRef.refreshList()
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      } else {
        this.showDialogForm = false
      }
    },
    //查看详情
    hanleDetail(detail) {
      this.type = 3
      this.showDialogForm = true
      this.diaLogForm = detail
      this.handleClose()
    },
    // 导出文档
    handleExport() {
      let screenData = {
        customerTel: this.diaLogForm.customerTel,
        logisticsCompany: this.diaLogForm.logisticsCompany
      }
      downloadFiles('logistics/export', screenData)
    },

    // 删除数据
    hanleDelete(id) {
      deleteInfo([id]).then(() => {
        this.$successMsg('删除成功')
        this.$refs.pageRef.refreshList()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.textColor {
  color: black;
}

::v-deep .el-date-editor {
  width: 100%;
}

::v-deep .el-select {
  width: 100%;
}

::v-deep .el-col-9 .el-button {
  padding: 5px;
}

::v-deep .el-dialog__header {
  background-color: #dddddd;
}

.base {
  padding: 20px 20px 0;
}

.table {
  margin-top: 12px;
}

.right {
  float: right;
}
</style>
