<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <!--
      <el-form label-width="77px" class="ele-form-search">
        <el-row :gutter="15">
          <el-col :md="6" :sm="12">
            <el-form-item label="客户:">
              <el-input
                placeholder="请输入手机号"
                clearable
                v-model="where.title"
              />
            </el-form-item>
          </el-col>
          <el-col :md="6" :sm="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload"
              >
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      -->
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        cache-key="receiverCustomerTables"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar></template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link type="primary" :underline="false" @click="goDetail(row)">
            查看
          </el-link>
          <template v-if="0 === row.ifcheck">
            <el-link type="primary" :underline="false" @click="checkPass(row)">
              审核通过
            </el-link>
            <el-link
              type="primary"
              :underline="false"
              @click="showCheckFail(row)"
            >
              不通过
            </el-link>
          </template>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <edit :data="current" :visible.sync="showEdit" @done="reload" />
    <!-- 审核不通过 -->
    <el-dialog title="审核不通过原因" :visible.sync="reasonVisible" width="30%">
      <el-form ref="form" label-width="90px">
        <el-form-item label="不通过原因:">
          <el-input
            clearable
            v-model="reason"
            placeholder="请输入不通过原因"
            :rows="4"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="reasonVisible = false">取消</el-button>
          <el-button type="primary" @click="saveCheckFail">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import { pageSubsidy, checkSubsidy } from '@/api/receiver/subsidy';
  import edit from './components/edit.vue';

  export default {
    name: 'receiverClues',
    components: { edit },
    data() {
      return {
        where: {
          title: '',
          selectedCar: {
            brandId: '',
            seriesId: '',
            modelId: ''
          }
        },
        // 表格列配置
        columns: [
          {
            prop: 'sid',
            label: '订单号',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'cityName',
            label: '购车城市',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'bizName',
            label: '购车门店',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'brandName',
            label: '品牌车型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'statusCn',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            prop: 'cTime',
            label: '提交时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 80
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            resizable: false,
            slot: 'action'
          }
        ],
        // 表格选中数据
        selection: [],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
        // 日期时间选择器快捷项
        pickerOptions: {
          shortcuts: [
            {
              text: '今天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '昨天',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24);
                end.setTime(end.getTime() - 3600 * 1000 * 24);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一周',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近一个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit('pick', [start, end]);
              }
            },
            {
              text: '最近三个月',
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit('pick', [start, end]);
              }
            }
          ]
        },
        reasonVisible: false,
        reason: ''
      };
    },
    created() {},
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    methods: {
      /* 下拉按钮点击 */
      dropClick(command, row) {
        if (command === 'edit') {
          this.current = row;
          this.showEdit = true;
        } else if (command === 'brokerage') {
          this.current = row;
          this.showEditBrokerage = true;
        } else if (command === 'coupon') {
          const path = '/car/product/coupon';
          this.$nextTick(() => {
            this.$router.push({
              path,
              query: row ? { id: row.id, title: row.title } : undefined
            });
          });
        }
      },
      goDetail(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageSubsidy({ ...where, ...order, page, limit });
      },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload({ page: 1, where: this.where });
      },
      /* 重置搜索 */
      reset() {
        this.where = {};
      },
      checkPass(row) {
        this.$confirm('确定审核通过【' + row.sid + '】订单吗?', '提示', {
          type: 'warning'
        })
          .then(() => {
            const loading = this.$loading({ lock: true });
            checkSubsidy({ id: row.id, status: 1 })
              .then((msg) => {
                loading.close();
                this.$message.success(msg);
                this.reload();
              })
              .catch((e) => {
                loading.close();
                this.$message.error(e.message);
              });
          })
          .catch(() => {});
      },
      showCheckFail(row) {
        this.reasonVisible = true;
        this.current = row;
      },
      saveCheckFail() {
        const loading = this.$loading({ lock: true });
        checkSubsidy({
          id: this.current.id,
          status: 0,
          reason: this.reason
        })
          .then((msg) => {
            this.reasonVisible = false;
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      }
    }
  };
</script>
<style scoped></style>
