<template>
  <nm-container>
    <nm-list ref="list" v-bind="list" @selection-change="handleSelectionChange">
      <!--查询条件-->
      <template v-slot:querybar>

        <el-form-item label="关键字：" prop="key">
          <el-input v-model="list.model.key" placeholder="名称/关键字" clearable />
        </el-form-item>

        <el-form-item label="经销商：" prop="dtCD">
          <channel-select ref="ChannelSelect" v-model="list.model.stores" @updateSelection="updateChannel" />
        </el-form-item>

        <el-form-item label="开始：" prop="startDate">
          <el-date-picker v-model="list.model.startDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>

        <el-form-item label="结束：" prop="endDate">
          <el-date-picker v-model="list.model.endDate" type="date" placeholder="选择日期" value-format="yyyy-MM-dd"></el-date-picker>
        </el-form-item>

      </template>

      <!--按钮-->
      <template v-slot:querybar-buttons>
        <nm-button v-bind="buttons.singimport" @click="batchGenerate" />
        <nm-button v-bind="buttons.add" @click="add" />
      </template>

      <!--自定义列-->
      <!-- <template v-slot:col-name="{row}">
        <nm-button :text="row.name" type="text" />
      </template> -->

      <!--类型-->
      <!-- <template v-slot:col-saleType="{ row }">
        <el-tag type="success" v-if="row.saleType === 0">正常销量</el-tag>
        <el-tag type="warning" v-if="row.saleType === 1">赠酒</el-tag>
      </template> -->

      <!--操作列-->
      <template v-slot:col-operation="{ row }">
        <nm-button v-bind="buttons.edit" @click="edit(row)" />
        <nm-button v-bind="buttons.generate" :id="row.id" @click="generate(row)" />
        <!-- <nm-button v-bind="buttons.singexport" :id="row.id" @click="exportSign(row)" />
        <nm-button v-bind="buttons.singimport" :id="row.id" @click="exportSign(row)" /> -->
      </template>
    </nm-list>

    <!-- <save-page :id="curr.id" :visible.sync="dialog.save" @success="refresh" /> -->
    <save-page :visible.sync="dialog.save" @success="refresh" />
  </nm-container>
</template>
<script>
import { mixins } from 'dcms-ui'
import page from './page'
import cols from './cols'

import SavePage from '../components/save'
import ChannelSelect from 'dcms-module-crm/src/components/channel-select'
//import TerminalSelect from 'dcms-module-crm/src/components/terminal-select'

const api = $api.tPM.displayPolicy

export default {
  name: page.name,
  mixins: [mixins.list],
  components: { SavePage, ChannelSelect },
  data() {
    return {
      list: {
        title: page.title,
        cols,
        action: api.query,
        multiple: true,
        operationWidth: '200',
        model: {
          /** 经销商编码 */
          dtCD: '',
          stores: [],
          /** 终端编码 */
          inCD: '',
          terminals: [],
          /** 结账年月(6位数字) */
          date: '',
          /** 销量类型 */
          saleType: '-1',
          multipleSelection: [],
        },
        /**导出配置 */
        exportOptions: {
          /**启用导出按钮 */
          enabled: true,
          /**导出数据的方法 */
          action: api.exportData,
          /**启用高级配置 */
          advanced: true,
          /**导出按钮位置 */
          btnLocation: 'querybar',
          /**导出按钮权限编码 */
          btnCode: page.buttons.export.code,
          /**默认是否显示标题 */
          showTitle: true,
          /**默认是否显示版权信息 */
          showCopyright: true,
          /**默认是否显示列名 */
          showColName: true,
          /**默认是否显示导出日期 */
          showExportDate: true,
          /**默认是否显示导出人 */
          showExportPeople: true,
        },
      },
      selectdate: {
        method: this.queryDateOptions,
      },
      select: {
        method: this.queryOptions,
      },
      removeAction: api.remove,
      buttons: page.buttons,
    }
  },
  methods: {
    //单项导出
    exportSign(row) {
      this._success('功能开发中！')
      console.log(row)
    },
    updateChannel(items) {
      let first = items[0]
      this.list.model.dtCD = first.code
      this.$refs.list.refresh()
    },
    updateTerminal(items) {
      let first = items[0]
      this.list.model.inCD = first.code
      this.$refs.list.refresh()
    },
    queryDateOptions() {
      return new Promise((resolve) => {
        var date = []
        for (let index = 0; index < 12; index++) {
          var i = index
          if (i < 9) i = '0' + (i + 1)
          else i = i + 1
          var day = { label: '2021' + i, value: '2021' + i }
          date.push(day)
        }
        resolve(date)
      })
    },
    //状态选择
    queryOptions() {
      return new Promise((resolve) => {
        resolve([
          {
            label: '现饮',
            value: '-1',
          },
          {
            label: '非现饮',
            value: 0,
          },
          {
            label: '现代零售',
            value: 1,
          },
        ])
      })
    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.list.toggleRowSelection(row)
        })
      } else {
        this.$refs.list.clearSelection()
      }
    },
    handleSelectionChange(val) {
      //console.log(val)
      this.list.model.multipleSelection = val
    },
    //生成
    generate(row) {
      this._confirm(`您确定要导出核销数据吗？`).then(() => {
        this._openLoading('请稍后...')
        console.log(row)
        api
          .generate(row.id)
          .then(() => {
            this._closeLoading()
            this.$refs.list.refresh()
            this._success('生成完成！')
          })
          .catch(() => {
            this._closeLoading()
            this._error('生成失败！')
          })
      })
    },
    batchGenerate() {
      if (this.list.model.multipleSelection.length == 0) {
        this._success('请选择要导出项目！')
        return false
      }
      this._confirm(`您确定要导出核销数据吗？`).then(() => {
        this._openLoading('请稍后...')
        api
          .batchGenerate(this.list.model.multipleSelection)
          .then(() => {
            this._closeLoading()
            this.$refs.list.refresh()
            this._success('生成完成！')
          })
          .catch(() => {
            this._closeLoading()
            this._error('生成失败！')
          })
      })
    },
  },
}
</script>
