<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" label-width="68px">
      <el-form-item label="任务编码" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行人" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行时间">
        <el-date-picker
          size="mini"
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item size="mini" label="拜访目的" prop="purpose">
        <el-select v-model="queryParams.purpose" placeholder="" clearable>
          <el-option
            v-for="dict in dict.type.mk_bo_behavior_goal"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="btn_grooup">
      <el-button
        type="warning"
        plain
        icon="el-icon-download"
        size="mini"
        @click="handleExport"
      >导出</el-button>
    </div>
    <el-table v-loading="loading" :data="behaviorList">
      <el-table-column label="负责人" align="center" prop="staffName" />
      <el-table-column label="行动日期" align="center" prop="time" />
      <el-form-item label="行动日期" prop="time">
        <el-date-picker clearable
          v-model="form.time"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择行动日期">
        </el-date-picker>
      </el-form-item>
      <el-table-column label="联系人" align="center" prop="linkmanName" />
      <el-table-column label="拜访效果" align="center" prop="result" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_behavior_res" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column label="是否上级协助" align="center" prop="assist" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.assist"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="协助内容" align="center" prop="assistContent" />
      <el-table-column show-overflow-tooltip label="洽谈内容" align="center" prop="content" />
      <el-table-column width="150" label="任务编码" align="center" prop="taskCode"/>
      <el-table-column label="行动类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_behavior_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="客户" align="center" prop="customerName"/>
      <el-table-column label="拜访目的" align="center" prop="purpose">
        <template slot-scope="scope">
            <dict-tag :options="dict.type.mk_bo_behavior_goal" :value="scope.row.purpose"/>
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip label="销售组织" align="center" prop="salesOrgName"/>
      <el-table-column show-overflow-tooltip label="部门" align="center" prop="deptName"/>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
       <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleBrowse(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
      class="paginationClass"
    />
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form size="mini" ref="form" :model="form" :rules="rules" label-width="80px" :disabled="this.operatingState == 'Browse'">
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">基本信息</dev>
        </el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="任务" prop="taskId">
              <el-input v-model="form.taskCode"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行动类型" prop="type">
              <el-select v-model="form.type" placeholder="">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="行动日期" prop="time">
              <el-date-picker clearable
                v-model="form.time"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerName">
              <el-input v-model="form.customerName"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人" prop="linkmanName">
              <dr-popover-select v-model="form.linkmanName" title="联系人" type="LINKMAN_PARAM" :dataMapping="{
                  linkman: 'id',
                  linkmanName: 'name',
                }" :source.sync="form"
                :queryParams="additionalCondition"
              >
              </dr-popover-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拜访目的" prop="purpose">
              <el-select v-model="form.purpose" placeholder="">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_goal"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col  :span="8">
            <el-form-item label="拜访效果" prop="result">
              <el-select v-model="form.result">
                <el-option
                  v-for="dict in dict.type.mk_bo_behavior_res"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="是否上级协助" prop="assist">
              <el-select v-model="form.assist">
                <el-option
                  v-for="dict in dict.type.sys_yes_no"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="协助内容" prop="assistContent" v-show="form.assist == 'Y'" :rules="form.assist == 'Y' ? rules.assistContent : [{require: false}]">
              <el-input v-model="form.assistContent"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="销售区域" prop="marketingAreaName">
              <dr-popover-select v-model="form.marketingAreaName" title="销售区域" type="MK_SALESAREA_PARAM" :dataMapping="{
                  marketingArea: 'id',
                  marketingAreaName: 'name',
                }" :source.sync="form"
              >
              </dr-popover-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col  :span="8">
            <el-form-item label="销售组织" prop="salesOrgName">
              <el-input v-model="form.salesOrgName" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
             <el-form-item label="部门" prop="deptName">
              <el-input v-model="form.deptName" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col  :span="8">
            <el-form-item label="负责人" prop="staffName">
              <el-input v-model="form.staffName" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">跟进内容</dev>
        </el-divider>
        <el-form-item label="内容" prop="content" >
          <el-input
            type="textarea"
            :rows="2"
            maxlength=900
            placeholder="填写提示：今天拜访了谁，收集了什么述求，达成了什么结果，下一步计划。"
            autosize
            v-model="form.content">
          </el-input>
        </el-form-item>
        <el-divider content-position="left">
          <dev style="width: 50px; height: 40px; font-size: 18px">照片信息</dev>
        </el-divider>

        <el-upload
          action="actionUrl"
          list-type="picture-card"
          :on-change="uploadPic"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :auto-upload="false"
          :file-list="fileList"
          >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
        
        <div v-if="form.stage">
          <el-divider content-position="left">
            <dev style="width: 50px; height: 40px; font-size: 18px">签卡信息</dev>
          </el-divider>
          <el-table v-loading="loading" :data="form.behaviorXys" height="150px">
            <el-table-column width="150" label="签卡类型" align="center" prop="type">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.mk_bo_signintype" :value="scope.row.type"/>
              </template>
            </el-table-column>
            <el-table-column width="150" label="签卡时间" align="center" prop="createTime" />
            <el-table-column label="签到地址" align="center" prop="address" />
          </el-table>
        </div>

        <div class="md-auditInfo">
          <el-divider content-position="left">
            <dev style="width: 50px; height: 40px; font-size: 18px">其它信息</dev>
          </el-divider>
          <el-row>
            <el-col :span="6">
              <el-form-item label="创建人">
                <el-input v-model="form.createByName" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建时间">
                <el-input v-model="form.createTime" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="修改人">
                <el-input v-model="form.updateByName" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="修改时间">
                <el-input v-model="form.updateTime" readonly></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listBehavior, getBehavior} from "@/api/business/spd/bo/behavior";

