<template>
  <div id="form-1">
    <el-row>
      <el-col
        :span="24"
        class="main animate__animated animate__backInRight"
        v-loading="loadings"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
      >
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="9" class="one">
          <el-col :span="24" class="add">
            <el-col :span="2">
              <i class="el-icon-location"></i>
            </el-col>
            <el-col :span="22">
              <el-col>
                <p>{{ address.name }},{{ address.phone }}</p>
              </el-col>
              <el-col>
                <p>{{ address.province }} , {{ address.city }} , {{ address.area }} , {{ address.address }}</p>
              </el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="shop"> <i class="el-icon-s-shop"></i>{{ shop.name }}</el-col>
          <el-col :span="24" v-for="(item, index) in list" :key="index">
            <el-col :span="24" class="goods bode">
              <el-col :span="6"><el-image :src="item.goods.file[0].url"></el-image></el-col>
              <el-col :span="18">
                <el-col :span="12">
                  <el-col>
                    <p>{{ item.goods.name }}</p>
                  </el-col>
                  <el-col>
                    <p>规格：{{ item.name }}</p>
                  </el-col>
                </el-col>
                <el-col :span="12" class="money">
                  <el-col>
                    <p v-if="info.type == '0'">￥{{ item.sell_money }}</p>
                    <p v-else-if="info.type == '1'">￥{{ item.group_config.money }}</p>
                  </el-col>
                  <el-col>
                    <p>X{{ item.buy_num }}</p>
                  </el-col>
                  <el-col>
                    <span class="spanfour" style="color: blue" @click="toUrl(item.goods.url)"> 查看商品来源:{{ item.goods.source }}</span>
                  </el-col>
                  <el-col>
                    <p>{{ item.is_afterSale == true ? '已申请售后' : '未申请售后' }}</p>
                  </el-col>
                  <el-col>
                    <p>{{ item.is_rate == true ? '已评价' : '未评价' }}</p>
                  </el-col>
                </el-col>
              </el-col>
            </el-col>
            <el-col :span="24" class="bode">
              <el-col :span="6">运费</el-col>
              <el-col :span="18" class="other" v-if="!item.goods.freight == '0'">{{ item.goods.freight }}</el-col>
              <el-col :span="18" class="other" v-else>包邮</el-col>
            </el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">订单备注</el-col>
            <el-col :span="18" class="other" v-if="info.remarks">{{ info.remarks }}</el-col>
            <el-col :span="18" class="other" v-else>暂无备注</el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">配送方式</el-col>
            <el-col :span="18" class="other">快递配送</el-col>
          </el-col>
          <el-col :span="24" class="goods_total bode">
            <el-col :span="6">快递费</el-col>
            <el-col :span="18" class="other">
              <p>￥{{ total_detail.freight_total }}</p>
            </el-col>
          </el-col>
          <el-col :span="24" class="goods_total bode">
            <el-col :span="8">优惠后实付金额</el-col>
            <el-col :span="16" class="other">
              <p>￥{{ pay.pay_money }}</p>
            </el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">下单时间</el-col>
            <el-col :span="18" class="other">{{ info.buy_time }}</el-col>
          </el-col>
          <el-col :span="24" class="bode">
            <el-col :span="6">支付时间</el-col>
            <el-col :span="18" class="other">{{ info.pay_time }}</el-col>
          </el-col>
          <el-col :span="24" class="bode" v-if="info.status == '3'">
            <el-col :span="6">寄出运单号</el-col>
            <el-col :span="18" class="other">{{ transport.shop_transport_no || '暂无单号' }}</el-col>
          </el-col>
          <el-col :span="24" class="bode" v-if="info.status == '3'">
            <el-col :span="6">寄出快递公司</el-col>
            <el-col :span="18" class="other">{{ transport.shop_transport_name || '暂无快递信息' }}</el-col>
          </el-col>
        </el-col>
        <el-col :span="9" class="one">
          <el-button type="danger" @click="toSales('1')" v-if="this.status == '1'"> 取消订单 </el-button>
          <el-button type="danger" @click="toSales('2')" v-if="this.status == '2' || this.status == '2-'"> 拒收 </el-button>
          <el-col v-if="this.status == '3'">
            <el-form :model="form" :rules="rules" ref="form" label-width="180px">
              <el-col :span="24">
                <el-form-item label="售后商品" prop="goods">
                  <el-select v-model="form.goods" clearable filterable placeholder="请选择售后商品" size="small" style="width: 100%" @change="goodsChange">
                    <el-option v-for="i in list" :key="i.goods.name" :label="i.goods.name" :value="i.id"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="售后类型" prop="type">
                  <el-select v-model="form.type" clearable filterable placeholder="请选择售后类型" size="small" style="width: 100%" @change="typeChange">
                    <el-option v-for="i in typeList" :key="i.value" :label="i.label" :value="i.value"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24" v-if="this.form.type == '1' || this.form.type == '2'">
                <el-form-item label="退款金额" prop="money">
                  <el-input v-model="form.money" placeholder="请输入退款金额" size="small" type="Number" @input="toMoney"></el-input>
                  <p>
                    退款金额不得超过<span style="color: red">{{ payMoney.payTotal || '' }}</span
                    >元
                    <span @click="allMoney" style="color: blue; margin: 0 0 0 10px">全部退款</span>
                  </p>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="申请理由" prop="reason">
                  <el-select v-model="form.reason" clearable filterable placeholder="请选择申请理由" size="small" style="width: 100%">
                    <el-option v-for="i in reasonList" :key="i.value" :label="i.label" :value="i.value"> </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="售后描述" prop="desc">
                  <el-input v-model="form.desc" placeholder="请输入售后描述" size="small" type="textarea"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="附件" prop="file">
                  <c-upload :url="url" v-model="form.file" :limit="6"></c-upload>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="btn">
                <el-button type="primary" size="mini" @click="onSubmit('form')">提交</el-button>
                <el-button type="danger" size="mini" @click="toBack()">取消</el-button>
              </el-col>
            </el-form>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
