<template>
  <el-dialog
    :title="title"
    top="5%"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <div class="store-transport-item-container">
      <vab-query-form>
        <vab-query-form-left-panel :span="24">
          <template #default="{ row }">
            <el-button
              icon="el-icon-plus"
              type="primary"
              @click="handleEdit(row)"
            >
              {{ __('添加') }}
            </el-button>
          </template>
        </vab-query-form-left-panel>
      </vab-query-form>
      <el-table
        v-loading="listLoading"
        border
        :data="list"
        :height="tableConfig.height"
      >
        <el-table-column align="center" :label="__('操作')" width="150px">
          <template #default="{ row }">
            <el-button type="text" @click="handleDelete(row)">
              {{ __('删除') }}
            </el-button>
          </template>
        </el-table-column>
        <el-table-column
          align="center"
          :label="__('默认数量')"
          prop="transport_item_default_num"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('默认运费')"
          prop="transport_item_default_price"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('增加数量')"
          prop="transport_item_add_num"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('增加运费')"
          prop="transport_item_add_price"
          show-overflow-tooltip
        />
        <el-table-column
          align="center"
          :label="__('区域城市名称')"
          prop="transport_item_city_name"
          show-overflow-tooltip
          width="250"
        >
          <template slot-scope="scope">
            <div v-if="scope.row.transport_item_city_name">
              <div
                v-if="
                  scope.row.transport_item_city_name === undefined ||
                  scope.row.transport_item_city_name.length <= 0
                "
              >
                <el-button type="text" @click="handleTable(scope.row)">
                  {{ __('请选择地区') }}
                </el-button>
              </div>
              <el-row
                v-for="(item, index) in scope.row.transport_item_city_name"
                :key="index"
              >
                <el-button type="text" @click="handleTable(scope.row)">
                  {{ __(item.provinces) }}
                </el-button>
              </el-row>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        :current-page="queryForm.pageNum"
        :layout="layout"
        :page-size="queryForm.pageSize"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />

      <edit ref="edit" @fetch-data="fetchData" />
      <item ref="item" @fetch-data="fetchData" />
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { doDelete, getList } from '@/api/store/transport/item'
  import Edit from './StoreTransportItemEdit'
  import Item from './StoreTransportEdit'

  export default {
    name: 'StoreTransportItem',
    components: { Edit, Item },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        title: '',
        dialogFormVisible: false,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
        selectedCityIds: [],
      }
    },
    created() {},
    methods: {
      __,
      async handleTable(row) {
        let obj = {
          transport_item_id: row.transport_item_id,
          selectedCityIds: this.selectedCityIds,
          transport_item_city_ids: row.transport_item_city_ids,
        }
        this.$refs['item'].showEdit(obj)
      },
      handleEdit() {
        this.$refs['edit'].showEdit(this.queryForm)
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            transport_item_id: row.transport_item_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      showItems(row) {
        this.queryForm.transport_type_id = row.transport_type_id
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.selectedCityIds = []
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
        this.initSelectedCity(data.items)
      },
      initSelectedCity(data) {
        if (data && data.length > 0) {
          data.forEach((item) => {
            this.selectedCityIds = this.selectedCityIds.concat(
              item.transport_item_city_ids
            )
          })
        } else {
          this.selectedCityIds = []
        }
      },
    },
  }
</script>
