<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-custom__sdsinfo}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="捎带类型" prop="carryStatus">
          <el-select  v-model="dataForm.carryStatus" placeholder="请选择" clearable>
            <el-option
                v-for="item in carryStatusArr"
                :key="item.optionValue"
                :label="item.optionName"
                :value="item.optionValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布时间" prop="startTime">
          <el-date-picker
              v-model="dataForm.startTime"
              type="date"
              :picker-options="pickerBeginDateBefore"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="至" label-width="25px" prop="endTime">
          <el-date-picker
              v-model="dataForm.endTime"
              type="date"
              :picker-options="pickerBeginDateAfter"
              value-format="yyyy-MM-dd"
              format="yyyy-MM-dd"
              placeholder="选择日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%;">
        <el-table-column label="序号" type="index" show-overflow-tooltip align="center" width="50"></el-table-column>
        <el-table-column show-overflow-tooltip prop="departure" label="出发地" header-align="center" align="center" width="200px"></el-table-column>
        <el-table-column show-overflow-tooltip prop="destination" label="目的地" header-align="center" align="center" width="200px"></el-table-column>
        <el-table-column prop="departureTime" label="出发时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="mobile" label="联系电话" header-align="center" align="center"></el-table-column>
        <el-table-column show-overflow-tooltip prop="carryContent" label="捎带内容" header-align="center" align="center" ></el-table-column>
        <el-table-column prop="carryStatus" label="捎带类型" header-align="center" align="center" :formatter="carryStatusFormat"></el-table-column>
        <el-table-column prop="createdTime" label="发布时间" header-align="center" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序" header-align="center" align="center" width="100px"></el-table-column>
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button  type="text" size="mini" @click="sdsDetailHandle(scope.row.id)">查看</el-button>
            <el-button v-if="scope.row.shieldFlag === '0'" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="scope.row.shieldFlag === '0'"
                       type="text"
                       size="small"
                       @click="shield(scope.row.id)">屏蔽</el-button>
            <el-button v-if="scope.row.shieldFlag === '1'"
                       type="text"
                       size="small" style="color: darkgrey">已屏蔽</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <sds-detail v-if="detailVisible" ref="sdsDetail" @refreshDataList="getDataList"></sds-detail>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './sdsinfo-add-or-update'
import SdsDetail from './sds-detail'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/custom/sdsinfo/page',
        getDataListIsPage: true,
        deleteURL: '/custom/sdsinfo',
        deleteIsBatch: true
      },
      dataForm: {
        carryStatus: '',
        startTime: '',
        endTime: ''
      },
      carryStatusArr: [
        {
          'optionValue': '0',
          'optionName': '需要捎'
        },
        {
          'optionValue': '1',
          'optionName': '可以捎'
        }
      ],
      pickerBeginDateBefore: {
        disabledDate: time => {
          let beginDateVal = this.dataForm.startTime
          if (beginDateVal) {
            return time.getTime() > new Date(beginDateVal).getTime()
          }
        }
      },
      pickerBeginDateAfter: {
        disabledDate: time => {
          let EndDateVal = this.dataForm.endTime
          if (EndDateVal) {
            return time.getTime() < new Date(EndDateVal).getTime()
          }
        }
      },
      detailVisible: false
    }
  },
  components: {
    AddOrUpdate,
    SdsDetail
  },
  methods: {
    carryStatusFormat: function (row, column) {
      let carryStatus = row.carryStatus
      if (carryStatus === '0') {
        return '需要捎'
      } else if (carryStatus === '1') {
        return '可以捎'
      }
    },
    shield (val) {
      this.$prompt('', '请输入屏蔽原因', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        closeOnClickModal: false,
        closeOnPressEscape: false,
        inputPlaceholder: '100字以内',
        inputPattern: /^[\S\s]{1,100}$/,
        inputErrorMessage: '屏蔽原因不能为空且在100个字以内'
      }).then(({ value }) => {
        this.$http['post']('/custom/sdsinfo/shield', { id: val, shieldReason: value }).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.getDataList()
            }
          })
        }).catch(() => { })
      }).catch(() => {
      })
    },
    sdsDetailHandle (sdsId) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.sdsDetail.dataForm.id = sdsId
        this.$refs.sdsDetail.init()
      })
    }
  }
}
</script>
