<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.address" clearable class="filter-item" style="width: 500px;" placeholder="请输入客户地址" />
      <el-select v-model="listQuery.cityId" clearable style="width: 150px" class="filter-item" filterable placeholder="请选择城市">
        <el-option v-for="item in city" :key="item.id" :label="item.cityName" :value="item.id" />
      </el-select>
      
      <!-- <el-select v-model="listQuery.orderChannel" filterable style="width: 160px" class="filter-item" placeholder="请选择渠道" clearable
         >
        <el-option v-for="item in channels" :key="item.id" :label="item.name" :value="item.id" />
      </el-select> -->
      <el-button v-permission="['GET /admin/distance/distance']" class="filter-item" type="primary" icon="el-icon-search"
        @click="handleFilter">查找</el-button>
        <br/>
        <el-row><span style="color:red">此功能仅筛选地址与加盟商接单点最近的数据（不可作为派单分配依据）</span></el-row>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" min-width="150px" label="名称（接单点）" prop="shopName" />
      <el-table-column align="center" width="220px" label="归属加盟商" prop="fraName" />
      <!-- <el-table-column align="center" width="90px" label="美团渠道" prop="isMt" > 
        <template slot-scope="scope">
          <el-tag :type="scope.row.isMt?'success':'warning'">{{ scope.row.isMt?'接单':'不接单' }}</el-tag>
        </template>
      </el-table-column> -->
      <el-table-column align="center" min-width="300px" label="地址" prop="shopAddress" />
      <el-table-column align="center" width="90px" label="距离" prop="distance">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.distance }}KM</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" width="80px" label="渠道" prop="channel">
        <template slot-scope="scope">
          <el-tag>{{  channelsFilter(scope.row.channel) }}</el-tag>
        </template>
      </el-table-column>  -->
      <!-- <el-table-column align="center" min-width="100px" label="门店属性" prop="shopAddress"><template slot-scope="scope">
          <el-tag :type="scope.row.fraId && scope.row.fraId ? 'info' : 'warning'">{{
            scope.row.channel == 4 ? '盒马' : (scope.row.fraId == 293 ? '鲸致' : '加盟商') }}</el-tag>
        </template>
      </el-table-column> --> 

      <el-table-column align="center" width="80px" label="接单状态" prop="fraName"  >
        <template slot-scope="scope">
          <span v-if="scope.row.franchisee.pauseOrder!=null">{{scope.row.franchisee.pauseOrder==0?'开启':'暂停'}}</span>
        </template> 
        </el-table-column>
        <el-table-column align="center" width="200px" label="暂停日期" prop="pauseStartDate"  >
        <template slot-scope="scope"> 
          <span v-if="scope.row.franchisee.pauseStartDate!=null&&scope.row.franchisee.pauseOrder!=null&&scope.row.franchisee.pauseOrder==1">{{scope.row.franchisee.pauseStartDate}}到{{scope.row.franchisee.pauseEndDate}}</span>
        </template> 
        </el-table-column>
      <!-- <el-table-column align="center" min-width="100px" label="权重值" prop="franchisee.weight" />  -->
    </el-table>

  </div>
</template>

<script>
import { distance } from "@/api/distance";
import { getUserCity } from "@/api/city";
import Pagination from "@/components/Pagination"; // Secondary package based on el-pagination
import { allChannel } from "@/api/channel";
export default {
  name: "distance",
  components: { Pagination },
  data() {
    return {
      channels:[],
      list: [],
      city: [],
      total: 0,
      listLoading: false,
      listQuery: {
        address: undefined,
        cityId: 1,
        orderChannel:'',
      },
      dataForm: {
        id: undefined,
        keyword: undefined,
        url: undefined,
        isHot: undefined,
        isDefault: undefined,
      },
      dialogFormVisible: false,
      dialogStatus: "",
      textMap: {
        update: "编辑",
        create: "创建",
      },
      rules: {
        keyword: [
          { required: true, message: "关键词不能为空", trigger: "blur" },
        ],
      },
      downloadLoading: false,
    };
  },
  created() {
    getUserCity().then((res) => {
      this.city = res.data.data;
    });
    allChannel().then((res) => {
      this.channels = res.data.data;
    });
  },
  methods: {
    channelsFilter(value) {
      const rs = this.channels.filter((item) => {
        return item.id === value;
      });
      if (rs != null && rs.length > 0) {
        return rs[0].name;
      }
    },
    getList() {
      this.listLoading = true;
      distance(this.listQuery)
        .then((response) => {
          this.list = response.data.data;
          this.listLoading = false;
        })
        .catch((res) => {
          this.list = [];
          this.total = 0;
          this.listLoading = false;
          this.$notify.error({
            title: "失败",
            message: res.data.errmsg,
          });
        });
    },
    handleFilter() {
      this.getList();
    },
    resetForm() {
      this.dataForm = {
        id: undefined,
        keyword: undefined,
        url: undefined,
        isHot: undefined,
        isDefault: undefined,
      };
    },
    handleCreate() {
      this.resetForm();
      this.dialogStatus = "create";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    createData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          createKeyword(this.dataForm)
            .then((response) => {
              this.list.unshift(response.data.data);
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "创建成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.errmsg,
              });
            });
        }
      });
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row);
      this.dialogStatus = "update";
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].clearValidate();
      });
    },
    updateData() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          updateKeyword(this.dataForm)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v);
                  this.list.splice(index, 1, this.dataForm);
                  break;
                }
              }
              this.dialogFormVisible = false;
              this.$notify.success({
                title: "成功",
                message: "更新成功",
              });
            })
            .catch((response) => {
              this.$notify.error({
                title: "失败",
                message: response.data.errmsg,
              });
            });
        }
      });
    },
    handleDelete(row) {
      deleteKeyword(row)
        .then((response) => {
          this.$notify.success({
            title: "成功",
            message: "删除成功",
          });
          const index = this.list.indexOf(row);
          this.list.splice(index, 1);
        })
        .catch((response) => {
          this.$notify.error({
            title: "失败",
            message: response.data.errmsg,
          });
        });
    },
    handleDownload() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "关键词ID",
          "关键词",
          "跳转链接",
          "是否推荐",
          "是否默认",
        ];
        const filterVal = ["id", "keyword", "url", "isHot", "isDefault"];
        excel.export_json_to_excel2(
          tHeader,
          this.list,
          filterVal,
          "关键词设置"
        );
        this.downloadLoading = false;
      });
    },
  },
};
</script>
