<template>
  <div class="form-wrap">
    <el-form
      class="searchBox"
      ref="form"
      :model="form"
      :inline="true"
      label-width="140px"
    >
      <div class="block-title">
        查询条件
      </div>
      <el-form-item
        label="名称"
        prop="ticketTypeName"
      >
        <el-input v-model="form.ticketTypeName"></el-input>
      </el-form-item>
      <el-form-item
        label="类型"
        prop="category"
      >
        <el-select v-model="form.category">
          <el-option
            value=""
            label="全部"
          ></el-option>
          <el-option
            v-for="item in ticketCategory"
            :key="item.id"
            :value="item.id"
            :label="item.name">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="是否可售"
        prop="isSale"
      >
        <el-select v-model="form.isSale">
          <el-option
            label="全部"
            value=""
          ></el-option>
          <el-option
            label="是"
            :value="1"
          ></el-option>
          <el-option
            label="否"
            :value="0"
          ></el-option>
        </el-select>
      </el-form-item>
      <div class="btn-wrap">
        <el-button
          @click="reset"
        >
          重置
        </el-button>
        <el-button
          type="primary"
          @click="getTicketTypeList"
        >
          搜索
        </el-button>

        <el-button
          type="primary"
          @click="showDialog('add')"
        >
          新增票种
        </el-button>
      </div>
    </el-form>

    <div class="tableBox">
      <div class="block-title">
        现有票种
      </div>
      <el-table
        border
        stripe
        v-loading="loading"
        :data="tableData"
      >
        <el-table-column
          prop="name"
          label="票种名称"
        >
        </el-table-column>
        <el-table-column
          prop="id"
          label="票种ID"
          width="80"
        >
        </el-table-column>
        <el-table-column
          label="票种分类"
          width="80"
        >
          <template slot-scope="scope">
            {{ getTicketCate(scope.row.category) }}
          </template>
        </el-table-column>

        <el-table-column
          prop="price"
          label="窗口价格"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="onlinePrice"
          label="线上价格"
          width="80"
        >
        </el-table-column>
        <el-table-column
          prop="highPrice"
          label="虚拟高价"
          width="80"
        >
        </el-table-column>
        <el-table-column
          width="100"
          label="在售状态"
        >
          <template slot-scope="scope">
            <el-tag :type="scope.row.isSale | runStopColor">
              {{ scope.row.isSale ? '在售' : '禁售' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
        >
          <template slot-scope="scope">
            <!-- <el-link
              type="primary"
              size="small"
              @click="$refs.priceCalendar.show(scope.row.id,ticketPricePlanList)"
            >
              价格方案
            </el-link> -->
            <el-link
              type="primary"
              size="small"
              @click="showDialog('edit',scope.row)"
            >
              编辑
            </el-link>
            <br>
            <el-link
              type="primary"
              size="small"
              @click="showDialog('add',itemWithoutCategoryAndShorthand(scope.row))"
            >
              复制
            </el-link>
            <el-link
              type="primary"
              size="small"
              @click="deleteTicket(scope.row.id)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page.sync="form.pageNum"
        :page-size="10"
        layout="total, prev, pager, next"
        @current-change="getTicketTypeList"
        :total="total">
      </el-pagination>
    </div>
    <!-- 票种弹框 -->
    <ElDialog
      :title="dialogType === 'add' ? '新增票种' : '编辑票种'"
      width="1100px"
      v-model="dialogVisible">
      <!-- 新增与编辑 共用一个弹出 -->
      <TicketDialog
        :dialog-type="dialogType"
        :ticket-item="curentItem"
        :scenic-list="scenicList"
        :ticket-tag-list="ticketTagList"
        :member-type-list="memberTypeList"
        :invoice-seller="InvoiceSeller"
        @updateList="getTicketTypeList"
      ></TicketDialog>
    </ElDialog>

    <PriceCalendar ref="priceCalendar"></PriceCalendar>
  </div>
</template>

<script>
import moment from 'moment'
import { getTicketTypeList, deleteTicket, getScenic, updateTicket, listInvoiceSales } from '@/api/ticketType'
import { getTicketPricePlanList } from '@/api/ticketPricePlan'
import { getTicketTypeTagList } from '@/api/ticketTypeTag'
import { getMemberType } from '@/api/member'
import ElDialog from '@/components/Dialog'
import TicketDialog from './ticket/Dialog'
import PriceCalendar from './ticket/PriceCalendar'
import { TicketCategory } from './ticket/constant'

export default {
  computed: {
    ticketCategory () {
      return this.$store.state.app.ticketCategory
    }
  },
  data () {
    return {
      form: {
        ticketTypeName: '',
        category: '',
        keyWords3: '',
        isSale: '',
        pageNum: 1,
        pageSize: 10,
        keyWords6: '',
        keyWords7: '',
        keyWords8: '',
        ticketTypeName0: '1',
        // ticketTypeName1: 1, // 只查看自己绑定景点的票种
        ticketTypeName2: ''

      },
      loading: false,
      total: 0,
      tableData: [],
      memberTypeList: [],
      dialogVisible: false,
      // 弹框类型 'add' 或 'edit'
      dialogType: '',
      curentItem: {},
      scenicList: [],
      ticketTagList: [],
      ticketPricePlanList: [],
      InvoiceSeller: []
    }
  },
  created () {
    this.getScenicList()
    this.getTicketTypeList()
    // this.getMemberList()
    // this.getTicketTagList()
    // this.getTicketPricePlanList()
    // this.getInvoiceSeller()
  },
  components: {
    ElDialog,
    TicketDialog,
    PriceCalendar
  },
  methods: {
    getTicketCate (cate) {
      return TicketCategory[cate]
    },
    getInvoiceSeller () {
      listInvoiceSales({}).then(res => {
        this.InvoiceSeller = res?.data
      })
    },
    getTicketPricePlanList () {
      getTicketPricePlanList({}).then(res => {
        this.ticketPricePlanList = res.list
      })
    },
    getTicketTagList () {
      getTicketTypeTagList({}).then(res => {
        this.ticketTagList = res.children
      })
    },
    // 获取景点列表
    getScenicList () {
      getScenic().then(res => {
        this.scenicList = res?.data.records || []
      })
    },
    showDialog (type, item) {
      this.dialogType = type
      this.dialogVisible = true
      this.curentItem = item
      console.log(item)
    },
    // '创建票种失败，名称或助记符重复' 复制时去掉名称和助记符，待复制时用户填入
    itemWithoutCategoryAndShorthand (item) {
      let itemWithoutID = item
      item.name = '' // 名称
      item.shorthand = '' // 助记符
      return itemWithoutID
    },
    getMemberList () {
      getMemberType({
        pageNum: 0,
        pageSize: 0
      }).then(res => {
        this.memberTypeList = res.list
      })
    },
    // 获取票种列表
    getTicketTypeList () {
      this.loading = true
      getTicketTypeList(this.form).then(res => {
        this.tableData = res.data.records.map(item => {
          item.scenics = item.ticketTypeScenicList.map(s => s.scenicName).join('、')
          return item
        })
        console.log('this.tableData', this.tableData)
        this.total = res.data.total || 0
      }).finally(() => {
        this.loading = false
      })

      // 更新全局票种列表
      // this.$store.dispatch('getTicketTypeList')
    },
    // 删除票种
    deleteTicket (id) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTicket(id).then(res => {
          this.$message.success('删除成功')
          this.getTicketTypeList()
        })
      })
    },
    reset () {
      this.$refs.form.resetFields()
    },
    offSale (data) {
      let form = Object.assign({}, data)
      form.is_sell = 0
      updateTicket(form).then(res => {
        this.$message.success('操作成功')
        this.getTicketTypeList()
      })
    },
    onSale (data) {
      let saleEnd = moment(data.sell_enddate).hours(23).minutes(59).seconds(59)
      if (moment().isAfter(saleEnd)) {
        this.$alert('请修改票种可售日期', '票种已过期', {
          confirmButtonText: '知道了'
        })
      } else {
        let form = Object.assign({}, data)
        form.is_sell = 1
        updateTicket(form).then(res => {
          this.$message.success('操作成功')
          this.getTicketTypeList()
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.offSale-btn{
  color: #F56C6C
}
.onSale-btn{
  color: #67C23A
}
</style>