export default {
  name: "Behavior",
  dicts: ['mk_bo_behavior_res','mk_bo_behavior_type','sys_yes_no','mk_bo_behavior_goal','mk_bo_signintype'],
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      disabled: false,
      actionUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址,
      fileList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 行动表格数据
      behaviorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskId: null,
        type: null,
        time: null,
        customer: null,
        customerName: null,
        linkman: null,
        linkmanName: null,
        purpose: null,
        result: null,
        assist: null,
        assistContent: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        content: null,
        tenantId: null,
        revision: null,
      },
      // 查询日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "行动类型不能为空", trigger: "blur" }
        ],
        time: [
          { required: true, message: "行动日期不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        linkmanName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        purpose: [
          { required: true, message: "拜访目的不能为空", trigger: "blur" }
        ],
        assist: [
          { required: true, message: "是否需要上级协助不能为空", trigger: "blur" }
        ],
        assistContent: [
          { required: true, message: "协助内容不能为空", trigger: "blur" }
        ],
        staffName: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "洽谈内容不能为空", trigger: "blur" }
        ],
        result: [
          { required: true, message: "拜访效果不能为空", trigger: "blur" }
        ],
      },
      //当前操作状态
      operatingState: '',
      pickerOptions: {
        shortcuts: [{
          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]);
          }
        }]
      },
    };
  },
  created() {
    this.queryParams.params = {source:"list"};
    this.getList();
  },
  methods: {
    uploadPic(file, fileList) {
      this.fileList = fileList
    },
    handleRemove(file) {
      this.fileList = this.fileList.filter(item => item.uid !== file.uid)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    /** 查询行动列表 */
    getList() {
      this.loading = true;
      listBehavior(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.behaviorList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查看按钮操作 */
    handleBrowse(row) {
      this.reset();
      const id = row.id || this.ids
      getBehavior(id).then(response => {
        this.form = response.data;
        this.fileList = this.form.behaviorPs;
        this.open = true;
        this.operatingState = "Browse";
        this.title = "基础信息";
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskCode: null,
        type: null,
        time: null,
        customer: null,
        customerName: null,
        linkman: null,
        linkmanName: null,
        purpose: null,
        result: null,
        assist: null,
        assistContent: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        content: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.params.pageNot = '1';
      this.download('mk/bo/behavior/export', {
        ...this.queryParams
      }, `behavior_${new Date().getTime()}.xlsx`)
    },
    additionalCondition(){
      return {
        parame:{
          customer: this.form.customer ? this.form.customer : 'xxx'
        }
      }
    },
  }
};
</script>

<style lang="scss" scoped>
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
.paginationClass {
  z-index: 500;
  position: fixed;
  bottom: 10px;
  right: 10px;
  width: 100%;
  line-height: var(--footer-height);
  color: #fff;
}
</style>
