<template>
  <div class="annualGuarantee">
    <!-- 通用代码 左侧按钮 右侧输入框 下拉框 -->
    <div class="view-common-header">
      <div class="view-common-header-left">
        <el-button type="success" @click="handleAdd">新增</el-button>
        <el-button type="warning" @click="handleDelete">删除</el-button>
        <el-button type="primary" @click="handleExport('导出')">导出</el-button>
      </div>
      <div class="view-common-header-right">
        <el-input @keyup.enter.native="fetchTableData('search')" placeholder="请输入内容" v-model="searchValue" class="input-select">
          <el-button @click="fetchTableData('search')" slot="append" icon="el-icon-search"></el-button>
        </el-input>
      </div>
    </div>
    <LTable ref="table" @select-all="selectAll" @selection-change="selection" :defaultFetch="true" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="pagination" />
    <CrudDialog v-bind="modalConfig" ref="NewDialogRef" @crudDialogSubmit="crudDialogSubmit"></CrudDialog>
  </div>
</template>

<script>
import { compareObj } from '@/utils/index'
import { columns, options, modalConfig, data } from './index'
import { socialSecurityPage, socialSecurityAdd, socialSecurityEdit, socialSecurityRemove } from '@/api/task/carrierFlightMission'

export default {
  name: 'AnnualGuarantee',
  components: {},
  data() {
    // 这里存放数据
    return {
      searchValue: '',
      removeArr: [],
      columns: columns(this),
      modalConfig: modalConfig(this),
      options,
      // 分页
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 20
      },
      tableData: data
    }
  },
  mounted() {},
  methods: {
    async fetchTableData(search) {
      let postData = {
        pageSize: this.pagination.pageSize,
        pageIndex: search ? 1 : this.pagination.pageIndex,
        keyword: this.searchValue
      }
      const { code, data } = await socialSecurityPage(postData)
      if (code == 200) {
        this.tableData = data.list
        this.pagination.total = data.totalCount
      }
    },
    handleAdd() {
      let config = {
        type: 'add',
        dataShow: {}, // 回填的值（字段对应上）
        title: '新增'
      }
      this.$refs.NewDialogRef.open(config)
    },
    handUpdate(row) {
      row.guaranteeYear = String(row.guaranteeYear)
      let config = {
        type: 'update',
        dataShow: row, // 回填的值（字段对应上）
        title: '编辑'
      }
      this.$refs.NewDialogRef.open(config)
    },
    handRemove(row) {
      this.$confirm('是否确认删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = {
            socialSecurityIds: [row.socialSecurityId]
          }
          await socialSecurityRemove(params)
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {})
    },
    handleDelete() {
      this.$confirm('是否确认删除选中数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = {
            socialSecurityIds: this.removeArr
          }
          await socialSecurityRemove(params)
          this.$refs.table.clearSelection()
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {})
    },
    // 全选
    selectAll(data) {
      this.removeArr = []
      if (data.length > 0) {
        data.forEach((item) => {
          this.removeArr.push(item.socialSecurityId)
        })
      } else {
        this.removeArr = []
      }
    },
    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item.socialSecurityId)
      })
    },
    handleExport(type) {
      let msg = ''
      type == '下载模板' ? (msg = '是否确认下载该模板') : (msg = `是否${type}该航材`)
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    async crudDialogSubmit(data) {
      const { dialogData, type } = data
      if (type == 'update') {
        await socialSecurityEdit(dialogData)
      } else {
        await socialSecurityAdd(dialogData)
      }
      this.fetchTableData()
      this.$message({
        type: 'success',
        message: '操作成功!'
      })
    },
    handPriceList(row) {
      this.$router.push(`/carrierStandard/safeguardCost/${row.socialSecurityId}`)
    }
  }
}
</script>

<style lang="scss" scoped></style>
