<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="外呼号码" prop="F_Tel">
        <el-input v-model="ruleForm.F_Tel" placeholder="请输入外呼号码"/>
      </el-form-item>
      <el-form-item label="外呼任务" prop="productName">
        <el-table
          v-loading="loading"
          :data="taskDataLists"
          border
          stripe
          highlight-current-row
          @current-change="handleCurrentChangeTask"
        >
          <el-table-column type="index" label="编号" align="center" fixed width="80" />
          <el-table-column prop="F_TaskUser" label="任务人" align="center" min-width />
          <el-table-column prop="F_State" label="任务状态" align="center">
            <template slot-scope="scope">{{ scope.row.F_State | judgmentState }}</template>
          </el-table-column>
          <el-table-column prop="F_Name" label="任务名称" align="center" />
          <el-table-column prop="F_StartTime" label="任务开始时间" align="center" />
          <el-table-column prop="F_EndTime" label="任务结束时间" align="center" />
          <el-table-column prop="F_Remarks" label="任务说明" align="center" />
        </el-table>
        <pagination
          v-show="(pageParams.total > 0)"
          :total="pageParams.total"
          :pageindex.sync="pageParams.pageindex"
          :pagesize.sync="pageParams.pagesize"
          class="pagination"
          @pagination="getListTask"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getOutboundNumberDetail, editOutboundNumber, getTaskLists } from '@/api/outbound/automaticOutbound'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'
import Pagination from "@/components/Pagination"; // 对el-pagination 二次封装

export default {
  name: 'EditOutboundNumber',
  components: {
    Pagination
  },
  filters: {
    judgmentState(status) {
      const statusMap = {
        0: "未启动",
        1: "启动",
        2: "暂停",
        3: "终止",
      };
      return statusMap[status];
    },
  },
  props: {
    rowid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    },
    taskId: {
      type: Number,
      default: 0
    },
  },
  data() {
    return {
      pageParams: {
        pageindex: 1, // 当前第几页
        pagesize: Number(this.$store.getters.serverConfig.PAGESIZE), // 每页几条数据
        total: 0, // 总共多少数据
      },
      taskDataLists: [], //任务列表
      ruleForm: {
        F_ID: '', //外呼号码id
        F_Parentid: '', //任务id
        F_LastCallid: '', //通话记录id
        F_Tel: '', //外呼号码
      },
      rules: {
        F_Tel: [{
          required: true,
          trigger: 'blur',
          message: '请输入外呼号码',
        }]
      },
      loading: false
    }
  },
  created() {
    this.getListTask();
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.F_ID = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          // 编辑
          editOutboundNumber(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getListOutboundNumber(this.taskId) // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    // 详情
    getDetail(rid) {
      const data = {
        id: rid
      }
      getOutboundNumberDetail(data).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
            this.ruleForm.F_Parentid = res.F_Parentid //任务id
            this.ruleForm.F_LastCallid = res.F_LastCallid //通话记录id
            this.ruleForm.F_Tel = res.F_Tel //外呼号码
        }
      })
    },
    //获取任务列表
    getListTask() {
      this.loading = true;
      return new Promise((resolve) => {
        const params = {
          pageindex: this.pageParams.pageindex, // int 第几页
          pagesize: this.pageParams.pagesize, // int 每页几条信息
          keyword: "", //关键字
        };
        getTaskLists(params).then((response) => {
          this.loading = false;
          if (response.state.toLowerCase() === "success") {
            this.pageParams.total = response.rows.Totals;
            this.taskDataLists = response.rows.Rows;
          }
        });
        resolve();
      });
    },
    handleCurrentChangeTask(currentRow, oldCurrentRow) {
      this.ruleForm.F_Parentid = currentRow.F_ID; //外呼任务行id
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form {
  .form_select {
    width: 100%;
  }
  .form_date {
    width: 100%
  }
}
</style>