<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="3">
        <div class="head-container">
          <el-tabs v-model="checkStoreAreaId" @tab-click="handleQuery" tab-position="left">
            <el-tab-pane v-for="item in areaList" :label="item.name" :name="item.id + ''"></el-tab-pane>
          </el-tabs>
          <!-- <el-tree :data="clothTypeList" :props="defaultProps" :expand-on-click-node="false" node-key="id" default-expand-all highlight-current @node-click="handleNodeClick" /> -->
        </div>
      </el-col>
      <el-col :span="21">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入衣服名称" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">

          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-upload2" @click="handleImport">导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="el-icon-download" @click="handleExport">导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" fit highlight-current-row border stripe :data="clothItemPriceDataList">
          <el-table-column label="名称" align="center" prop="name" />
          <el-table-column label="洗衣方式" align="center">
            <el-table-column label="干洗" align="center" prop="dryCleaningModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.dryCleaningModePrice"> {{ scope.row.dryCleaningModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="水洗" align="center" prop="washModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.washModePrice"> {{ scope.row.washModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="织补" align="center" prop="darningModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.darningModePrice"> {{ scope.row.darningModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="单烫" align="center" prop="ironModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.ironModePrice"> {{ scope.row.ironModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="保值精洗" align="center" prop="fineWashModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.fineWashModePrice"> {{ scope.row.fineWashModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="护理" align="center" prop="nurseModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.nurseModePrice"> {{ scope.row.nurseModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>

            <el-table-column label="奢护洗" align="center" prop="shehuModePrice">
              <template slot-scope="scope">
                <span v-if="scope.row.shehuModePrice"> {{ scope.row.shehuModePrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
          </el-table-column>
          <el-table-column label="衣服类型" align="center">
            <el-table-column label="加厚" align="center" prop="plusPrice">
              <template slot-scope="scope">
                <span v-if="scope.row.plusPrice"> {{ scope.row.plusPrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="羊绒" align="center" prop="sheepPrice">
              <template slot-scope="scope">
                <span v-if="scope.row.sheepPrice"> {{ scope.row.sheepPrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
            <el-table-column label="真丝" align="center" prop="silkPrice">
              <template slot-scope="scope">
                <span v-if="scope.row.silkPrice"> {{ scope.row.silkPrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>

            <el-table-column label="儿童" align="center" prop="childPrice">
              <template slot-scope="scope">
                <span v-if="scope.row.childPrice"> {{ scope.row.childPrice }}</span>
                <span v-else>未设置</span>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="40%" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?storeAreaId=' + upload.storeAreaId"
        :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreArea } from '@/api/system/storeArea'
import { listClothItem, listClothItemPriceByArea } from '@/api/core/clothItem'
import { listType } from '@/api/core/clothType'
import { listWashMode } from '@/api/core/washMode'
import { getToken } from '@/utils/auth'
export default {
  name: 'Type',
  dicts: ['cloth_type'],
  data() {
    return {
      defaultProps: {
        label: 'name'
      },
      // 遮罩层
      loading: true,
      areaList: [],
      checkStoreAreaId: '1',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      clothItemPriceVO: null,
      clothItemPriceDataList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        typeId: null
      },
      // 表单参数
      form: {
        status: '0'
      },
      // 导入参数
      upload: {
        // 是否显示弹出层（单价导入）
        open: false,
        // 弹出层标题（单价导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        //区域id
        storeAreaId: '',
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/mapi/cloth/price/importData'
      },
      // 表单校验
      rules: {},
      clothTypeList: [],
      washModeList: [],
      clothVO: null,
      clothType0VO: null,
      clothType0V1: null,
      clothType0V2: null,
      clothType0V3: null
    }
  },
  created() {
    this.getAreaList()
    this.getWashModeList()
    this.getClothTypeList()
    this.getList()
  },
  methods: {
    /** 导入按钮操作 */
    handleImport() {
      if (!this.checkStoreAreaId) {
        this.$modal.msgError('请选择区域')
        return false
      }
      this.upload.storeAreaId = this.checkStoreAreaId
      const up_name = this.areaList.find((item) => item.id === parseInt(this.checkStoreAreaId))?.name

      this.upload.title = `【${up_name}】 洗衣方式单价导入`
      this.upload.open = true
    },
    /** 导出按钮操作 */
    handleExport() {
            if (!this.checkStoreAreaId) {
                this.$modal.msgError('请选择区域')
                return
            }
            let primary = {
                storeAreaId: this.checkStoreAreaId
            }
            const up_name = this.areaList.find((item) => item.id === parseInt(this.checkStoreAreaId))?.name
            this.downloadJson(
                '/mapi/cloth/price/exportData',
                {
                    ...primary
                },
                `${up_name}衣服单价列表.xlsx`
            )
        },
    /** 下载模板操作 */
    importTemplate() {
      this.download('/mapi/cloth/price/downloadTemplate', {}, `衣服单价导入模板.xlsx`)
    },

    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      var respMsg = response.code == '200' ? '' : '导入失败！'
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + respMsg + '<br/>' + response.msg + '</div>', '导入结果', { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    getAreaList() {
      listStoreArea({ pageSize: 10000, pageNum: 1, status: '0' }).then((res) => {
        this.areaList = res.rows
      })
    },
    /** 获取洗衣方式 */
    getWashModeList() {
      listWashMode({ status: '0' }).then((response) => {
        this.washModeList = response.rows
      })
    },
    /** 获取衣服类型 */
    getClothTypeList() {
      listType({ pageSize: 9999, pageNum: 1 }).then((response) => {
        this.clothTypeList = response.rows
      })
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeId = data.id
      this.clothItemPriceDataList = []
      this.handleQuery()
    },
    getList() {
      this.queryParams.areaId = parseInt(this.checkStoreAreaId)
      listClothItemPriceByArea(this.queryParams).then((response) => {
        this.clothItemPriceDataList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        rechargeAmount: null,
        giveAmount: null,
        givePointAmount: null,
        expireMonth: null,
        status: '0',
        delFlag: null,
        createById: null,
        createBy: null,
        createTime: null,
        updateById: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
<style>
.list_title {
    text-align: center;
    line-height: 40px;
}
</style>