const moment = require('moment');

import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions: orderDetail } = createNamespacedHelpers('orderDetail');
const { mapActions } = createNamespacedHelpers('afterSale');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
const { mapActions: sot } = createNamespacedHelpers('sot');

export default {
  name: 'form-1',
  props: { id: { type: String }, status: { type: String } },
  components: { cUpload: () => import('@/components/usual/c-upload.vue') },
  data: function () {
    return {
      info: {},
      // 地址
      address: {},
      // 实付金额
      total_detail: {},
      // 商铺
      shop: {},
      // 运单号
      transport: {},
      pay: {},
      // 商品列表
      list: [],
      // 物流
      reverse: false,
      activities: [],
      activit: {},
      // 订单状态
      order_processList: [],
      // 快递公司
      shop_transport_typeList: [],
      loading: false,
      rules: {
        goods: [{ required: true, message: '请选择商品', trigger: 'change' }],
        type: [{ required: true, message: '请选择售后类型', trigger: 'change' }],
        money: [{ required: true, message: '请输入金额', trigger: 'blur' }],
      },
      // 售后表单
      form: {},
      // 售后类型
      typeList: [],
      // 售后申请理由
      reasonList: [],
      // 退款金额
      payMoney: {},
      url: '/files/point/afterSale/upload',
      loadings: true,
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...orderDetail({ orderFetch: 'fetch' }),
    ...sot({ sotFetch: 'fetch' }),
    ...mapActions(['query', 'cgfr', 'fetch', 'create', 'update']),
    // 查询
    async search() {
      let res;
      res = await this.orderFetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `info`, res.data);
        // 地址
        this.$set(this, `address`, res.data.address);
        this.$set(this, `shop`, res.data.shop);
        // 商品
        this.$set(this, `list`, res.data.goods);
        // 应付金额
        this.$set(this, `total_detail`, res.data.total_detail);
        this.$set(this, `pay`, res.data.order.pay);
      }
      this.loadings = false;
    },
    toUrl(url) {
      if (url) {
        window.open(url, '_blank');
      } else {
        this.$message.error('该商品还未添加来源网址，无法跳转');
      }
    },
    handleSelect(value) {},
    async toSales(status) {
      if (status == '1') {
        var info = { order_detail: this.id, type: '4' };
      } else if (status == '2') {
        var info = { order_detail: this.id, type: '5' };
      }
      this.$confirm('是否确认申请售后', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(async () => {
        let res = await this.create(info);
        if (this.$checkRes(res)) {
          this.$message({ type: `success`, message: `申请售后成功` });
          this.search();
        }
      });
    },
    // 选择商品
    goodsChange(value) {
      this.$set(this.form, `goods`, value);
      this.typeChange(this.form.type || '');
    },
    // 选择类型
    async typeChange(value) {
      this.$set(this.form, `type`, value);
      if (value != '3') {
        let info = {
          order_detail: this.id,
          goods_id: this.form.goods,
        };
        let res = await this.cgfr(info);
        if (this.$checkRes(res)) {
          this.$set(this, `payMoney`, res.data);
          this.allMoney();
        }
      }
    },
    // 全部退款
    allMoney() {
      this.$set(this.form, `money`, this.payMoney.payTotal);
    },
    // 输入金额
    toMoney(val) {
      if (val > this.payMoney.payTotal) {
        this.$message.error('超过退款最大金额');
        this.$set(this.form, `money`, '');
      }
    },
    // 提交
    async onSubmit(formName, val) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$confirm('是否确认保存', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(async () => {
            let form = this.form;
            form.order_detail = this.id;
            let res = await this.create(form);
            if (this.$checkRes(res)) {
              this.$message({ type: `success`, message: `申请售后成功` });
              this.search();
            }
          });
        } else {
          return false;
        }
      });
    },
    // 返回
    toBack() {
      this.$emit('toBack');
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 减免方式
      res = await this.dictQuery({ code: 'order_process' });
      if (this.$checkRes(res)) this.$set(this, `order_processList`, res.data);
      // 售后类型
      res = await this.dictQuery({ code: 'afterSale_type' });
      if (this.$checkRes(res)) {
        let type = res.data.filter((i) => i.value != '4' && i.value != '5');
        this.$set(this, `typeList`, type);
      }
      // 售后申请理由
      res = await this.dictQuery({ code: 'afterSale_reason' });
      if (this.$checkRes(res)) {
        let type = res.data.filter((i) => i.value != '4' && i.value != '5');
        this.$set(this, `reasonList`, res.data);
      }
    },
  },
  computed: {},
  metaform() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  .one {
    margin: 10px 0 0 5%;
    padding: 5px;
    .add {
      border-bottom: 2px dashed #ccc;
      margin: 0 0 5px 0;
      padding: 5px 0;
    }
    .shop {
      padding: 4px 0;
      font-size: 18px;
      border-bottom: 1px solid #ccc;
    }
    .goods {
      padding: 10px 0;
      .money {
        text-align: right;
      }
    }
    .other {
      text-align: right;
      word-break: break-all;
      p {
        color: red;
      }
      .spanfour {
        color: blue;
      }
    }
    .el-col {
      margin: 3px 0;
    }
    .bode {
      border-bottom: 1px solid rgb(238, 237, 237);
    }
    .form {
      margin: 20px 0 0 0;
    }
  }
}
.btn {
  text-align: center;
}

/deep/.el-timeline {
  margin: 20px 0 0 0;
  font-size: 16px;
}
/deep/.el-timeline-item__node:nth-child(1) {
  background-color: green;
}
</style>
