<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" :size="ELsize" plain icon="el-icon-plus" @click="addOrUpdateHandle()">新增模板</el-button>
      </el-col>
    </el-row>
    <div v-for="(item, index) in dataList" :key="index">
      <div>
        <el-row class="mt30">
          <el-col :span="12">
            <div> <span style="font-weight:700;">{{ item.name }}</span> <span class="ml10">设置默认模板 <el-switch :size="ELsize"
                  v-model="item.general" @change="general($event, item.id)">
                </el-switch> </span> </div>
          </el-col>
          <el-col :span="12">
            <el-button :size="ELsize" v-if="!item.general" type="danger" class="btn-compile-right"
              @click="deleteTemp(item)">删除</el-button>
            <el-button :size="ELsize" type="primary" class="btn-compile-right"
              @click="addOrUpdateHandle(item.id)">修改</el-button>
          </el-col>
        </el-row>

        <el-table :data="item.ruleList" row-key="id" border style="width: 100%;margin-top: 20px; ">
          <el-table-column prop="phone" align="center" label="可配送区域">
            <template slot-scope="scope">
              <div>
                <div v-if="scope.$index == 0">默认区域</div>
                <div v-else> {{ scope.row.areaRuleLiteral }}
                  <span>
                    <el-button type="text"
                      @click="addEditArea(item.id, scope.row.areaRule, item.defaultRule, scope.row)">修改</el-button>
                  </span>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 0" label="首件（个）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.startStandards"></el-input>
                <div v-else>{{ scope.row.startStandards }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 1" label="首重量（Kg）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.startStandards"></el-input>
                <div v-else>{{ scope.row.startStandards }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 2" label="首体积 （M3）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.startStandards"></el-input>
                <div v-else>{{ scope.row.startStandards }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="运费（元）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.startFreight"></el-input>
                <div v-else>{{ scope.row.startFreight }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 0" label="续件（个）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{ scope.row.continued }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 1" label="续重量（Kg）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{ scope.row.continued }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" v-if="item.freightComputeMode == 2" label="续体积（M3）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id" v-model="scope.row.continued"></el-input>
                <div v-else>{{ scope.row.continued }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="续费（元）">
            <template slot-scope="scope">
              <div>
                <el-input v-if="scope.$index != 0 && currEdit == scope.row.id"
                  v-model="scope.row.continuedFreight"></el-input>
                <div v-else>{{ scope.row.continuedFreight }}</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column fixed="right" header-align="center" align="center" label="操作">
            <template slot-scope="scope">
              <el-button type="text" :size="ELsize" v-if="scope.$index != 0"
                @click="edit(item.id, scope.row)">{{ scope.row.id == currEdit ? '完成' : '修改' }}</el-button>
              <el-button type="text" class="c-error" :size="ELsize" v-if="scope.$index != 0"
                @click="deleteTempRule(scope.row, item.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="mt30" v-if="item.freePost == false">
          <el-button :size="ELsize" type="primary" @click="addEditArea(item.id, {}, item.defaultRule)">点击新增配送区域</el-button>
        </div>
      </div>
    </div>

    <!--添加，编辑 -->
    <formBoardEdit @result="updataList" v-if="addOrUpdateVisible" ref="formBoardEditRef"></formBoardEdit>
    <!--地区选择-->
    <region v-if="regionOpen" ref="region" @result='regionResult'></region>
  </div>
</template>

<script>
import region from './region'
import formBoardEdit from './formBoardEdit'
// import seviceApi from '@/api/serviceApi.js'
import { freightTemplateList, freightTemplateDefault, freightTemplateDel, freightTemplateInfo, freightTemplateRuleDel, freightTemplateRuleEdit } from '@/api/operating.js'
export default {
  data() {
    return {
      dataForm: {},
      dataList: [],
      dataListLoading: false,
      addOrUpdateVisible: false,
      regionOpen: false,
      currEdit: null
    }
  },
  components: {
    formBoardEdit,
    region
  },
  mounted() {
    this.getDateList()
  },
  methods: {
    /* 地区选择返回 */
    regionResult(e) {
      this.regionOpen = false
      this.getDateList()
    },
    // 更新列表
    updataList(e) {
      this.getDateList()
    },
    // 设置默认模板
    general(e, id) {
      // freightTemplateInfo(id).then(res => {
      //   if (res && res.code == 200) {
      //     res.data.general = e
      freightTemplateDefault(id).then(res => {
        if (res && res.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          this.getDateList()
        } else {
          this.getDateList()
        }
      })
      //   }
      // })
    },
    getDateList() {
      freightTemplateList().then(res => {
        if (res && res.code == 200) {
          this.dataList = res.data
        }
      })
    },
    // 添加，编辑
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.formBoardEditRef.init(id || '')
      })
    },
    // 选择地区
    addEditArea(id, areaRule, defaultRule, item) {
      this.regionOpen = true
      this.$nextTick(() => {
        this.$refs.region.init(id, areaRule, defaultRule, item)
      })
    },

    edit(id, row) {
      if (this.currEdit != row.id) {
        // 编辑
        this.currEdit = row.id
      } else {
        // 完成
        this.currEdit = null
        let param = {
          id,
          ...row
        }
        freightTemplateRuleEdit(row).then(res => {
          if (res && res.code == 200) {
            this.$message.success('修改成功！')
          }
        })
      }
    },

    // 删除运费模板
    deleteTemp(row) {
      this.$confirm(`是否删除模板 [ ${row.name} ]?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        freightTemplateDel(row.id).then(res => {
          if (res && res.code == 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.getDateList()
          }
        })
      }).catch(() => { })
    },

    // 商家删除运费模板规则
    deleteTempRule(row, templateId) {
      console.log(row)
      this.$confirm(`是否删除 [ ${row.areaRuleLiteral} ] ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = templateId + '/' + row.id
        freightTemplateRuleDel(templateId, row.id).then(res => {
          if (res && res.code == 200) {
            this.$message.success('删除成功！')
            this.getDateList()
          }
        })
      }).catch(() => { })
    }
  }
}
</script>
<style scoped lang="scss">
.mt30 {
  margin-top: 30px;
}

.btn-compile-right {
  float: right;
  margin-left: 10px;
}

.el-input--medium .el-input__inner {
  text-align: center;
}
</style>
